Template to count daily usage from cumulative energy sensor

Tags: #<Tag:0x00007f3f158bb0e0>

I’ve got a power socket that reports it’s total lifetime power consumption. Right now it’s on about 9000 watts. What I would like to do is see how much power the socket uses each day. I have been trying to make a template sensor that tracks the daily increase. So by the end of today the socket should report 9500 watts. I need the template to show 500 watts. Then if tomorrow the socket reports 9700 watts, the template shows 200 watts. Is this possible? I can’t see how the statistics component can do this.
Thanks!

I haven’t used the statistics component. It might be useful here.

But, you could do it with an input_number, an automation, and a template sensor. E.g., have an automation run at midnight every day. It simply records the current value to the input_number. Then the template sensor reports the current value minus the value of the input_number.

Let me know if you’d like a concrete example.

Have a look at this

Hi, I use the MQTT based solution as follows. If anyone has a better way of storing the daily (periodic usage variable) i’d be keen to learn more.

This gives me cumulative energy usage, daily energy usage and cost of the daily energy. So cool. I have something similar for the water meter.

(Note: the comments may be a little off)

#Pull hex energy delivered from API, convert to Energy delivered
  - platform: template
    sensors:
      energy_cum:
        value_template: '{{ (states("sensor.energy_delivered") | int(states("sensor.energy_delivered"), 16 )  * 1.0) | round(2) }}'
        friendly_name: Energy Delivered
        unit_of_measurement: ''
# Create a summary of the prior day energy delivered via JSON
  - platform: mqtt
    name: "energy_cum_prior"
    state_topic: "power/meter/delivered"
    value_template: "{{ value_json.energy_day_1 }}"
    unit_of_measurement: "Watt"
  - platform: template
    sensors:
      energy_today:
        value_template: '{%- if not (is_state("sensor.energy_cum","unknown") or is_state("sensor.energy_cum_prior","unknown") )-%}  {{ ((states.sensor.energy_cum.state | float) - (states.sensor.energy_cum_prior.state | float)) | max (0) | round(1) }} {%- endif -%}' ## ensure calc is no lower than zero!
        unit_of_measurement: 'Watt'
        friendly_name: "Energy Today"

#Daily energy use in dollars
  - platform: template
    sensors:
      energy_dollars_today:
        value_template: '{{ ((states.sensor.energy_today.state | int | multiply(0.001)) * (states.sensor.energy_price.state) | int | multiply(0.01)) | round(2) }}'
        friendly_name: "Energy Dollars Today"
#automation to pubish the energy usage at midnight each day
automation:    
- alias: 'record cumulative energy use every 1 day'
  initial_state: 'on'
  trigger:
    - platform: time
      at: '00:00:01'
  action:
    - service: mqtt.publish
      data_template:
        topic: 'power/meter/delivered'
        retain: true
        payload: '{"energy_day_1":"{{states.sensor.energy_cum.state}}"}'

The statistics sensor will probably only show you a moving total for the the last 24 hours. If that is what you need then it’s ok.

If you need a running total for the day from midnight, I would use 2 sensors. The first is a template sensor whose value is determined daily at ‘0:00:00’ and the second a template sensor that computes the difference between the cum sensor and the day start sensor.

Try this - store daily cumulative close and calculate daily usage using MQTT

Awesome! This sounds like the best solution for me. How would I create a template sensor that inherits the another sensor’s value at 0:00:00? I think I can figure out the second template, but if you already have it I’d love to see it written out.
Thank you

I don’t have one at disposal, but will try to config one for you in a few days (if you’re patient :wink:).

That would awesome, thanks so much!

I am a little lazy…

Perhaps this thread has a solution for you.

1 Like