Create absolute energy sensor from daily energy sensor

Hi,

My Wemo Switch reports energy consumption on a daily basis: i.e it is increasing over the day and reset to 0 at midnight.
I would like to create an absolute energy sensor that is increasing continuously over the days, avoiding the midnight disruption.

What is the best/cleanest way to do that?

Thx.

Use an automation triggered just before midnight that adds the total for the day to an input number.

  trigger:
    - platform: time
      at: '23:59:50'
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.cumulative_energy
        value: "{{ states('input_number.cumulative_energy')|float + states('sensor.total_energy_today')|float }}"

And if you want, a script to reset it:

reset_cumulative_energy:
  sequence:
    - service: input_number.set_value
      data:
        entity_id: input_number.cumulative_energy
        value: 0

Maybe not the best, but you could create in input_number and an automation with a numeric_state trigger for your sensor. If triggered and if the new state (trigger.to_state.state) is lower than the old state (trigger.from_state.state), then add the from_state to the input_number.

Create an additional template sensor, that sums your input_number and your sensor, as it will be the total before midnight and the actual since midnight.

Hi Tom & Chris,
Yes, this is roughly what I had in mind.
Here is my result:

input_number:
  wemo_yesterday_max_energy:
    name: Wemo Yesterday Max Energy
    min: 0.0
    max: 1000000.0
    icon: mdi:flash

automation:
  - alias: Store Energie Max
    description: "Wemo: Store Energie Max at midnight reset"
    trigger:
      - platform: state
        entity_id: switch.wemo
    condition: "{{ float(trigger.to_state.attributes.today_energy_kwh) < float(trigger.from_state.attributes.today_energy_kwh) }}"
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.wemo_yesterday_max_energy
          value: "{{ float(states('input_number.wemo_yesterday_max_energy')) + trigger.from_state.attributes.today_energy_kwh }}"

sensor:
  - platform: template
    sensors:
      wemo_energy:
        device_class: energy
        unit_of_measurement: kWh
        value_template: "{{ float(states('input_number.wemo_yesterday_max_energy')) + float(state_attr('switch.wemo', 'today_energy_kwh')) }}"

It should work, except maybe a little glitch during the transition, I will check this night.

Any other “lighter” idea?

The glitch is present:


But it’s OK: I will add a +1min offset in apexcharts-card series to present correct hourly and daily bar graph.

Thanks for sharing. Happy to find this. I had the same idea with the yesterday-helper but during thinking about the idea of the trigger states, I have now tried to skip the yesterday-helper stuff and reduced the helper, automation and sensor to only one template sensor:

  - trigger:
      - platform: state
        entity_id: sensor.klimaanlage_wohnzimmer_total_energy_consumption_today
    sensor:
      - name: Klimaanlage Wohnzimmer ENERGY_COUNTER 2
        unique_id: unique_id_template_sensor_klimaanlage_wohnzimmer_energy_counter_2
        state: >
          {% if is_number(trigger.from_state.state) and (float(trigger.to_state.state,default=0) > float(trigger.from_state.state,default=0)) %}
            {{ ( states("sensor.klimaanlage_wohnzimmer_energy_counter_2")|float(default=0)
            + float(trigger.to_state.state,default=0) 
            - float(trigger.from_state.state,default=0) )|round(1) }}
          {% else %}
            {{ states("sensor.klimaanlage_wohnzimmer_energy_counter_2")|float(default=0)|round(1) }}
          {% endif %}
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing

Let’s see, if this is working or if there are any cases, where it will no work.