The problem is that at 00:00 and at some more hours the log shows crazy high numbers I might have done something wrong (obviously) and I deleted and recreated this type of sensor a couple of times to get rid of those values, because there is no way to reset the counter.
I try to take all measures to prevent uploading the wrong data:
The energy is in Watthours so kWh's are avoided in my calculation
Counter can only increase: I have a check built in that only higher new data is accepted
Now I try to read the sensor by this script (which all works fine with use of a 'custom sensor')
Code: Select all
if tonumber(otherdevices["Solar battery usage"]) == nil then
old_battery_energy_usage_value = 0
print (otherdevices["Solar battery usage"])
print ("old battery energy usage value was NIL. Either it is a first start of the script (new virtual sensor), or something is wrong")
else
--Download the previous value of the solar battery energy usage in Wh
old_battery_energy_usage_value = tonumber(otherdevices_svalues["Solar battery usage"])
end
print ("Old value of the solar battery energy usage is " .. old_battery_energy_usage_value .. "Wh")
I write the data to the counter as follows:
Code: Select all
if new_battery_energy_usage_value > old_battery_energy_usage_value then
commandArray['UpdateDevice'] = battery_energy_usage_idx .. '|0|' .. new_battery_energy_usage_value
else
print ("Something went wrong, the new solar battery usage value of "..new_battery_energy_usage_value.." is smaller than the previous value of "..old_battery_energy_usage_value)
end