Checking for a value in a list using is_state_attr

Hi there,
In one of my automations, I’d like to trigger actions when a state attribute has a certain value.
The values are in a list. Thanks to this post (Templating Help With State Attributes), I was able to use the parse function to read the value using state_attr.

By using the following, I can read the value of condition:

{{ state_attr('weather.my_city_hourly', 'forecast')[0]['condition'] }}

What I want to do is say “if the value of condition is sunny, do something”.

Using is_state_attr without the parsed part works fine (see Templating - Home Assistant):

is_state_attr('device_tracker.paulus', 'battery', 40)

What is the syntax when the value is in a list?
I’ve tried placing , sunny after condition, with and without brackets, but nothing has worked so far.

Can anyone help me with this?

alias: example
trigger:
  - platform: template
    value_template: >
      {{ 'sunny' in state_attr('weather.my_city_hourly', 'forecast')[0]['condition'] }}
condition: []
action:
  ... etc ...

is_state_attr() is meant for comparing an attribute’s value to a reference value. It’s not suited for what you’re attempting to do (compare a portion of an attribute’s value to a reference value).

Thank you so much! That works perfectly :slight_smile:

1 Like