How to combine (sum) two Energy consumption sensors A + B = C

I have a hot water boiler that reports two different consumption sensors. (Let’s just call them A and B). I tried to define a new Helper using the “Combine the state of several sensors” option, but it doesn’t seem to do what I hoped for.
First of all, the combined new sensor state is permanently “unknown”, despite the fact that A and B has real values.
Secondly, the new sensor doesn’t inherit the same class as A and B which are both kWh sensors.
Any hints on how to solve/correct this is much appreciated.

You can create a helper template sensor in the UI where the state template looks something like:

{{ states('sensor.a') + states('sensor.b') }}

The “combine the state” helper should do both things you want (1: result in a value, and 2: use the same unit_of_measurement as both source sensors). I just tried this out and it is working as expected in my installation. So my guess is that one or both of your source sensors have something wrong with them.

You can do a template helper, but I would be surprised if it didn’t suffer from the same issue.

Go to developer tools > states, find one of your source sensors, and let us know what the state is, and what the state attributes are.

Almost. The cast is missing (either int or float), because states are strings.

{{ states('sensor.a') | float(0) + states('sensor.b') | float(0) }}

It’s also best to set an availability template for a sensor, e.g.:

{{ has_value('sensor.a') and has_value('sensor.b') }}

Aha, I think perhaps your advise made me discover the error; Sensor A is Wh while sensor B is kWh.
Is there an easy way to fix this?

State template of:

{{ states('sensor.a')|float(0) + (states('sensor.b')|float(0) * 1000) }}

will give you Wh total, or

{{ (states('sensor.a')|float(0) / 1000) + states('sensor.b')|float(0) }}

will give kWh.

1 Like

Ok, I’ve done this, and so far it looks ok.
I defined a new Templated helper and assigned:
Unit of measurement: kWh
Device Class: Energy
State Class: Total

I’ll have to wait and see if values appear correctly.
Thanks for your help.