Using Attributes in a Template

Since this isn’t working, I must be doing something wrong. I am looking to use this create some sensors when certain equipment is running.

This returns: fan,heatPump

{{ states.climate.home.attributes.equipment_running}}

This returns: off
It should be returning on, why is that?

{% if states.climate.home.attributes.equipment_running in ['fan'] %}
on
{% else %}
off
{% endif %}

Because “fan,heatPump” is not in [‘fan’]

‘fan’ is in [‘fan’ ,’ heatPump’]

Two different logical tests.

Try this:

{% if 'fan' in state_attr('climate.home', 'equipment_running') %}
  on
{% else %}
  off
{% endif %}

How are you using this template?
I suspect there is a better way, please show the full sensor config.

Hi,

In the developer > template editor it worked.

When I added it to an automation, I ran into issues referring to the first if line:

while scanning for the next token found character ‘%’ that cannot start any token

value_template: 
        {% if 'fan' in state_attr('climate.home', 'equipment_running') %}
        on
        {% else %}
        off
        {% endif %}
value_template: >

Please post the rest of your sensor.

  - platform: template
    sensors:
      hvac_humidifier:
        friendly_name: 'HVAC Humidifier'
        value_template: >
        {% if 'humidifier' in state_attr('climate.home', 'equipment_running') %}
        on
        {% else %}
        off
        {% endif %}

Ah, that’s what I though. If it only has two states use a binary sensor:

binary_sensor:
  - platform: template
    sensors:
      hvac_humidifier:
        friendly_name: 'HVAC Humidifier'
        value_template: "{{ 'humidifier' in state_attr('climate.home', 'equipment_running') }}

If the template evaluates to true the state will be on and likewise off for false. This will be displayed capitalised in the Lovelace frontend.

Thank you, worked like a charm!

1 Like