"state: >" or "value_template: >-" in Template?

I think I’ve drafted a new sensor to recommend a suitable boiler flow temperature based on the outside temperature at my home (rounded to the nearest degree):

  - sensor:
    - name: House Boiler Flow Temperature
      unit_of_measurement: "°C"
      availability: >
        {{ states('sensor.outside_temperature_rounded') not in ['unavailable', 'unknown'] }}
      state: >
        {% set Toutside = states('sensor.outside_temperature_rounded') | float(0) %}
        {% if 50 > Toutside > 12.5 %} 50
        {% elif 12.5 > Toutside > 9.5 %} 50
        {% elif 9.5 > Toutside > 5.5 %} 55
        {% elif 5.5 > Toutside > 0.5 %} 55
        {% else %} 60
        {% endif %}```

This matches the format of other templates I have found online and have modified for other things in my Config.

However, I have also seen templates where instead of "state: >" in the 7th row they use "value_template: >-".

Which one should I use in my template please? And why?

Many thanks.

> and >- are markers for multi-line YAML.

>- is used when you want to strip out newlines, and > when you don’t

The configuration format for Template Sensors was changed (in 2021.4.0).

The new format, sometimes called “modern” format, uses a different structure and option names. For example, state replaces the old format’s value_template.

The old format, now called “legacy” format, is still acceptable and the community forum is filled with examples (because it was the only way to configure Template Sensors for several years).

Although Home Assistant continues to support legacy format, when configuring new Template Sensors it’s preferable to use modern format. Modern format includes new features that are not supported by legacy format and will never be added to legacy format.

1 Like

And in 9 out of 10 examples I see on the forum it is used unnecessarily.

2 Likes

Thanks for explaining that, I shall go ahead with state: