Avoid float calculation artifacts

This commit is contained in:
Odd Stråbø 2021-11-20 10:11:32 +01:00
parent b2f29ec263
commit 0cd5872fb6

View file

@ -5,6 +5,7 @@ import datetime
import struct
import sys
import time
from decimal import Decimal
from io import RawIOBase
from typing import Callable, Collection, Optional, cast
@ -403,15 +404,18 @@ if __name__ == "__main__":
if per_voltages(now):
data = try_read_parse(dev, 0x0100, 11, parse_battery_state)
if data:
data[DataName.CALCULATED_BATTERY_POWER] = data.get(
DataName.BATTERY_VOLTAGE, 0
) * data.get(DataName.BATTERY_CURRENT)
data[DataName.CALCULATED_PANEL_POWER] = data.get(
DataName.PANEL_VOLTAGE, 0
) * data.get(DataName.PANEL_CURRENT)
data[DataName.CALCULATED_LOAD_POWER] = data.get(
DataName.LOAD_VOLTAGE, 0
) * data.get(DataName.LOAD_CURRENT)
data[DataName.CALCULATED_BATTERY_POWER] = float(
Decimal(data.get(DataName.BATTERY_VOLTAGE, 0))
* Decimal(data.get(DataName.BATTERY_CURRENT, 0))
)
data[DataName.CALCULATED_PANEL_POWER] = float(
Decimal(data.get(DataName.PANEL_VOLTAGE, 0))
* Decimal(data.get(DataName.PANEL_CURRENT, 0))
)
data[DataName.CALCULATED_LOAD_POWER] = float(
Decimal(data.get(DataName.LOAD_VOLTAGE, 0))
* Decimal(data.get(DataName.LOAD_CURRENT, 0))
)
log(data)
for consumer in consumers:
consumer.write(data)