How to use is_state() with attributes?

I am trying to remove all the [homeassistant.components.binary_sensor.template] Could not render template... from the HASS log file, but I have been unable to use the is_state() function. How are you HASS users checking attribute values?
This is an example of the statement which litters the log file with the above messages:
{{ states.sensor.alarm_keypad.attributes.alarm }}

I get an unknown value when I do:
{{ states("sensor.alarm_keypad.attributes.alarm" }}
or
{{ is_state("sensor.alarm_keypad.attributes.alarm", "true") }}

NOTE: states.sensor.alarm_keypad.attributes.alarm is boolean.

Configuration example, please.

is_state wonā€™t check for an attribute, it will only check for state.

If sensor.alarm_keypad exists in the entity list, and if that entity has the attribute alarm, then the template {{ states.sensor.alarm_keypad.attributes.alarm }} will render without errors.

Travis,
The sensor.alarm_keypad does exists in the entity list and the template does work as expected in my automations/alerts. However, it still generates errors on startup of HASS. This would indicate a race condition where these binary sensors templates are evaluated before the referenced entity is created. So the next question is, is there an order in which these entities should be added?

Here is the section of the binary sensor template:

      security_alarm_alert:
        value_template: "{{ states.sensor.alarm_keypad.attributes.alarm }}"
        friendly_name: 'Security Alarm Alert'
        entity_id:
          - sensor.alarm_keypad

I ran into the same issue and found this thread first so wanted to point people to the solution:

Thereā€™s a is_state_attr(ā€œentity_idā€, ā€œattribute nameā€, ā€œstateā€) statement that you can use:

https://home-assistant.io/docs/configuration/templating/#home-assistant-template-extensions

4 Likes

Iā€™m actually trying to use this is_state_attr, but only get errors with it.
My config:

    kitchen_is_lit:
      friendly_name:          Kitchen Is Lit
      device_class:           light
      value_template:         {{ is_state_attr('sensor.kitchen_motion_sensor', 'dark', false) }}

results in

ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: invalid key: "OrderedDict([("is_state_attr('sensor.kitchen_motion_sensor'", None), ('dark', None), ('false)', None)])"
  in "/config/binary-sensors.yaml", line 24, column 0

Iā€™ve tried writing the attribute value ā€˜falseā€™ with quotes too.
Could someone please help figure out why, and how to fix it?

Is ā€˜darkā€™ an attribute of your motion sensor? Go the the template dev tool and type

{{ states.sensor.kitchen_motion_sensor }} And that will show you the attributes you can check

It sure should be

sensor.kitchen_motion_sensor
_____
off	
_____
light_level: 18035
battery: 100
last_updated: 2018-03-08,13:24:52
lux: 63.59
dark: false
daylight: true
temperature: 17.4
on: true
reachable: true
friendly_name: Kitchen motion sensor
icon: mdi:run-fast

If I paste in the value_template once hassio is up and running, then I get a proper True/False as expected. It seems to fail during booting.

Oh, so it works fine after HA is loaded? That happens with several of my value templates. Some sensors have ā€œnullā€ or nothing for their states at boot, I get this error, then a few min later they populate and itā€™s fine.

Kind of why Iā€™m stuck. Works great when I test the template, fails miserably in the config.

Have you tried:

{{ states.sensor.kitchen_motion_sensor.attributes.dark }}

which should just eval to true or false

or if you need to check if itā€™s false specificaly:

{{ states.sensor.kitchen_motion_sensor.attributes.dark==false }}
1 Like

Yeah, so the solution was of course super simple: Just use (the right) quotes!

binary_sensor: 
- platform:                    template
  sensors:
    kitchen_motion:
      friendly_name:           Kitchen Motion
      device_class:            motion
      value_template:          '{{ states("sensor.kitchen_motion_sensor") == "on" }}'

    kitchen_sensor_reachable:
      friendly_name:           Kitchen Sensor Reachable
      device_class:            connectivity
      value_template:          '{{ states.sensor.kitchen_motion_sensor.attributes.reachable }}'

Thanks for your time!

1 Like

I would like to make a switch which reflects a alarm sensor attribute state (true = switch on + call script, false = switch off + call service). I have {{ state_attr('sensor.business_keypad', 'chime') }} which gives me true/false output. How do I combine this with is_state()?

I would like to use it in a template switch:

switch:
  - platform: template
    switches:
      skylight:
        value_template: "{{ is_state('sensor.skylight', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.skylight
         turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.skylight
    value_template: "{{ is_state('sensor.skylight', 'on') and is_state_attr('sensor.business_keypad', 'chime', True) }}"
2 Likes

Thank you Petro.

  - platform: template
    switches:
      chime:
        value_template: "{{is_state_attr('sensor.home_keypad', 'chime', true) }}"
        turn_on:
          service: notify.notify
          data:
            message: 'chime on' 
        turn_off:
          service: notify.notify
          data:
            message: 'chime off' 

Switch is reflecting chime actual status, but notify service call doesnā€™t seem to be working in this setup?

1 Like

I have the same problem with is_state_attr never passing ā€œcheck configurationā€ in another application.

because you arenā€™t putting your template in quotes.

good

value_template: "{{ is_state_attr('light.foo', 'attr', 'blah') }}"

or

value_template: '{{ is_state_attr("light.foo", "attr", "blah") }}'

or

value_template: '{{ is_state_attr(''light.foo'', ''attr'', ''blah'') }}'

bad

value_template: {{ is_state_attr('light.foo', 'attr', 'blah') }}

or

value_template: {{ is_state_attr("light.foo", "attr", "blah") }}

or

value_template: '{{ is_state_attr('light.foo', 'attr', 'blah') }}'

or

value_template: "{{ is_state_attr("light.foo", "attr", "blah") }}"
4 Likes

Very helpful. I wish more of the documentation in these forums would migrate into the official documentation.