Best practice for daily/weekly counter

I got a counter Item and want to sum up values per Day/Week.

I know that there are different solutions to achieve this.

I currently use a rule to set a Reset switch on the counter every night Like:

#counterrule.rule
// Triggers:
// - At 00:05

// context: reset-1
sendCommand(Resetswitch, ON)
Thread::sleep(500)
sendCommand(Resetswitch, OFF)
#influx.persist
Strategies {everyfivemin   : "0 */5 * * * ?"}
Items {Counter : strategy = everyfivemin}

Furthermore I use in Grafana a Query to get the daily Sum:

SELECT max("value") FROM "autogen"."Counter" WHERE time >= now() - 7d GROUP BY time(1d) fill(none)

It works but when the power fails at 0:05 the counter isn’t reset and the values would be wrong.
I’m currently planning to integrate such a counter (bundled with a Raspberry Pi) into a machine which is usually without power at night so I need a better solution for the Counting.

Any suggestions?
Let influx/grafana do the work and no resetting at all?
Modify the rule like “no reset since yesterday so reset when power comes on”?

There could be a better way however since we don’t see the whole rule it is hard to guess what that ‘better way’ could be… The switch itself can’t count :wink:

That is the whole rule.
The Counter is hardware based (KNX binary input with a reset switch).

Ah that makes sense.
Well, I think it all depends how that machine works as well how you store data.

This would be a more solid way maybe. Create a reset item and timestamp it upon reset. Then do a date comparison. Or just when startlevel has state X if the machine goes offline only once per day.

I don’t know anything about Influx but maybe it is possible to check what the last entry was date-wise. If it was yesterday (or earlier), reset and create new date entry, if it was today count = count + 1?

If every count is an entry in the database, I think Grafana should also have some functionality for that. I’ve just started getting the grips of Grafana so unable to comment on that so far but at least a mysql query could do that stuff. Google is your friend there :wink:

Create a reset item and timestamp it upon reset. Then do a date comparison. Or just when startlevel has state X if the machine goes offline only once per day.

You got an example for that rule?
That’s what i’m considering. Reset on startup but only if the last reset was more than one day ago.