Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Logging, 3EM as pvinverter, 3EM Position #22

Merged
merged 17 commits into from
Feb 27, 2023
Merged
12 changes: 8 additions & 4 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[DEFAULT]
AccessType = OnPremise
SignOfLifeLog = 5
GridMeterBalancing = 1
LogLevel=DEBUG
SignOfLifeLog = 1
CustomName = 3EM_OG
DeviceInstance = 42
# Role Could be pvinverter or grid
Role = grid
# Available Postions: 0 = AC, 1 = AC-Out 1, AC-Out 2
Position = 0

[ONPREMISE]
Host=192.168.2.13
Host=192.168.178.236
Username=
Password=
37 changes: 26 additions & 11 deletions dbus-shelly-3em-smartmeter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python

# vim: ts=2 sw=2 et

# import normal packages
import platform
import logging
import logging.handlers
import sys
import os
import sys
Expand All @@ -21,7 +23,24 @@


class DbusShelly3emService:
def __init__(self, servicename, deviceinstance, paths, productname='Shelly 3EM', connection='Shelly 3EM HTTP JSON service'):
def __init__(self, paths, productname='Shelly 3EM', connection='Shelly 3EM HTTP JSON service'):
config = self._getConfig()
deviceinstance = int(config['DEFAULT']['DeviceInstance'])
customname = config['DEFAULT']['CustomName']
role = config['DEFAULT']['Role']

allowed_roles = ['pvinverter','grid']
if role in allowed_roles:
servicename = 'com.victronenergy.' + role
else:
logging.error("Configured Role: %s is not in the allowed list")
exit()

if role == 'pvinverter':
productid = 0xA144
else:
productid = 45069

self._dbusservice = VeDbusService("{}.http_{:02d}".format(servicename, deviceinstance))
self._paths = paths

Expand All @@ -34,17 +53,15 @@ def __init__(self, servicename, deviceinstance, paths, productname='Shelly 3EM',

# Create the mandatory objects
self._dbusservice.add_path('/DeviceInstance', deviceinstance)
#self._dbusservice.add_path('/ProductId', 16) # value used in ac_sensor_bridge.cpp of dbus-cgwacs
#self._dbusservice.add_path('/ProductId', 0xFFFF) # id assigned by Victron Support from SDM630v2.py
self._dbusservice.add_path('/ProductId', 45069) # found on https://www.sascha-curth.de/projekte/005_Color_Control_GX.html#experiment - should be an ET340 Engerie Meter
self._dbusservice.add_path('/ProductId', productid)
self._dbusservice.add_path('/DeviceType', 345) # found on https://www.sascha-curth.de/projekte/005_Color_Control_GX.html#experiment - should be an ET340 Engerie Meter
self._dbusservice.add_path('/ProductName', productname)
self._dbusservice.add_path('/CustomName', productname)
self._dbusservice.add_path('/Latency', None)
self._dbusservice.add_path('/CustomName', customname)
self._dbusservice.add_path('/Latency', None)
self._dbusservice.add_path('/FirmwareVersion', 0.1)
self._dbusservice.add_path('/HardwareVersion', 0)
self._dbusservice.add_path('/Connected', 1)
self._dbusservice.add_path('/Role', 'grid')
self._dbusservice.add_path('/Role', role)
self._dbusservice.add_path('/Position', 0) # normaly only needed for pvinverter
self._dbusservice.add_path('/Serial', self._getShellySerial())
self._dbusservice.add_path('/UpdateIndex', 0)
Expand Down Expand Up @@ -133,7 +150,7 @@ def _update(self):
meter_data = self._getShellyData()

#send data to DBus
self._dbusservice['/Ac/Power'] = meter_data['total_power'] # positive: consumption, negative: feed into grid
self._dbusservice['/Ac/Power'] = meter_data['total_power']
self._dbusservice['/Ac/L1/Voltage'] = meter_data['emeters'][0]['voltage']
self._dbusservice['/Ac/L2/Voltage'] = meter_data['emeters'][1]['voltage']
self._dbusservice['/Ac/L3/Voltage'] = meter_data['emeters'][2]['voltage']
Expand Down Expand Up @@ -231,8 +248,6 @@ def main():

#start our main-service
pvac_output = DbusShelly3emService(
servicename='com.victronenergy.grid',
deviceinstance=40,
paths={
'/Ac/Energy/Forward': {'initial': 0, 'textformat': _kwh}, # energy bought from the grid
'/Ac/Energy/Reverse': {'initial': 0, 'textformat': _kwh}, # energy sold to the grid
Expand Down
18 changes: 10 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
DAEMON_NAME=${SCRIPT_DIR##*/}

# set permissions for script files
chmod a+x /data/dbus-shelly-3em-smartmeter/restart.sh
chmod 744 /data/dbus-shelly-3em-smartmeter/restart.sh
chmod a+x $SCRIPT_DIR/restart.sh
chmod 744 $SCRIPT_DIR/restart.sh

chmod a+x /data/dbus-shelly-3em-smartmeter/uninstall.sh
chmod 744 /data/dbus-shelly-3em-smartmeter/uninstall.sh
chmod a+x $SCRIPT_DIR/uninstall.sh
chmod 744 $SCRIPT_DIR/uninstall.sh

chmod a+x /data/dbus-shelly-3em-smartmeter/service/run
chmod 755 /data/dbus-shelly-3em-smartmeter/service/run
chmod a+x $SCRIPT_DIR/service/run
chmod 755 $SCRIPT_DIR/service/run



# create sym-link to run script in deamon
ln -s /data/dbus-shelly-3em-smartmeter/service /service/dbus-shelly-3em-smartmeter
ln -s $SCRIPT_DIR/service /service/$DAEMON_NAME



Expand All @@ -27,4 +29,4 @@ then
echo >> $filename
fi

grep -qxF '/data/dbus-shelly-3em-smartmeter/install.sh' $filename || echo '/data/dbus-shelly-3em-smartmeter/install.sh' >> $filename
grep -qxF '$SCRIPT_DIR/install.sh' $filename || echo '$SCRIPT_DIR/install.sh' >> $filename
3 changes: 2 additions & 1 deletion restart.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
DAEMON_NAME=${SCRIPT_DIR##*/}

kill $(pgrep -f "python $SCRIPT_DIR/dbus-shelly-3em-smartmeter.py")
kill $(pgrep -f "python $SCRIPT_DIR/$DAEMON_NAME")
10 changes: 6 additions & 4 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
DAEMON_NAME=${SCRIPT_DIR##*/}

rm /service/dbus-shelly-3em-smartmeter
kill $(pgrep -f 'supervise dbus-shelly-3em-smartmeter')
chmod a-x /data/dbus-shelly-3em-smartmeter/service/run
./restart.sh
rm /service/$DAEMON_NAME
kill $(pgrep -f 'supervise $DAEMON_NAME')
chmod a-x $SCRIPT_DIR/service/run
$SCRIPT_DIR/restart.sh