Custom energy monitoring entity

Hi everyone! I’m new here so I’m sorry if this is the wrong section of the Blog.
I’ve searched on the site and I wasn’t able to find an answer so I’m opening this new Topic…

I just bought a shelly EM with 2 power clamps in order to start taking advantage of the section “energy” in HA, at the moment the two clamps measure the energy from the grid and the total energy consumed by the house.
I also have a photovoltaic system that should be included but the shelly can’t measure DC and thus it is not immediate to integrate.

I was thinking of creating a custom template entity that simply measure the difference between the total consumption and the power from the grid in order to get the energy produced by the solar system.
I tryed a simple entity:

template:

  - sensor:

    - name: 'autoconsumo solare energia'

      unit_of_measurement: 'kWh'

      device_class: 'energy'

      state: >

          {% set consumo = states('sensor.2_power_meter_channel_1_energy')}

          {% set rete = states('sensor.2_power_meter_channel_2_energy')}

          {{consumo - rete}}

But it doesn’t work in the energy panel.

Can someone help me?

To be used in the energy panel you also have to add:

state_class: total_increasing

Also you need to convert your sensor states, that are always strings, into numbers so you can do the subtraction:

      state: >
          {% set consumo = states('sensor.2_power_meter_channel_1_energy')|float(0) %}
          {% set rete = states('sensor.2_power_meter_channel_2_energy')|float(0) %}
          {{consumo - rete}}

And you were missing the % before the } at the end of your templates.

1 Like

Works perfectly. Thank you very much!