Sensor template not showing any value

Hi all,

I tried to create a template sensor (Heating setpoint) which is 10 C higher than the outside temperature, but at least 5 and maximum 10 C.

for this i added below to my configuration.yaml

- platform: template
  sensors:
    heating_setpoint:
      friendly_name: "heating setpoint"
      unit_of_measurement: "°C"
      unique_id: fasdfsadrrjfffsdajmggnr231
      value_template: >
        {% set heating_setpoint = states('sensor.st_00137716_temperature') | float(none) %}

        {% if heating_setpoint is none %}
          {{ 5 }}  # Default to 5 if sensor is unavailable
        {% else %}
          {% set temp = heating_setpoint + 10 %}
          {% set clamped_temp = [5, temp, 10] | sort | slice(1,1) | first %}
          {{ clamped_temp }}
        {% endif %}

altough the sensor is created after restarting home assistant, it does not show any value.
what mistake did i make?
thanks in advance for any advise…

My guess would be the comment. You cannot insert a YAML comment into a Jinja template. Jinja template syntax is to bracket the comment between {# and #}.

1 Like

That’s not how you do comments in Jinja and your slicing method won’t return a single numeric value, which is required if you set a unit_of_measurement.

{% set heating_setpoint = states('sensor.st_00137716_temperature') | float(none) %}

{% if heating_setpoint is none %}
  5  {# Default to 5 if sensor is unavailable #}
{% else %}
  {% set temp = heating_setpoint + 10 %}
  {% set clamped_temp = ([5, temp, 10] | sort)[1] %}
  {{ clamped_temp }}
{% endif %}
1 Like

Also you should not be using the legacy template sensor platform for new sensors. It will keep working but will not be getting new features.

New format (and some simplifications):

configuration.yaml

 template:
   - sensor:
       - name: "Heating Setpoint"
         unit_of_measurement: "°C"
         unique_id: fasdfsadrrjfffsdajmggnr231
         state: >
           {% set heating_setpoint = states('sensor.st_00137716_temperature') | float(-9) %}
           {% set temp = heating_setpoint + 10 %}
           {{ ([5, temp, 10] | sort)[1] }}
1 Like