Difference in two sensors

Hi there!

I’m trying to make a template sensor that displays the difference between two other sensors but I’m terrible with making templates so I need your help :slight_smile:

So I want to determine what the power usage is of my house using a sensor giving me the solar power and a sensor giving me the usage from the grid.

The reason I’m asking for a difference between the two and not just calculate one minus the other is that it can give a negative number if solar power is higher than the usage from the grid.

What I got so far from searching on the internet/forum is the below code:

value_template: "{{ states('sensor.zonnepanelen_meter_kw') | float > states('sensor.verbruik_net_kw') | float  }}"

But this gives me a True or False, not a number.

Any help is appreciated!

Darryl

> is the symbol for checking to see if the left side is greater than the right side

If you wanted to add the two, you’d use +, and to subtract you use -, and if you want the value regardless of the sign you want the abs filter.

value_template: "{{ (states('sensor.zonnepanelen_meter_kw') | float - states('sensor.verbruik_net_kw') | float) | abs }}"

Thanks for helping out! I’m just not getting the correct result. So I’ve 3 sensors, two of them from my smart meter between my home and the grid, so 1 sensor for usage of the grid and 1 sensor supplying to the grid. Then one more sensor for total power delivered from the solar panels to the house.

So, a moment shot:
Sensor 1: 0,0 kW (no usage from grid)
Sensor 2: 1,3 kW (supplying to the grid)
Sensor 3: 1,74 kW (production from solar panels)

This means my house is using 0,44 kW.

What I got so far:

value_template: "{{ (states('sensor.sensor1') | float + states('sensor.sensor2') | float - states('sensor.sensor3') | float) | abs }}"

The response from above code is: 1,3 kW, I don’t know why.

When I fill in the formula it says: 0,0 + 1,3 - 1,74 = -0,44 (with abs filter it should be a positive 0,44)

I don’t know what I’m doing wrong here.

Try the following in Developer Tools → Templates

{{ states('sensor.sensor1') | float }}
{{ states('sensor.sensor2') | float }}
{{ states('sensor.sensor3') | float }}
{{ (states('sensor.sensor1') | float + states('sensor.sensor2') | float - states('sensor.sensor3') | float) }}

I found a small typo in one of the sensors in the code, my apologies.

Below code is working in my case:

value_template: "{{ (states('sensor.sensor1') | float + states('sensor.sensor2') | float - states('sensor.sensor3') | float) | abs }}"

Thanks for the help anyway!

2 Likes

I have a similar problem, I first had the energy used from the net from the 3 fases added with a start value so that is equaled my analog meter (commented out). I then changed the template so that I can subtract the energy sent to the net by my solar panels because the analog meter can count backwards. But now i get an Unknown value on lovelace.

Any idea what I do wrong?

template:
  - sensor:
      - name: "Energy Total"
        unique_id: energy_total
        state: >-
          {{ '%0.1f' | format(states('sensor.shelly_3em_channel_a_energy') | float + 
                              states('sensor.shelly_3em_channel_b_energy') | float + 
                              states('sensor.shelly_3em_channel_c_energy') | float -
                              states('sensor.shelly_3em_channel_b_energy_returned') + 1213)
          }}
        #          {{
        #            [ states('sensor.shelly_3em_channel_a_energy'),
        #              states('sensor.shelly_3em_channel_b_energy'),
        #              states('sensor.shelly_3em_channel_c_energy'),
        #              1213,] | map('float') | sum
        #          }}
        availability: >-
          {{ '%0.1f' | format(states('sensor.shelly_3em_channel_a_energy') | float + 
                              states('sensor.shelly_3em_channel_b_energy') | float + 
                              states('sensor.shelly_3em_channel_c_energy') | float -
                              states('sensor.shelly_3em_channel_b_energy_returned') + 1213)
          }}
#          {{
#            [ states('sensor.shelly_3em_channel_a_energy'),
#              states('sensor.shelly_3em_channel_b_energy'),
#              states('sensor.shelly_3em_channel_c_energy'),
#              1213,] | map('is_number') | min
#          }}
        unit_of_measurement: kWh
        device_class: energy
        state_class: total