Help with sensor template pls

Hi,

I’m trying to make this work for acouple of days now without succes.
This is what I have now without errors in the logs.

  - platform: template
    sensors:
     airco_current_temp:
       entity_id: climate.melcloud_zolder_117363
       value_template: '{{ climate.melcloud_zolder_117363.attributes.current_temperature | float }}'       
     airco_mode:
       value_template: >-
         {% if states('sensor.airco_current_temp') | float < 23 %}
           Heat
         {% else %}
           Cool
         {% endif %}

sensor.airco_current_temp returns “unknown” & sensor.airco_mode “Heat”.

Any idea?

Thanks!!

E.

Have you tried the template editor in home assistant? Maybe surround “states(… float” with parenthesis?

Try this:

  - platform: template
    sensors:
      airco_current_temp:
        entity_id: climate.melcloud_zolder_117363
        value_template: "{{ state_attr('climate.melcloud_zolder_117363', 'current_temperature') | float }}"
        unit_of_measurement: '°C' # or F if that is what you use.
      airco_mode:
        entity_id: sensor.airco_current_temp
        value_template: >-
          {% if states('sensor.airco_current_temp') | float < 23 %}
            Heat
          {% else %}
            Cool
          {% endif %}

In case you missed it, tom_I corrected an indentation error (and several others things).

You indented using only one space when it should be two.

  - platform: template
    sensors:
     airco_current_temp:
    ^
Should be two spaces

Thank you all for the reply’s but 10min after posting this I found the problem:

This works:

  - platform: template
    sensors:
     airco_current_temp:
       entity_id: climate.melcloud_zolder_117363
       value_template: '{{ states.climate.melcloud_zolder_117363.attributes.current_temperature | float }}'
     airco_mode:
       value_template: >-
         {% if states('sensor.airco_current_temp') | float < 23 %}
           Heat
         {% else %}
           Cool
         {% endif %}

Forgot the “states”…

E.

Well that’s special. This is one of the few exceptions to the 2-space indentation rule. I just tested it on my own system (everything under sensors: indented by only 1-space) and it passed Config Check. However, adopting this 1-space practice can cause problems just about everywhere else.

BTW, did you try tom_I’s solution? He fixed the error you made concerning the attribute (technically speaking, his post contains the solution). In fact, his solution used the state_attr function which is more fault-resistant than the technique you employed.