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

Set a unit of measurement.

All states are strings.

You already can, you just need to use something like:

{{ states('sensor.calculated_battery_capacity')|float(0) }}

A template sensor would have been a much easier solution using an input number (slider) helper and a state template of:

{{ (states('input_number.battery_soh')|float / 100) * 15.3 }}

Thanks but…
Shouldn’t the calculated value to be considered NUMERIC since there were all entities numbers?

I cannot assign unit measure to the result of a group. There is no such option.

I understand everything can be created using yaml text code (for those understanding the code), I even considered using template yaml instead of helper GUI but there are a lot of tips’n’tricks, keywords and other chars to be used.
I chose the easy way because It was already there.

I didn’t think of writing complex formulas in the ā€œtemplate stateā€ box.

What I wonder, nobody seems to complain about the resulted/calculated helper values are not numeric.

I will try to recreate ā€œtemplatesā€ instead of ā€œgroup:productā€ keeping the sensor name.

I repeat: all states are strings.

That’s because the elements of your group have incompatible units, and you probably have an error message about this. Try giving your first element a unit of kWh too.

None of what I posted is YAML. It’s Jinja templates, all within the UI.

Can you add a screenshot of how you set these up?

1 Like

Designed capacity has already set kwh units.
Divider is a number.
SOH is set as %
All three are seen in graphs as numbers.
Where is the incompatibility?

Probably if I create SOH as simple number, it may work.

Thanks for the help.
I will take the template approach these days.

System simply isn’t that clever to ā€œmultiplyā€ the units. I’ve never come across anyone trying to do the maths with this method — would strongly recommend the template sensor I showed which is a single entity (plus the input number, if you choose to use that).

1 Like

:joy::joy::joy:
ā€œisn’t that cleverā€
Good one.

It does not need to be clever.
It calculates it and I expect to be considered numeric by default not a string.
It is a simple use case. Take 3 numeric entities and make a product (multiply). The displayed result is correct but is not usable as a number in a graph.
(I work in sw testing by the way)

Thanks.

There is no system in place outside of templates that does what you want. Use a template sensor. Set the device class, unit of measure, and state class. It will then be represented as a numerical entity in the ui.

Keep in mind that what troon says is still correct. All states in HA are strings.

Is it still not possible to subtract two entities with a Helper?

They actually implemented addition, but didn’t do subtraction?

4 Likes

Yes, it’s a pity, I also miss math functions in the helpers, especially subtraction!

1 Like

Simply create a Template Sensor helper and subtract the two values in the ā€œState templateā€ field.

In the following image, the value of sensor.subtrahend is subtracted from the value of sensor.minuend.

The difference between the two values will be displayed by a new entity called sensor.subtraction_example.

A detailed description of the template is provided here.

The documentation for creating templates is here.

1 Like

Any idea how to change this to an absolute value?

e.g. 4.0 and -4.0 should both result in 4.0.

Use the abs filter (ref).

{{ (states('sensor.minuend') | float(0) -
    states('sensor.subtrahend') | float(0))
   |abs }}
1 Like

I’ve disconnected the humidistat for my Aprilaire whole home humidifier and use a Zigbee dry contact switch now to turn it off and on. Currently I am averaging together 8 hydrometers around the house. The recommended Relative Humidity of the whole home humidifier is ((Outdoor Temperature / 2) + 25). I want to get that Humdistat measurement as a helper so I can create an automation where if the All Home Humidity level helper drops below the Humidistat helper it will kick the humidifer on and vice versa.

Are you asking for help? This should really be a new topic as it’s not directly related to the feature request.

Create a template sensor helper with a state template of:

{{ (states('sensor.outside_temperature')|float(0) / 2) + 25 }}

Substitute in your own outside temperature sensor entity ID.

Thank you. Sorry, I’m new to Home Assistant from another platform and learning how to utilize templates. I was able to figure it out and do exactly what you described. I have this template tied to a generic hydrostat with an automation that turns the hydrostat on and off depending on if the outdoor temperature is above or below 50° F.

I found this topic. I understand that it’s easier to implement addition and multiplication on a helper that has a list of entities. I also know that more complex arithmetic can be solved better in a template sensor, since you can have an expression there that does solve the ā€œorderā€ thing that is problematic with a list of entities.

That said, the current state of things is confusing at best for novice users. I’d like to suggest a couple of options to make it easier.

  • A simple solution could be to add separate helpers for subtracting and devision.
    • The subtract helper could still have a list of entities as argument and would then subtract all values.
    • The devision helper would have only two arguments. It would also need to have a solution to prevent devision by zero.
  • An alternative that’ already suggested, is to add an ā€œinvertā€ checkbox to each entity in the list. Benefit would be that it would still be possible to do a lot with a single helper, while other solutions would require a cascade of multiple helpers to get the desired result. It wouldn’t solve for the devision case though.
  • Another alternative that’s already suggested in the thread is to implement a separate inverter helper. That would solve for the subtract use case, but not for the devision use case.
  • Another alternative would be to allow for directly entering a constant value in the helper. Constant values now require a separate helper. With the constant -1 and multiplication, that would basically be the invert helper.
  • The Group sensor does have multiplication, but the ā€œcombine entitiesā€ helper does not. That’s an inconsistency I can’t find a good explanation for.
  • A more fancy solution would be to create a true arithmetic helper, where you could create arithmetic expressions. That’s arguably very close to a the template helper though. The benefit would be that it could have more guidance for novice users, or even a graphical expression builder.
1 Like