Make negative value positive

Hello,
I have the problem that I do not get the negative value displayed as positive.
I tried with the following entry, but it seems to be an error.
The value “sensor.einspeisungbezug_3” is delivered by the smartmeter positive and negative
I need one entitiy pos and one negativ

template:        
- sensor:
      - name: "GridPowerIn"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
        state: >
          {% if states('sensor.einspeisungbezug_3') | float(0) >= 0 %}
            {% set gridin = states('sensor.einspeisungbezug_3') | float(0) %}
          {% else %}
            {% set gridin = 0 %}
          {% endif %}
          {{ gridin }}

      - name: "GridPowerOut"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
        state: >
          {% if states('sensor.einspeisungbezug_3') | float(0) =< 0 %}
           {% set gridout = states('sensor.einspeisungbezug_3') |float | abs) %}
          {% else %}
            {% set gridout = 0 %}
          {% endif %}
          {{ gridout }}

After restarting I always get this error:

Invalid config for [template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got '=') for dictionary value @ data['sensor'][1]['state']. Got "{% if states('sensor.einspeisungbezug_3') | float(0) =< 0 %}\n {% set gridout = states('sensor.einspeisungbezug_3') |float | abs) %}\n{% else %}\n {% set gridout = 0 %}\n{% endif %} {{ gridout }}\n". (See /config/configuration.yaml, line 179).

I can not find the solution

Use <= not =<

i.e. “less than or equal”, not “equal or less than”.

also with “<=” (less than or equal) comes same error

Please paste in the error message with <= in the template.

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensor'][1]['state']. Got "{% if states('sensor.einspeisungbezug_3') | float(0) <= 0 %}\n {% set gridout = states('sensor.einspeisungbezug_3') |float | abs) %}\n{% else %}\n {% set gridout = 0 %}\n{% endif %} {{ gridout }}\n". (See /config/configuration.yaml, line 179).

and here is the unexpected closing bracket :wink: after the “abs” which should cause your error

2 Likes

Thanks
It was the bracket. But I forgot the bracket in front of “states”

{% set gridout = (states('sensor.einspeisungbezug_3') |float |abs) %}

Those are unnecessary.

{% set gridout = states('sensor.einspeisungbezug_3') |float |abs %}