I would like to have a sensor that stores the number of days that a device is being used regardless the number of times that is switched in the same day. In other words, whenever the device is used it should increment the total days by 1. I was able to create an automation using the counter.increment service but the challenge is to avoid that the automation run just only once a day if for some reason the device was switched on several times in the same day. Any contribution is more than welcome.
I would create a helper datetime.
When the device is used the datetime is set to now() then increment counter.
As condition you use this datetime in a template condition.
I think last triggered is cleared when you reboot HA.
So if you use the device, reboot HA then use the device again that will give you two counts, if I’m correct that the last triggered is cleared.
Datetimes is persistent over a restart on the other hand.
It definitely works! The only thing is that you need to run the automation the first time manually otherwise the condition fail since there is no last triggered date.
If you’re interested there’s another way to do it. The question of running an automation just once a day was posted last February. It also had a second requirement where the time threshold wasn’t simply at 00:00 but at 05:00.
For this application, which only needs to ensure the automation executes once a day (time threshold is 00:00), the template is simpler:
Thank you @123 . For some reason I didn’t found that topic from February, anyway it is always good to have alternative solution. I have being reading so many HA topics in the last months and allow me to share that you are one of the greatest contributors of this fantastic community.