Add 2 or more sensors together giving 1 number

I have 2 solar setups each giving production figures via sensors, can I combine 2 (or more) sensors together to create a completely new sensor giving the total solar of the 2 systems combined.

currentcost_garage_solar:
        entity_id: sensor.current_cost
        unit_of_measurement: 'W'
        value_template: '{{ state_attr("sensor.current_cost", "Appliance 1")[:-2]  }}'
        friendly_name: CurrentCost Garage Solar

currentcost_shed_solar:
        entity_id: sensor.current_cost
        unit_of_measurement: 'W'
        value_template: '{{ state_attr("sensor.current_cost", "Appliance 2")[:-2]  }}'
        friendly_name: CurrentCost Shed Solar

just create another template sensor and in value_template do what you want (convert to float and calculate a sum?) with your currentcost_garage_solar and currentcost_shed_solar

Thanks Ahmad but I don’t know how to do the sum

Something like this should work for you.:

       - platform: template
          sensors:
            currentcost_sum_solar:
              friendly_name: Total of Garage and Shed
              unit_of_measurement: W
              value_template: "{{ states('sensor.ChargingPower')|int + states('sensor.currentcost_shed_solar')|int }}"

2 Likes

I was going to suggest almost the same but with ‘float’ in place of ‘int’. you beat me to it :slight_smile:

1 Like

Yeah… I guess it really depends on what kind of value xion2000’s sensors return. My solar setup returns int values :slight_smile:

Thanks everyone, this has worked for me (other variations may work too)

currentcost_solar_total:
        unit_of_measurement: W
        value_template: "{{ states('sensor.currentcost_garage_solar')|float + states('sensor.currentcost_shed_solar')|float| round (0) }}"
        friendly_name: CurrentCost Solar Total

I’ve tried to remove any figures after the decimal point by using round (0) but its not working, it’s not that important but does anyone know how, cheers.

You might have to use extra brackets to ensure correct execution order in value template. I’d try something like the below for your value template (extra brackets around addition of the two sensor values :wink: ) :

value_template: "{{ ( states('sensor.currentcost_garage_solar')|float + states('sensor.currentcost_shed_solar')|float )| round (0) }}"

what’s your goal here? do you want to use it in future calculations or just to display it in frontend?
if the former, make your result int.
if the latter, use python/jinja format to control how it’s displayed.

the thing is sometimes you might have some decimal point even after round(0), try search this forum

Thanks for the help guys.
At the moment my goal is just for it to be displayed in the lovelace gui and I thought it looked neater without the decimal point. I had searched the forum but the solution I found was not working so I asked the question of members via this thread and got the solution in the end. Isn’t that the correct order of doing things?

then here’s what you’re after

value_template: >
  {% set total = states('sensor.currentcost_garage_solar')|float + states('sensor.currentcost_shed_solar')|float %}
  {{ '{:.0f}'.format(total) }}

it calculates your total and then displays it with 0 decimal points

that’s fine. you just need to know what to look for when searching. here I gave you some hints so use them in the future.

Where do you put that code into the configuration.yaml? How do you create a variable?

What “CODE” are you referring too?