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

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

Thanks Petro. I will try it

I have solved it in this way:
Create a Number Helper ā€žMinus-1ā€œ with a fixed value of -1
Create a Helper ā€žHeatpump-Invertedā€œ which multiplies the Heatpump and ā€žMinus-1ā€œ
Sum General Consumption and Heatpump-Inverted, the result is what you want.

Hope this helps.
Joop

thank you. I try ASAP

Good idea. It works. Thanks to the community

1 Like

smart approach
can you please explain how you created the ā€œHeatpump-Invertedā€ helper ?
I could not figure out how to multiply a helper with another helper
Iā€™m obviously not experienced enough :wink:

@Komposit Hi sorry for my late answer. You have to select following:

Hello.
Great info butā€¦ probably I am doing something wrong.
I challenge you to find a solution on this.
(I used all kind of search tricks and no relevant result was found)

The calculated value is not considered numeric.
I give you the example:

  • created template numeric for battery SOH (I change it manually because cannot be read from BMS with comm) eg ā€¦97
  • created template for 1/100 divider
  • created template for brutto/designed battery capacity eg 15.3 (kwh)
  • created the group with product for all of three above named ā€œcalculated_battery_capacityā€

The result is correctly calculated but is not numeric because I cannot see it in a graph/line.
and because is not numeric, I cannot set resolution/precision 0.00 0.000.
It is considered string.
How can I create it to be considered numeric?

And I wish to use this result on calculating other new values (reserved capacity, actual remaining usable capacity, etcetera)