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

Any progress on this?

yes, there is an open PR: https://github.com/home-assistant/core/pull/79398

This was merged three days ago into the codebase as part of the min/max/mean helper. I expect we’ll see this come out in stable sometime soon! :slight_smile:

2 Likes

Hi all, am I not mathematical enough, or is the subtract function not included ?

(Using 2022.12.0)

I don’t see subtract either.

I’m just coming back to confirm that SUM was released in stable in 2022.12. :+1:

Home Assistant Blog: Core Release 2022.12: Summing entities without templates

It’s working great! :grin: I’m using it to sum some WSDOT trip time measurements for the freeway routes between our company offices since they only provide the values on a per-segment basis, but I need it on a freeway exit-to-exit basis.

Like any other entity, it starts recording its historical data at the time you create the helper rather than just being a passthrough of the preexisting historical data from the entities that are being used for the sum. (That would have been amazingly neat, but I suspect it would be difficult to do efficiently.)

image


1 Like

Good job, but I don’t see subtract either.

3 Likes

No one is seeing subtract because it wasn’t … added.

(Couldn’t resist)

14 Likes

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.
9 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.

9 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.

4 Likes

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

1 Like