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?

2 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