I cant get a sensor template working for one subtracted from another but 0 if negative

I am so sorry in advance. I have read the forums and tried many examples but I either get errors on the template editor, or that works, then errors in the yams on check, I move stuff around, get a clean yaml, then the sensors dont appear. I am two hours in and ready to give up for another day…

So… I have a current cost sensor working and providing two values - power_now_cc and solar_generation_cc. These entities are providing values. I wish to create two new sensors that will be “Exporting Power” and “Importing Power” based on the above - however the exporting power I want set to 0 if it is a negative.

My existing sensors (working fine) -

  - name: "Power Now - CC"
    unit_of_measurement: 'W'
    state: '{{ state_attr("sensor.current_cost", "Appliance 0") * 0.9533 | round(0) -300}}'
    device_class: power
    state_class: measurement
  - name: "Solar Generation - CC"
    unit_of_measurement: 'W'
    state: '{{ state_attr("sensor.current_cost", "Appliance 1")  * 0.9533 | round(0)}}'
    device_class: power
    state_class: measurement

From this forum, I have tried multiple options but I cant get any to work. My best ones are these below which work fine in the template developer tools and give correct results, but when added to the template sensor block, they pass the yaml test, I do the restart and all my other sensors stop working - so I comment these out, restart and the others all start working again. I guess I have something wrong in the syntax which is working in dev tools, no yaml error -

  - name : "Importing Power"
    unit_of_measurement: 'W'
    value_template: >-
      {{ [0, states('sensor.power_now_cc') | round - states('sensor.solar_generation_cc') | round] | max }}
    device_class: power
    state_class: measurement

  - name : "Exporting Power"
    unit_of_measurement: 'W'
    value_template: >-
      {{ [0, states('sensor.solar_generation_cc') | round - states('sensor.power_now_cc') | round] | max }}
    device_class: power
    state_class: measurement

The dev tools is good -

  • name : “Importing Power”
    unit_of_measurement: ‘W’
    value_template: >-
    0
    device_class: power
    state_class: measurement

    - name : "Exporting Power"
      unit_of_measurement: 'W'
      value_template: >-
        433
      device_class: power
      state_class: measurement
    

Where am I going wrong ?

Like a dog with a bine, eventually got it.
I had to change it to state:
now only that, I had to add it to the second line also, as state: "{{xxxxxx threw yaml error.

The final one that is working is this -

  - name : "Importing Power"
    unit_of_measurement: 'W'
    state: >
      {{ [0, states('sensor.power_now_cc') | round - states('sensor.solar_generation_cc') | round] | max }}
    device_class: power
    state_class: measurement

  - name : "Exporting Power"
    unit_of_measurement: 'W'
    state: >
      {{ [0, states('sensor.solar_generation_cc') | round - states('sensor.power_now_cc') | round] | max }}
    device_class: power
    state_class: measurement

Next job, making energy versions of these and getting that energy dashboard working… what fun!

1 Like

Does the version in your configuration also include this space character shown in the examples you posted?

  - name : "Importing Power"
        ^
        |

In addition, this calculation might not work the way you expect:

    state: '{{ state_attr("sensor.current_cost", "Appliance 0") * 0.9533 | round(0) -300}}'

It’s not rounding the result of the multiplication. It’s rounding the value 0.9533 to 1 then performing the multiplication (followed by the subtraction).

I believe this is want you may want (wrap the multiplication in parentheses):

    state: '{{ (state_attr("sensor.current_cost", "Appliance 0") * 0.9533 | round(0)) - 300 }}'

Example