diff --git a/plugin.py b/plugin.py index 9b2aa91..dc24f0f 100644 --- a/plugin.py +++ b/plugin.py @@ -9,7 +9,7 @@ # """ - + @@ -317,7 +317,7 @@ def onHeartbeat(self): inverter_values = self.inverter.read_all() except ConnectionException: inverter_values = None - Domoticz.Debug("ConnectionException") + Domoticz.Error("ConnectionException") else: if inverter_values: @@ -349,7 +349,12 @@ def onHeartbeat(self): Domoticz.Debug("-> looking up...") lookup_table = unit[Column.LOOKUP] - to_lookup = int(inverter_values[unit[Column.MODBUSNAME]]) + try: + to_lookup = int(inverter_values[unit[Column.MODBUSNAME]]) + except KeyError as e: + to_lookup = -1 + Domoticz.Error("missing data in modbus inverter_values: "+str(e)) + return #data is missing, no point to continue for this device if to_lookup >= 0 and to_lookup < len(lookup_table): value = lookup_table[to_lookup] @@ -361,26 +366,40 @@ def onHeartbeat(self): elif unit[Column.MATH] and Parameters["Mode4"] == "math_enabled": Domoticz.Debug("-> calculating...") m = unit[Column.MATH] - if unit[Column.MODBUSSCALE]: - m.update(inverter_values[unit[Column.MODBUSNAME]], inverter_values[unit[Column.MODBUSSCALE]]) - else: - m.update(inverter_values[unit[Column.MODBUSNAME]]) - - value = m.get() - + try: + if unit[Column.MODBUSSCALE]: + m.update(inverter_values[unit[Column.MODBUSNAME]], inverter_values[unit[Column.MODBUSSCALE]]) + else: + m.update(inverter_values[unit[Column.MODBUSNAME]]) + + value = m.get() + except KeyError as e: + value = "Key not found in inverter_values table: {}".format(inverter_values) + Domoticz.Error("missing data in modbus inverter_values: "+str(e)) + return + # When there is no math object then just store the latest value. # Some values from the inverter need to be scaled before they can be stored. elif unit[Column.MODBUSSCALE]: Domoticz.Debug("-> scaling...") # we need to do some calculation here - value = inverter_values[unit[Column.MODBUSNAME]] * (10 ** inverter_values[unit[Column.MODBUSSCALE]]) + try: + value = inverter_values[unit[Column.MODBUSNAME]] * (10 ** inverter_values[unit[Column.MODBUSSCALE]]) + except KeyError as e: + Domoticz.Error("missing data in modbus inverter_values: "+str(e)) + return # Some values require no action but storing in Domoticz. else: Domoticz.Debug("-> copying...") - value = inverter_values[unit[Column.MODBUSNAME]] + try: + value = inverter_values[unit[Column.MODBUSNAME]] + except KeyError as e: + value = "Key not found in inverter_values table: {}".format(inverter_values) + Domoticz.Error("missing data in modbus inverter_values: "+str(e)) + return Domoticz.Debug("value = {}".format(value)) @@ -452,11 +471,13 @@ def contactInverter(self): Domoticz.Log("Connection Exception when trying to contact: {}:{} Device Address: {}".format(Parameters["Address"], Parameters["Port"], Parameters["Mode3"])) Domoticz.Log("Retrying to communicate with inverter after: {}".format(self.retryafter)) + return else: if inverter_values: Domoticz.Log("Connection established with: {}:{} Device Address: {}".format(Parameters["Address"], Parameters["Port"], Parameters["Mode3"])) + Domoticz.Debug("inverter_values = '" +format(inverter_values)+"'") inverter_type = solaredge_modbus.sunspecDID(inverter_values["c_sunspec_did"]) Domoticz.Log("Inverter type: {}".format(inverter_type)) @@ -523,6 +544,7 @@ def contactInverter(self): else: Domoticz.Log("Connection established with: {}:{} Device Address: {}. BUT... inverter returned no information".format(Parameters["Address"], Parameters["Port"], Parameters["Mode3"])) Domoticz.Log("Retrying to communicate with inverter after: {}".format(self.retryafter)) + Domoticz.Debug("inverter_values = '" +format(inverter_values)+"'") else: Domoticz.Log("Retrying to communicate with inverter after: {}".format(self.retryafter))