Template Sensor for Grid Consumption

Hey,

So, I have a Shelly EM measuring the total consumption of the house on one channel and the solar production on another.

In order to determine what I’m pulling from the grid, I need to subtract what the house is consuming from what the panels are producing… BUT I essentially need it to report 0 if the value is positive and, if negative, report the negative value as positive.

Example 1: I’m producing 2kWh and the house is using 2.5kWh. I’m therefore using 2kWh-2.5kWh = -0.5kWh (I need this to report as positive 0.5kWh).

Example 2: I’m producing 2kWh and the house is using 0.5kWh. I’m therefore using 2kWh-0.5kWh = 1.5kWh (I need this to report as 0).

The code I have at this time is:

template:
  - sensor:
      - name: Energia Rede
        state: "{{ (states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float + states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float - states('ssensor.shellyem_a4e57cbaafa6_channel_1_energy')|float)}}"
        unit_of_measurement: kWh  
        device_class: energy
        state_class: measurement

Can someone help me tweak it to achieve what I need?

Thanks!

I’ve tried this but it doesn’t work. Hopefuly it should be more illustrative of what I want.

template:
  - sensor:
      - name: Energia Rede
        state: >
          {% if (states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float + states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float - states('sensor.shellyem_a4e57cbaafa6_channel_1_energy')|float)) | int > 0 %}
            0
          {% else %}
            {{ (states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float + states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float - states('sensor.shellyem_a4e57cbaafa6_channel_1_energy')|float)))*-1 %}}
        unit_of_measurement: kWh  
        device_class: energy
        state_class: measurement
        state: >-
          {{ -([(states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float(0) +
                 states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float(0) -
                 states('ssensor.shellyem_a4e57cbaafa6_channel_1_energy')|float(0)),
                 0]|min) }}

I think that does what you want: takes the minimum value of 0 and your calculation, and report -that.

Thanks for your reply.

However, it’s reporting the negative number instead of 0.

Yeah, I noticed it was the wrong way around and edited it.

To clarify:

My house is currently using 1.1kWh and my panels are producing 1.3kWh. Your code is reporting -3.21Wh.

Can you clarify what the three sensors are and how they relate to the calculation? Given the screenshot I provided above, you should be able to work out what you need to do with them to make it work.

After your edit, it reports 0 no matter what. I think you missed the part where you have to turn a negative number into a positive.

Channel 2 Energy is the solar panels.
Channel 1 Energy is house usage.
Channel 1 Energy Returned is the return to grid.

There was a typo on my original post that you then copied to the code (“ssensor”). I’ve corrected that but that wasn’t the issue.

So to be clear, you want to:

  • calculate (solar panels + returned to grid - house usage), let’s call it x
  • if x is positive, report 0
  • if x is negative, report -x

Yes? If so, please paste this into Developer Tools / Templates and post a screenshot:

{% set solar = states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float(0) %}
{% set returned = states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float(0) %}
{% set house = states('sensor.shellyem_a4e57cbaafa6_channel_1_energy')|float(0) %}
{% set x = solar + returned - house %}

x is {{ solar }} + {{ returned }} - {{ house }} = {{ x }}

result is {{ -([x,0]|min) }}

Ok, it’s working and the issue is the delay with which Shelly calculates energy consumption.

Thanks!

1 Like

Now I’m facing a new issue and since you’re so kind to help, I misght aswell ask :smiley:

This new sensor I created - and yeah, I’ve restarted - won’t show up as a choice for “Grid Consumption” on the Energy dashboard. Any idea why?

No idea if this is the reason, but my source energy sensor has state_class: total.

Tried total and total_increasing but still nothing.

Nevermind, I had fucked up the formatting for the unit_of_measurement during our troubleshooting.

Thanks again!