Day counting sensor

Hello everyone,

I have this as a sensor, it counts the days since the last battery change. It gets the date from a date / time selection helper.

      value_template: '{{ (((as_timestamp(now())-states.input_datetime.last_battery.attributes.timestamp)) | int /60/1440) | round(0) }}'
      friendly_name: "Days since last battery change"
      entity_id: input_datetime.last_battery

It works and it counts the days since the date input. Problem is that I find I have to toggle the input date selector for the day count to be updated.

Is there something I can put in an automation so I don’t have to do that and the days count is automatically updated and displayed?


1 Like

Have you tried input number? You can have an automation increment it every day at a particular time.

How would that work with the date input?

But it’s just a number you want as output?
So instead of writing the date you changed the battery, reset the counter.

Agreed… you could set up automation to increment the count every day and reset the count when the battery changes

Thanks guys, I really wanted to note the input date as well and have it displayed on a lovelace card and have the number of days calculated off that.

Could I assign the value from the sensor I’ve set up to a number and then increment that? I have a few of these I’d need to setup. Just seems a messy way to go about it, would be better if I could just update directly off the date I set.


Just in case anyone is looking for a solution to this. I used the homeassistant.update_entitiy service, specified the appropriate template sensors as entity_ids and ran it via an overnight automation. All good.

Thanks to those that took the time to respond.