Template sensors that shouldn't be updated on reboot - energy consumption

Hello All!

I have 3 template sensors for calculating my energy consumption / solar production that are working aboslutely OK except when I reboot my HA when they spike. This causes the utility meters associated to also spike and mess everything up. What can I do?

This is what I have under sensor / platform template:

energy_consumed:
        value_template: "{{ ((states('sensor.energy_consumed_phase_1') | float + states('sensor.energy_consumed_phase_2') | float + states('sensor.energy_consumed_phase_3') | float) /1000) | round(1) }}"
        unit_of_measurement: "kWh"
        device_class: power
      energy_returned:
        value_template: "{{ ((states('sensor.energy_returned_phase_1') | float + states('sensor.energy_returned_phase_1') | float + states('sensor.energy_returned_phase_1') | float) /1000) | round(1) }}"
        unit_of_measurement: "kWh"
        device_class: power
      production_difference:
        value_template: "{{ (states('sensor.energy_returned') | float - states('sensor.energy_consumed') | float) | round(1) }}"
        unit_of_measurement: "kWh"
        device_class: power

I tried to recreate one of those sensor in the “new way” and only trigger it every 5 minutes but it doesn’t seem to work:

template:
  - trigger:
      - platform: time_pattern
        minutes: 5
    sensor:
      - name: energy_consumed_2
        state: "{{ ((states('sensor.energy_consumed_phase_1') | float + states('sensor.energy_consumed_phase_2') | float + states('sensor.energy_consumed_phase_3') | float) /1000) | round(1) }}"
        unit_of_measurement: "kWh"
        device_class: power

What am I doing wrong? How can I solve this issue?

Thanks!

1 Like

Not sure on the first question but to get time_pattern to trigger every 5 minutes use:

  - trigger:
      - platform: time_pattern
        minutes: "/5"

Also, I think you need to use device_class: energy if its in kWh ?
Power is in W or kW

1 Like

You should add availability templates to prevent the glitching:

energy_consumed:
  value_template: "{{ ((states('sensor.energy_consumed_phase_1') | float + states('sensor.energy_consumed_phase_2') | float + states('sensor.energy_consumed_phase_3') | float) /1000) | round(1) }}"
  availability_template: >
    {{ not ( is_state('sensor.energy_consumed_phase_1', 'unavailable') or
             is_state('sensor.energy_consumed_phase_2', 'unavailable') or
             is_state('sensor.energy_consumed_phase_3', 'unavailable') ) }}
  unit_of_measurement: "kWh"
  device_class: power
4 Likes