Helper: Subtract/Add/divide/multiply two sensor values

I think it would be very helpful to have a general formula calculation. For example,

f = (a + b - c) * sqrt(d)

I don’t know if it’s possible though…

3 Likes

Oh the template sensor works!

1 Like

after having setup a template, it would still be very nice to have it as a helper. if this were implemented I would throw away my template for the helper system.

as long as it has multiple + and - like: a+b-c

1 Like

Please keep in mind this is an open source project. There are amazing people who have already been working on this feature.

This PR is one of those examples Add subtract in Group sensor by gjohansson-ST · Pull Request #95164 · home-assistant/core · GitHub

Looks like that PR was “friendly-declined” and closed without merging.

1 Like

sensor groups exist and you ahve the following options:

sum for addition
product for multiplication

division and subtraction don’t make sense with multiple entities because the order would matter and HA doesn’t maintain an order under the hood for lists.

This add/subtract helper would be ideal for measuring the power usage of my house. I have a sensor of total power consumption and a sensor of power production of my solar panels. However the solar panels merge with the power grid in my house prior to the point were the consumption is measured. So I always get my power consumption were the solar production is substracted. In the amazing energy-tab I can see the hourly results, but not real time. For my buggy neo coolcam power plugs i use the Rietman integration function helper and it works like a charm, so I assume that a simple add/subtract function can’t be that difficult, but i have to admit that I’m not a programmer and I’m very thankful for all the work that has been put in HA

5 Likes

I am trying to create a delta sensor of multiple sensor. I have an Emporia meter with a Balance sensor that I would like to subscract 1 zigbee sensor value:

Energy delta

This seems to work but I get the negative values in the energy dashboard on a per device item:

I got - more or less - the same issue when I try to add to values…

I got two sets of solar panels : one in front of the house, one in the back.
The entity of the panels in front is sensor.zonnepanelen_vooraan_actueel_vermogen
The entity of the panels in the back is sensor.zonnepanelen_achteraan_actueel_vermogen

How can I add these 2 values, and create a new value “sensor.zonnepanelen_totaal_vermogen”? that I can display on the home panel

Hi Kris, isn’t that explained in this post?

Found it! for use of total power usage in my house:

  1. Create Helper for “Combine the state of several sensors” , use sensor of power production for your solar panels and sensor of your Net (eg DSMR) power production. Use the ‘Statistical Range’. This wil calculate the own usage of your solar energy.
  2. Create another Helper for “Combine the state of several sensors”, use the above sensor from 1 and your Net power consumption. Use the ‘Sum’ function.

However for other purposes I need the substract function also…

1 Like

Sensor helpers already exist for sum. They do not exist for subtraction.

3 Likes

This can be solve if you think more than 5s About it. You can keep the sumation and add multipliers to the sensor outputs. This could do multiple interesting functions at once.

Please explain the ‘Statistical Range’ this does the “subtraction”; at least it works for me! to have a helper at the end that shows my actual power usage.

1 Like

Max minus min.

Yes, it’s not the most obvious way to subtract but it works for two enties. So for entities a, b statistical_range gives modulus(a-b) not quite true subtraction but would work ok if you know one is always bigger than the other, otherwise could confuse.

1 Like

It is also possible to use a helper of type template and subtract sensor values this way

For example

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

Simple replace sensor1 and sensor2 with your entity ids.

First argument of float function is the precision of rounding. In the example I don’t care about the decimal places at all. Pipe Sensor values to a float function have the effect that Unavailable or NaN Values are become a zero before subtraction.

2 Likes

That’s not true. The first argument of the float filter is the default value that’s output when the function cannot convert the string to a number.

Hello,
I have the same problem. I have two energy sensors. One for my heat pump, the other general.
It is not possible for me to subtract the heat pump from general consumption. Both are added to Energy dashboard

You can subtract them in a template sensor