Eliminate values less than zero

Goodnight.
I created a sensor to calculate the value of watts exported. the problem is that when there is no solar energy, the sensor gives negative values. how do I make the sensor go to zero when this happens?
Ths :wink:

  - platform: template
    sensors:
      watts_exportados:
        friendly_name: "Watts exportados"
        unit_of_measurement: 'W'
        device_class: power
        value_template: "{{ (states('sensor.wibeee_phase2_active_power')|round ) -  (((states('sensor.wibeee_phase4_active_power')|round ) + (states('sensor.wibeee_phase3_active_power') |round))) }}"
  - platform: template
    sensors:
      watts_exportados:
        friendly_name: "Watts exportados"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% set power = (states('sensor.wibeee_phase2_active_power')|round ) -  (((states('sensor.wibeee_phase4_active_power')|round ) + (states('sensor.wibeee_phase3_active_power') |round))) %}
          {{ 0 if power < 0 else power }}
2 Likes

Another way to do the same thing:

        value_template: >
          {{ [0, states('sensor.wibeee_phase2_active_power')|round - states('sensor.wibeee_phase4_active_power')|round + states('sensor.wibeee_phase3_active_power')|round] | max }}
1 Like

Another solution would be to use a range filter.

You define upper and lower limits, and values outside of the range will replaced with the corresponding limit.

Thank you all. Fatastic community!