Hey guys Is it possible to count how many days elapsed after the Utility meter resets, and then keep counting until it reaches 60 days? After that, I’d like to reset the counter again to zero…
I want to take the “last reset” value and compare it against the actual date, and keep counting days until it’s 60 days and then reset the “elapsed days sensor” to zero. That would be a billing cycle.
Unix timestamps are in seconds. So when you subtract two, you get the number of seconds between them. So I divided that by the number of seconds in a day to convert the value into the number of days. I have no idea what causes states.sensor.watts_peak.attributes.last_reset to change. You didn’t say how that was being “reset”.
I’m trying to use the utility meter, It’s a sensor that resets every month.
I’d like to count every single day until it reaches to 60 days like it was my billing cycle, but I’m banging my head since the sensor updates every 30 days instead of 60, so I don’t know how to keep counting more than 30 days in a row.
I don’t use this integration, nor have I looked at its code. But I just briefly read through its doc page. Why don’t you set cycle to yearly, then have an automation that resets it after 60 days?
Fourth, your days_kw template sensor suffers from the same problem of using now(). As defined it will only update when sensor.power_yearly updates. (And you also have a typo.)
As soon as the automation is true, it would trigger utility_meter.reset and that would change last_reset: 2019-08-28T17:46:20.800015-05:00 to the current date and time. (sensor.power_yearly has the attribute last_reset)
Then this code will count days until the automation runs and resets the date again, which should be every 60 days.
It’s true that when sensor.power_yearly changes it would cause sensor.days_kw to update. However, then it won’t change again until sensor.power_yearly changes again. So, unless sensor.power_yearly changes often (at least its attributes), the template sensor will not change often. At the very least, the value of now() changing does not cause the template sensor to update.
When states.sensor.power_yearly.attributes.last_reset updates to the current date (due to the automation), it will make sensor.days_kw update to 0, since last_reset attributes will store the last date the sensor was reset. So (today - today / 60* 60* 24 ) = 0
The automation will reset sensor.power_yearly to the current date every 60 days, it will do so using utility_meter.reset, so the last_reset attribute will be updated.
So… do you think now() would cause any trouble? If so, how can I change the code to make it work properly?
Thanks I read the two links you provided, but how can I change the template to remove the now() and make it do the math if it’s a different format? I don’t know how to do that.
I’ve been using the counting_days sensor and it’s updating every day, I haven’t tried to trigger the automation manually to see if it works yet.
A template sensor will only update when one of the referenced entities changes. As I said, your days_kw template sensor, as currently written, will only update when sensor.power_yearly changes (either its “state” or one of its attributes.) If you want it to update at other times, then you need to do one of the following:
Add an entity into the template that changes when you want to template the update. E.g., use sensor.date (instead of now().)
As with your counting_days template sensor, use the entity_id option to make it update when the specified entity/entities change.
Use another automation that calls the homeassistant.update_entity service to force it to update.
It’s actually updating every minute, because you’re using entity_id: sensor.time, and sensor.time changes every minute.