Skip to content

Commit

Permalink
openDTU v24.2.12 breaking change - code fix (#145)
Browse files Browse the repository at this point in the history
* openDTU v24.2.12 breaking change - code fix

* adapt json parsing, test with old and new firmware

* delete file, not needed

* remove whitspace

* rework based on comments

* remove whitespace
  • Loading branch information
henne49 authored Feb 19, 2024
1 parent 9e6ae7e commit f3901e0
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 19 deletions.
48 changes: 29 additions & 19 deletions dbus_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ def check_and_enrich_ahoy_data(self, meter_data):
# Check for Attribute (inverter)
if (self._servicename == "com.victronenergy.inverter" and
not "fld_names" in meter_data):
raise ValueError("Response from OpenDTU does not contain fld_names in data")
raise ValueError("Response from ahoy does not contain fld_names in data")
# Check for an additonal Attribute
if not "ch0_fld_names" in meter_data:
raise ValueError("Response from OpenDTU does not contain ch0_fld_names data")
raise ValueError("Response from ahoy does not contain ch0_fld_names data")
# not needed: meter_data["record"] = self.fetch_ahoy_record_data()

# add the field "inverter" to meter_data:
Expand All @@ -423,20 +423,19 @@ def check_and_enrich_ahoy_data(self, meter_data):
def check_opendtu_data(self, meter_data):
''' Check if OpenDTU data has the right format'''
# Check for OpenDTU Version
if not "AC" in meter_data["inverters"][self.pvinverternumber]:
if not "serial" in meter_data["inverters"][self.pvinverternumber]:
raise ValueError("You do not have the latest OpenDTU Version to run this script,"
"please upgrade your OpenDTU to at least version 4.4.3")
# Check for Attribute (inverter)
if (self._servicename == "com.victronenergy.inverter" and
not "DC" in meter_data["inverters"][self.pvinverternumber]):
raise ValueError("Response from OpenDTU does not contain DC data")
# Check for another Attribute
if not "Voltage" in meter_data["inverters"][self.pvinverternumber]["AC"]["0"]:
raise ValueError("Response from OpenDTU does not contain Voltage data")

def fetch_opendtu_inverter_data(self, inverter_serial):
'''Fetch inverter date from OpenDTU device for one interter'''
iv_url = self._get_status_url() + "?inv=" + inverter_serial
return self.fetch_url(iv_url)

def fetch_ahoy_iv_data(self, inverter_number):
'''Fetch inverter date from Ahoy device for one interter'''
iv_url = self.get_ahoy_base_url() + "/inverter/id/" + str(inverter_number)
logging.debug(f"Inverter URL: {iv_url}")
return self.fetch_url(iv_url)

def fetch_ahoy_record_data(self):
Expand Down Expand Up @@ -601,15 +600,26 @@ def get_values_for_inverter(self):
current = get_ahoy_field_by_name(meter_data, self.pvinverternumber, "I_AC")

elif self.dtuvariant == constants.DTUVARIANT_OPENDTU:
root_meter_data = meter_data["inverters"][self.pvinverternumber]
# OpenDTU v24.2.12 breaking API changes 2024-02-19
if "AC" in meter_data["inverters"][self.pvinverternumber]:
root_meter_data = meter_data["inverters"][self.pvinverternumber]
firmware_v24_2_12_or_newer=True
else:
inverter_serial = meter_data["inverters"][self.pvinverternumber]["serial"]
logging.info(f"Inverter #{self.pvinverternumber} Serial: {inverter_serial}")
root_meter_data = self.fetch_opendtu_inverter_data(inverter_serial)["inverters"][0]
logging.debug(f"{root_meter_data}")
firmware_v24_2_12_or_newer=False

producing = is_true(root_meter_data["producing"])
power = (root_meter_data["AC"]["0"]["Power"]["v"]
if producing
else 0)
field_inv = "AC" if firmware_v24_2_12_or_newer else "INV"
if self.useyieldday:
pvyield = root_meter_data["AC"]["0"]["YieldDay"]["v"] / 1000
pvyield = root_meter_data[field_inv]["0"]["YieldDay"]["v"] / 1000
else:
pvyield = root_meter_data["AC"]["0"]["YieldTotal"]["v"]
pvyield = root_meter_data[field_inv]["0"]["YieldTotal"]["v"]
voltage = root_meter_data["AC"]["0"]["Voltage"]["v"]
dc_voltage = root_meter_data["DC"]["0"]["Voltage"]["v"]
current = (root_meter_data["AC"]["0"]["Current"]["v"]
Expand Down Expand Up @@ -646,7 +656,7 @@ def set_dbus_values(self):
powerthird = power/3

#Single Phase Voltage = (3-Phase Voltage) / (sqrt(3))
# This formula assumes that the three-phase voltage is balanced and that
# This formula assumes that the three-phase voltage is balanced and that
# the phase angles are 120 degrees apart
# sqrt(3) = 1.73205080757 <-- So we do not need to include Math Library
singlePhaseVoltage = voltage / 1.73205080757
Expand All @@ -655,14 +665,14 @@ def set_dbus_values(self):
self._dbusservice["/Ac/Power"] = power

realCurrent = power / 3 / singlePhaseVoltage
self._dbusservice["/Ac/L1/Voltage"] = singlePhaseVoltage

self._dbusservice["/Ac/L1/Voltage"] = singlePhaseVoltage
self._dbusservice["/Ac/L1/Current"] = realCurrent
self._dbusservice["/Ac/L1/Power"] = powerthird
self._dbusservice["/Ac/L2/Voltage"] = singlePhaseVoltage
self._dbusservice["/Ac/L2/Voltage"] = singlePhaseVoltage
self._dbusservice["/Ac/L2/Current"] = realCurrent
self._dbusservice["/Ac/L2/Power"] = powerthird
self._dbusservice["/Ac/L3/Voltage"] = singlePhaseVoltage
self._dbusservice["/Ac/L3/Voltage"] = singlePhaseVoltage
self._dbusservice["/Ac/L3/Current"] = realCurrent
self._dbusservice["/Ac/L3/Power"] = powerthird
self._dbusservice["/Ac/Power"] = power
Expand All @@ -672,7 +682,7 @@ def set_dbus_values(self):
self._dbusservice["/Ac/L2/Energy/Forward"] = pvyield / 3
self._dbusservice["/Ac/L3/Energy/Forward"] = pvyield / 3
self._dbusservice["/Ac/Energy/Forward"] = pvyield

else:
pre = "/Ac/" + self.pvinverterphase
self._dbusservice[pre + "/Voltage"] = voltage
Expand Down
163 changes: 163 additions & 0 deletions docs/opendtu_v24.2.12_inverter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"hints": {
"default_password": true,
"radio_problem": false,
"time_sync": false
},
"inverters": [
{
"AC": {
"0": {
"Current": {
"d": 2,
"u": "A",
"v": 0.050000001
},
"Frequency": {
"d": 2,
"u": "Hz",
"v": 49.97999954
},
"Power": {
"d": 1,
"u": "W",
"v": 12.10000038
},
"PowerFactor": {
"d": 3,
"u": "",
"v": 1
},
"ReactivePower": {
"d": 1,
"u": "var",
"v": 0
},
"Voltage": {
"d": 1,
"u": "V",
"v": 226.3999939
}
}
},
"data_age": 49,
"DC": {
"0": {
"Current": {
"d": 2,
"u": "A",
"v": 0.400000006
},
"name": {
"u": ""
},
"Power": {
"d": 1,
"u": "W",
"v": 12.60000038
},
"Voltage": {
"d": 1,
"u": "V",
"v": 31.29999924
},
"YieldDay": {
"d": 0,
"u": "Wh",
"v": 4
},
"YieldTotal": {
"d": 3,
"u": "kWh",
"v": 0.004
}
},
"1": {
"Current": {
"d": 2,
"u": "A",
"v": 0.01
},
"name": {
"u": ""
},
"Power": {
"d": 1,
"u": "W",
"v": 0
},
"Voltage": {
"d": 1,
"u": "V",
"v": 0.5
},
"YieldDay": {
"d": 0,
"u": "Wh",
"v": 0
},
"YieldTotal": {
"d": 3,
"u": "kWh",
"v": 0
}
}
},
"events": 12,
"INV": {
"0": {
"Efficiency": {
"d": 3,
"u": "%",
"v": 96.03174591
},
"Power DC": {
"d": 1,
"u": "W",
"v": 12.60000038
},
"Temperature": {
"d": 1,
"u": "°C",
"v": 18.5
},
"YieldDay": {
"d": 0,
"u": "Wh",
"v": 4
},
"YieldTotal": {
"d": 3,
"u": "kWh",
"v": 0.004
}
}
},
"limit_absolute": 600,
"limit_relative": 100,
"name": "HM-600",
"order": 0,
"poll_enabled": true,
"producing": true,
"reachable": false,
"serial": "114182000000"
}
],
"total": {
"Power": {
"d": 1,
"u": "W",
"v": 12.10000038
},
"YieldDay": {
"d": 0,
"u": "Wh",
"v": 4
},
"YieldTotal": {
"d": 3,
"u": "kWh",
"v": 0.004
}
}
}
48 changes: 48 additions & 0 deletions docs/opendtu_v24.2.12_livedata_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"hints": {
"default_password": true,
"radio_problem": false,
"time_sync": false
},
"inverters": [
{
"data_age": 5,
"limit_absolute": 600,
"limit_relative": 100,
"name": "HM-600",
"order": 0,
"poll_enabled": true,
"producing": true,
"reachable": true,
"serial": "114182940773"
},
{
"data_age": 8973,
"limit_absolute": -1,
"limit_relative": 0,
"name": "Test",
"order": 0,
"poll_enabled": true,
"producing": false,
"reachable": false,
"serial": "114182000001"
}
],
"total": {
"Power": {
"d": 1,
"u": "W",
"v": 12.39999962
},
"YieldDay": {
"d": 0,
"u": "Wh",
"v": 30
},
"YieldTotal": {
"d": 3,
"u": "kWh",
"v": 0.029999999
}
}
}

0 comments on commit f3901e0

Please sign in to comment.