Instantaneous Demand not calculated correctly
Created by: danstreeter
The values provided over MQTT for Instantaneous Demand (Node 0702.04.00
in payload) is returned as a signed 24 bit integer in hex... (see https://zigbeealliance.org/wp-content/uploads/2019/12/docs-07-5356-19-0zse-zigbee-smart-energy-profile-specification.pdf page 248, table on line 6028, first row) when this value is represented as a negative value (I am generating more than I am consuming) the current function in https://github.com/unlobito/ha-hildebrandglow/blob/mqtt/custom_components/hildebrandglow/mqttpayload.py#L160 calculates this incorrectly and you get a widly high value intead of a negative value.
Scenario
I'm curently feeding the grid with 785
watts - so the Glow app reads -785
, the values coming in from the data source via MQTT is FFFFFCEF
, which is being calculated as an unsigned int as 4294966511
(Clearly not what i'm using! haha)
The fix for this is to implement a two's compliment conversion of this number to get the signed integer out the back.
Forgive me, my mathmatical knowledge on this is 'literally learnt about it this morning' - so i'm probably saying it all wrong.
But
hex FFFFFCEF
== binary 11111111111111111111110011101111
== -785
decimal from signed 2's complement.
I think this SO link may provide some ways to do it, but will need testing obviously: https://stackoverflow.com/a/6728042/5889983
Cant do it myself as on work time until this 1730 GMT+1, but could possibly have a look this evening when off work.