If/else statement for positive oder negative consumption

Hey there, i am trying to create an if/else statement for my energymeter. this sensor shows my energy consumption. if the value is positive, i get energy from the grid. if the value is negative, i give energy to the grid. I would like to create a new sensor, which shows me the amount of energy i am giving to the grid. the following code shows my idea. the config starts with no errors, but the sensor only shows “not available”

sensor:
  - platform: template
    sensors:
     einspeiseleistung:
        value_template: >-
          {% if states('sensor.Haus_Leistung_W') > 0 %}
            0
          {% else %}
            {{ states('sensor.Haus_Leistung_W') | abs }}
          {% endif %}

Do you have an idea?

sensor:
  - platform: template
    sensors:
      einspeiseleistung:
        friendly_name: "Einspeiseleistung"
        unit_of_measurement: "W"
        value_template: >
          {% set hl = states('sensor.haus_leistung_w') | float(0) %}
          {{ 0 if hl > 0 else hl | abs }}
1 Like