Counting how many days elapsed in Utility Meter

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…

This is what I have so far…

- platform: template
  sensors:
    days_kw:
      value_template: '{{ ( as_timestamp(now()) - as_timestamp(states.sensor.watts_peak.attributes.last_reset ) ) | timestamp_custom("%j") | int -1 }}'
      friendly_name: 'Watts basement'
      unit_of_measurement: "Days"

I would appreciate any help.

See:

and

thanks, I’m reading the documentation and apparently it would be like this :

- platform: template
  sensors:
    elapsed_days_sensor:
      value_template: '{{ ( as_timestamp(now()) - as_timestamp(states.sensor.watts_peak.attributes.last_reset ) ) | timestamp_custom("%j") | int -1 }}'
      friendly_name: 'Watts basement'
      unit_of_measurement: "Days"
      entity_id: sensor.date

Is that correct?

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.

I would appreciate any help.

If you want to use timestamp_custom in this way, then you should pass it’s second parameter as false. But a simpler solution is:

value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.sensor.watts_peak.attributes.last_reset ))/(60*60*24))|int }}"
1 Like

Amazing… thanks!!!

Im trying to understand this:

/(60*60*24))

So is it going to reset every 60 days? The utility meter resets every month.

Okay, today the counter went back to zero instead of keep counting days. i’m running out of ideas…

basically I want to make a sensor that counts 60 days, and then resets to 0 every couple of months…

I really appreciate any help …

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 appreciate your help.

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?

ohh that’s a great idea!

okay do you think this would work?

utility_meter:
  power_yearly:
    source: sensor.energy_yearly
    cycle: yearly
-  alias: 'counter'
   trigger:
     - platform: template
       value_template: " {{ now().timetuple().tm_yday == 32 }}"
     - platform: template
       value_template: " {{ now().timetuple().tm_yday == 91 }}"
     - platform: template
       value_template: " {{ now().timetuple().tm_yday == 152 }}"
     - platform: template
       value_template: " {{ now().timetuple().tm_yday == 213 }}"
     - platform: template
       value_template: " {{ now().timetuple().tm_yday == 274 }}"
     - platform: template
       value_template: " {{ now().timetuple().tm_yday == 335 }}"
   action:
     - service: utility_meter.reset
       data: 
         entity_id:
         - utility_meter.power_yearly

sensor:


- platform: template
  sensors:
    days_kw:
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.power_yearly.attributes.last_reset ))/(60*60*24))|int }}"
      friendly_name: 'bimonthly'
      unit_of_measurement: "Days"

Template triggers that use now() generally will not work. You should define and use sensor.date in your triggers.

ok, how about this?

utility_meter:
  power_yearly:
    source: sensor.energy_yearly
    cycle: yearly
- platform: time_date
  display_options:
    - 'date'

- platform: template
  sensors:
    counting_days:
      friendly_name: "days"
      entity_id: sensor.time
      value_template: "{{ as_timestamp(now()) | timestamp_custom('%d/%m') }}"
-  alias: 'counter'
   trigger:
     - platform: template
       value_template: " {{ is_state('sensor.counting_days', '01/02') }}"
     - platform: template
       value_template: " {{ is_state('sensor.counting_days', '01/04') }}"
     - platform: template
       value_template: " {{ is_state('sensor.counting_days', '01/06') }}"
     - platform: template
       value_template: " {{ is_state('sensor.counting_days', '01/08') }}"
     - platform: template
       value_template: " {{ is_state('sensor.counting_days', '01/10') }}"
     - platform: template
       value_template: " {{ is_state('sensor.counting_days', '01/12') }}"
   action:
     - service: utility_meter.reset
       data: 
         entity_id:
         - utility_meter.power_yearly


- platform: template
  sensors:
    days_kw:
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.power_yearly.attributes.last_reset ))/(60*60*24))|int }}"
      friendly_name: 'bimonthly'
      unit_of_measurement: "Days"

First, the way you have the time_date sensor configured it will create sensor.date, not sensor.time. But that’s good.

Second, you don’t need the intermediary sensor.counting_days (unless you want it for some other use, too.)

Third, your trigger can be this simple:

trigger:
  platform: template
  value_template: >
    {{ states('sensor.date').split('-', 1)[1] in
       ('02-01', '04-01', '06-01', '08-01', '10-01', '12-01') }}

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.)

Thank you, it’s much better!

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.

value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.power_yearly.attributes.last_reset ))/(60*60*24))|int }}"

Am I understanding this correctly? btw where’s the typo?

Thanks for your help I really appreciate it.

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.

states.power_yearly.attributes.last_reset

This needs to be:

states.sensor.power_yearly.attributes.last_reset

or better yet:

state_attr('sensor.power_yearly', 'last_reset')

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?

Okay I changed the code to this:

value_template: " {{ ((as_timestamp(now()) - as_timestamp(state_attr('sensor.power_yearly', 'last_reset')))/(60*60*24))|int }}"

Please let me know if I’m making a mistake or something…

Thanks again for your input.

See the two links I provided in my first reply.

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:

  1. Add an entity into the template that changes when you want to template the update. E.g., use sensor.date (instead of now().)
  2. As with your counting_days template sensor, use the entity_id option to make it update when the specified entity/entities change.
  3. 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.

For sensor.days_kw, I think I’d do this:

value_template: "{{ ((as_timestamp(states('sensor.date') ~ ' 00:00:00') - as_timestamp(state_attr('sensor.power_yearly', 'last_reset')))/(60*60*24))|int }}"

Thank you!!! This is what I was looking for!

(as_timestamp(states('sensor.date') ~ ' 00:00:00'))

Like this:

- platform: template
  sensors:
    days_kw:
      value_template: "{{ ((as_timestamp(states('sensor.date') ~ ' 00:00:00') - as_timestamp(state_attr('sensor.power_yearly', 'last_reset')))/(60*60*24))|int }}"
      entity_id: sensor.date
      friendly_name: 'bimonthly'
      unit_of_measurement: "Days"

I tested the automation manually and it works, it’s updating last_reset to the current date.

Thanks again, I really appreciate your help :slight_smile: