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

It would be nice to have subtract… or the ability to include any kind of manual/static values rather than only including references to entities. One could make a helper that contains a negative number and then include than as part of a sum. It’s a workaround, but that could probably work? (she says, not having tried it… :woman_shrugging:)

1 Like

I tried make helper with constant negative walue… wihtout success.
But I made some trick.
I made virtual mqtt sensor.
Usine MQTT Explorer i publish topic with negative value. (Remember topic must have Retain Flag).
Next, I made sum with my sensor.
It’s work but need a lot of work. Aubstract with constant value should be much simply

you are kidding me - came on ad subtract - its on the f–n icon already.

and this was first helper i try to make!!!

9 Likes

Subtract would be awesome! It would reduce the number of yaml-created templates a lot.

3 Likes

Also running into the issue of not having substract, would be awesome if this was added because now i still need to use templates…

1 Like

Until a ‘Subtract Two Sensors’ Helper is implemented (if ever) here’s how to do it with a simple Template Sensor.

In the following example, the goal is to subtract the value of sensor.subtrahend from the value of sensor.minuend and report the result in sensor.difference.

template:
  - sensor:
      - name: Difference
        state: "{{ states('sensor.minuend') | float(0) - states('sensor.subtrahend') | float(0) }}"

How the template works:

  • states('sensor.minuend') gets the state value of sensor.minuend.
  • State values are always strings (text) even when they appear to be numeric. We convert the string value to a floating point number (a number with decimal places as opposed to integer) using float(0).
  • The 0 in float(0) is a chosen default value. If float is unable to convert the sensor’s state value it will report 0 (the default value). You can set the default value to whatever is best for your application. For example, if the sensor’s value is unavailable then float will report 0.
  • The second half of the template simply performs the same procedure for sensor.subtrahend and then subtracts the resulting value from the value of sensor.minuend.
8 Likes

Substraction of a fixed value is possible though:
Create a Number with a negative value.
Create a ‘Combine the state of several sensors’ and ‘Sum’ the Number and e.g. a sensor value.
I used this to make a sensor offset.

If you see a ‘non-numeric’ error when using the Sum result in a Card on your Dashboard: A restart of Home Assistant will solve this error.

Also looking for subtract.
Could be a good option to negate any used sensor with a checkbox, then SUM could be simply used for subtract (not only 2 sensors, but any)

2 Likes

2023 and still without subtract option :man_facepalming:
I wish it would be included in the next update…

Please Home Assistant team!

7 Likes

No multiply either :roll_eyes:. All im after is multiplying voltage and current to give watts.

7 Likes

Same here. Multiply a sensor with another sensor or a certain value would be great.

I just want to get rid of my templates as I’m mostly on smartphone and the config file isn’t userfriendly there.

3 Likes

big facepalm when I came here and see that only sum is available, would be great to have at least difference?

1 Like

I have a simple suggestion. Sum will work for either subtract or sum if we were able to set a flag per selected entity that simply states “Invert”. This would work better in my mind because if you are doing a calculation with more than two sensors, you may want to have one be a negative value in the equation and this the sum is calculated accordingly.

An example for me is trying to ascertain my non-load items on my inverter, can get the Total , can get the UPS load with a sum but there isnt a way to take the first total and subtract the new UPS value to get the non-load value.

Just a thought. And the inversion can just be a simple {value}*-1.

3 Likes

Agreed that not having the basic arithmetic primitives (+, -, *, /) is weird (if not subtraction, then at least negation as commented by kosmika)…

Having all of the standard ‘calculator’ operations would certainly make the most sense.

5 Likes

Sum and Product (multiply) is part of Group sensor (min_max/Combine several … will be deprecated).
PR’s are made for subtraction and division which obviously also is for Group sensor then.

1 Like

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