Skip to content

Commit

Permalink
* Added: Device name can be changed in the config.ini
Browse files Browse the repository at this point in the history
* Added: Device instance can be changed in the `config.ini`
* Added: How to create multiple instances in `README.md`
* Changed: Topic variable name in `config.ini`
  • Loading branch information
mr-manuel committed Mar 10, 2023
1 parent 950eab3 commit 1563670
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.1.0
* Added: Device name can be changed in the `config.ini`
* Added: Device instance can be changed in the `config.ini`
* Added: How to create multiple instances in `README.md`
* Changed: Topic variable name in `config.ini`

## v0.0.2
* Added: Set logging level in `config.default.ini`
* Changed: Logging levels of different messages for clearer output
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ If the seconds are under 5 then the service crashes and gets restarted all the t

If the script stops with the message `dbus.exceptions.NameExistsException: Bus name already exists: com.victronenergy.grid.mqtt_grid"` it means that the service is still running or another service is using that bus name.

### Multiple instances

It's possible to have multiple instances, but it's not automated. Follow these steps to achieve this:

1. Save the new name to a variable `driverclone=dbus-mqtt-grid-2`

2. Copy current folder and add a number `cp -r /data/etc/dbus-mqtt-grid/ /data/etc/$driverclone/`

3. Rename the main script `mv /data/etc/$driverclone/dbus-mqtt-grid.py /data/etc/$driverclone/$driverclone.py`

4. Fix the script references for service and log
```
sed -i 's:dbus-mqtt-grid:'$driverclone':g' /data/etc/$driverclone/service/run
sed -i 's:dbus-mqtt-grid:'$driverclone':g' /data/etc/$driverclone/service/log/run
```
5. Change the `device_name` and increase the `device_instance` in the `config.ini`
Now you can install and run the cloned driver. Should you need another instance just increase the number in step 1 and repeat all steps.
### Compatibility
It was tested on Venus OS Large `v2.92` on the following devices:
Expand Down
12 changes: 10 additions & 2 deletions dbus-mqtt-grid/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
; default: WARNING
logging = WARNING

; Device name
; default: MQTT Grid
device_name = MQTT Grid

; Device VRM instance
; default: 31
device_instance = 31

; used when no voltage is received
voltage = 230

Expand Down Expand Up @@ -43,6 +51,6 @@ broker_port = 1883
; Password used for connection
;password = mypassword

; Topic where the meters data as JSON string is published
; Topic where the grid data as JSON string is published
; minimum required JSON payload: {"grid": { "power": 0.0 } }
topic_meters = enphase/envoy-s/meters
topic = enphase/envoy-s/meters
16 changes: 9 additions & 7 deletions dbus-mqtt-grid/dbus-mqtt-grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def on_connect(client, userdata, flags, rc):
if rc == 0:
logging.info("MQTT client: Connected to MQTT broker!")
connected = 1
client.subscribe(config['MQTT']['topic_meters'])
client.subscribe(config['MQTT']['topic'])
else:
logging.error("MQTT client: Failed to connect, return code %d\n", rc)

Expand All @@ -112,7 +112,7 @@ def on_message(client, userdata, msg):
grid_L3_power, grid_L3_current, grid_L3_voltage, grid_L3_forward, grid_L3_reverse

# get JSON from topic
if msg.topic == config['MQTT']['topic_meters']:
if msg.topic == config['MQTT']['topic']:
if msg.payload != '' and msg.payload != b'':
jsonpayload = json.loads(msg.payload)

Expand Down Expand Up @@ -175,6 +175,7 @@ def __init__(
deviceinstance,
paths,
productname='MQTT Grid',
customname='MQTT Grid',
connection='MQTT Grid service'
):

Expand All @@ -192,8 +193,8 @@ def __init__(
self._dbusservice.add_path('/DeviceInstance', deviceinstance)
self._dbusservice.add_path('/ProductId', 0xFFFF)
self._dbusservice.add_path('/ProductName', productname)
self._dbusservice.add_path('/CustomName', productname)
self._dbusservice.add_path('/FirmwareVersion', '0.0.2')
self._dbusservice.add_path('/CustomName', customname)
self._dbusservice.add_path('/FirmwareVersion', '0.1.0')
#self._dbusservice.add_path('/HardwareVersion', '')
self._dbusservice.add_path('/Connected', 1)

Expand Down Expand Up @@ -272,7 +273,7 @@ def main():


# MQTT setup
client = mqtt.Client("MqttGrid")
client = mqtt.Client("MqttGrid_" + str(config['MQTT']['device_instance']))
client.on_disconnect = on_disconnect
client.on_connect = on_connect
client.on_message = on_message
Expand Down Expand Up @@ -357,8 +358,9 @@ def main():


pvac_output = DbusMqttGridService(
servicename='com.victronenergy.grid.mqtt_grid',
deviceinstance=31,
servicename='com.victronenergy.grid.mqtt_grid_' + str(config['MQTT']['device_instance']),
deviceinstance=int(config['MQTT']['device_instance']),
customname=config['MQTT']['device_name'],
paths=paths_dbus
)

Expand Down

0 comments on commit 1563670

Please sign in to comment.