Simply add two sensors together?

Really dump question here - all I want to do is add two sensor values together… how the heck do I do this?

I think I need a value template but I can’t seem to get the right combination of brackets etc to get it working.

Sensor 1 - name - sensor.powermon_housesolar state - 543.38
Sensor 2 - name - sensor.powermon_shedsolar state - 474.87

I just want to create a new sensor called ‘total’ with sensor 1 state + sensor 2 state… the template sensor page has loads of complex examples, I just need a simple one :slight_smile:

1 Like
value_template: "{ states('sensor.powermon_housesolar') | float + states('sensor.powermon_shedsolar') | float }"
3 Likes

No luck @tom_l - it just seems to output the text of the formula rather than evaluating it…

image

Code -

  - platform: template
    sensors:
      powermon_totalsolar:
        friendly_name: "Solar Generation - Total"
        value_template: "{ states('sensor.powermon_housesolar') | float + states('sensor.powermon_shedsolar') | float }"
        unit_of_measurement: 'watts'

EDIT - It just needed an extra pair of {} to make it evaluate…

value_template: “{{ states(‘sensor.powermon_housesolar’) | float + states(‘sensor.powermon_shedsolar’) | float }}”

… is now working. Cheers @tom_l!!

4 Likes

Apologies that was a stupid mistake. I’m neck deep in templates myself at the moment not sure how I stuffed that up.

Great, thanks, I just made my sensors adding two sensors together!

Great value cause question was spot on to be found by google and also solved and still, years later, valid so lesson learned.
And this reply might add the likelihood that others will find this too cause it helped a lot adding up the solar generator output over 5 growatt inverters to get the key figures how man kWh had been produced, how many fed in and and how may been used. A feature all fans of Watts & Wh will love and share.
thx

Almost. These days, you should add a default value onto the float filter in case the sensor state cannot be converted. Usually something like:

value_template: "{{ states('sensor.a')|float(0) + states('sensor.b')|float(0) }}"
1 Like

will add it to my master templates

thanks for the quick reply

Also you can do this in the UI with a helper now.

1 Like

Tried it out - works great and lot, lot easier than the yaml editing where just a space or bracket more or less can cost you hours.

Could you indicate which helper does this please?

Min/Max has a few more settings in drop box

1 Like

So the helper in question would be 3rd one on the picture

But I can’t see simple addition or subtraction operations here:

HA 2023.10.0

1 Like

Sum = addition.

Cool but I want to subtract :innocent:
Actually the +/- icon for the helper made me think that basic operations would be available, I’ll go with a template then. Thanks

The original poster of this topic didn’t though. Whose was why the answer was relevant.

It’s far more powerful to use yaml. The GUI is a very limited version of what is available. On purpose. If the devs tried to include everything they’d essentially be writing a yaml editor.

Hi All,

I want to have 1 sensor due that this isn’t correct showing in my energy graph.
In Belgium we’re having 2 tarifs, high (from 7AM - 10PM) & low (10PM-7AM).
So in my HA i’ve 2 sensors energyconsumption tarif 1 (thats the High) and tarif 2 (Low). I want to merge those to 1 sensor. Issue here is that they give the total amount of electricity that has been used over the time that the meter is used so no daily meter.
Is there a way to merge those sensors and give a daily usage of the grid?
I was thinking to make something in the configuration file like:

template:

  • sensor:
    • name: “Dagelijks Stroomverbruik”
      unit_of_measurement: “KW”
      unique_id: ‘Dagelijks Stroomverbruik’
      state: >
      {{ states(‘sensor.electricity_meter_energieverbruik_tarief_1’) | float(0) + states(‘sensor.electricity_meter_energieverbruik_tarief_2’) | float(0))) }}

But this isn’t correct, anyone that can help me?

Thanks!

Please format your code correctly by surrounding it with three backticks:

```
YOUR CODE HERE
```

With the code you’ve pasted, we cannot see any indentation errors (important in YAML) and the forum substitutes “smart quotes” for the real ones.

It looks as though you have two excess close-parentheses )) at the end of your template; and the unit should be kW with a lower-case k.

You can create template sensors in the UI now (so no need for YAML), under Helpers:

State template:

{{ states('sensor.electricity_meter_energieverbruik_tarief_1') | float(0)
 + states('sensor.electricity_meter_energieverbruik_tarief_2') | float(0) }}

Ok thanks i’ve tried to add it but i get the following result:

Its the total amount of kW but i want to have it for each day. Do i need to create a new sensor based on this where i can say to reset it each day?