I calculate electricity consumption. Every hour there is a record of the energy meter status to the influxdb. With the ud rule of the current meter reading, it subtracts the meter reading from 1 hour ago. But instead of using energy in the last hour, I get the result in two hours. Where is the mistake?
rule "Energy by hour"
when
Time cron "0 0 * * * ?"
then
var hour = Licznik_Import.state as DecimalType - Licznik_Import.historicState(now.minusHours(1), "influxdb").state as DecimalType
postUpdate(LicznikLastH, hour)
end
No problem with starting every 1 hour.
I used the strategies from the official tutorial and there is a record for the action every 1 hour like this:
0 0 * * * ?
What does the additional sign “*” change?
If there is no record at exactly now-1 hours, (and there will not be), then by design historicState goes back in time to the next valid state. Each recorded state is assumed to stay valid until the next record in time.
You need to have a think about what you really want here - just the most recent record, perhaps?
If, say, you are recording data each hour, then historicState for (now - 1 minute) will return a record from between 1 minute and an hour ago. That record might be 2 minutes old, or 59 minutes old, etc.
rule "Energy by hour"
when
Time cron "0 0 * * * ?"
then
var hour = (Licznik_Import.state as DecimalType) - (Licznik_Import.historicState(now.minusHours(1), "influxdb").state as DecimalType)
postUpdate(LicznikLastH, hour)
end
Please consider to use the method instead of the action: