Adding two negative value sensors together to make the new sensor value positive

Help, I’m trying to add two negative sensor values together from my solar arrays to make a new sensor value which is positive.

I’ve successfully managed to convert the sensors to show a positive value as follows:

- name: Solar Patio New
  device_class: power
  state_class: measurement
  unit_of_measurement: "W"
  state: "{{ states('sensor.solar_patio_power') | int(0) | abs }}" 

- name: Solar Shed New
  device_class: power
  state_class: measurement
  unit_of_measurement: "W"
  state: "{{ states('sensor.shed_new_power_2') | int(0) | abs }}"

However, when I try to combine these values into a new sensor 'Total Solar Power, the value is showing as negative, rather than positive. Where am I going wrong?

  • name: Total Solar Power
    device_class: power
    unit_of_measurement: “W”
    state: >
    {{ [ states(‘sensor.solar_patio_power’) | int(0) | abs }},
    states(‘sensor.shed_new_power_2’) | int(0) | abs }}]
    | map(‘float’) | sum }}
    availability: >
    {{ not ‘unavailable’ in
    [ states(‘sensor.solar_patio_power’) | int(0) | abs }},
    states(‘sensor.shed_new_power_2’) | int(0) | abs }} ] }}

Thanks

You have too many brackets.

try this:

state: >
  {{ [ states('sensor.solar_patio_power') | int(0) | abs, states('sensor.shed_new_power_2') | int(0) | abs ] | map('float') | sum }}
availability: >
  {{ not 'unavailable' in [ states('sensor.solar_patio_power'), states('sensor.shed_new_power_2') ] }}

Thanks for amending, it worked perfectly :slight_smile:

One further question, the output value of the new sensor is now returning a decimal point. How do i remove this? Thanks
Capture

state: >
  {{ ( [ states('sensor.solar_patio_power') | int(0) | abs, states('sensor.shed_new_power_2') | int(0) | abs ] | map('float') | sum) | round() }}

the “round()” by default rounds to 0 decimal places. So it’s the same as “round(0)”. So you can get additional decimal places by changing the number inside the parenthesis (“round(2)”, etc).

or you can just convert the result directly to an integer:

state: >
  {{ ( [ states('sensor.solar_patio_power') | int(2) | abs, states('sensor.shed_new_power_2') | int(4) | abs ] | map('float') | sum) | int()}}

Perfect, thanks for your help.