Sensor-Template works in developer-tool but nut in configuration.yaml

Hi,
I am new to Home Assistant and try to compensate wrong values of Sonoff SNZB-02 with an offset.

Therefore I wrote following Code in the developer tool which is working.

sensor:
  - platform: template
    sensors:
     {% set min_value = 25 %}
      rf_wohnzimmer_korrigiert:
        {% if (states('sensor.temperatursensor_wohnzimmer_humidity') | float > min_value) %}
          unit_of_measurement: '%'
          value_template: "{{ states('sensor.temperatursensor_wohnzimmer_humidity') | float - 20.0 | float }}"
        {% endif %}
        

Output is value_template: “43.8”.

If I add this code in the configuration.yaml it produces errors:

sensor:
  - platform: template
    sensors:
    {% set min_value = 25 %}
    
     {% set min_value = 25 %}
      rf_wohnzimmer_korrigiert:
        {% if (states('sensor.temperatursensor_wohnzimmer_humidity') | float > min_value) %}
          unit_of_measurement: '%'
          value_template: "{{ states('sensor.temperatursensor_wohnzimmer_humidity') | float - 20.0 | float }}"
        {% endif %}

Error message:
Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token

  • in “/config/configuration.yaml”, line 38, column 6*

I don’t know what I did wrong. Could someone give me advice please?

Thank you in advance,
Ben

You can’t use template / logic at that level. Try:

sensor:
  - platform: template
    sensors:
      rf_wohnzimmer_korrigiert:
      unit_of_measurement: '%'
      value_template: "{{ (states('sensor.temperatursensor_wohnzimmer_humidity')| float - 20) if (states('sensor.temperatursensor_wohnzimmer_humidity') | float > 25) else states('sensor.temperatursensor_wohnzimmer_humidity') | float }} }}"

Oh, I see. That solves my problem, thank you very much :slight_smile:

value_template: "{{ (states('sensor.temperatursensor_wohnzimmer_humidity')| float - 20) if (states('sensor.temperatursensor_wohnzimmer_humidity') | float > 25) }}"