State if state

Sensors Offline

{% for state in states.sensor -%}

{% if state.state == ‘down’ %} {{ state.attributes.friendly_name }} {{‘\n’}} {% endif %}

{%- endfor %}

Really ? “my state”.is.state.with an extra state. “mystate”.state.state.

Make it make sense.

The first state is the state object which has a lot more information than just its .state.

Using a keyword as a variable like this will make you insane. Just use another word.

1 Like

While I agree with SG that it’s best to avoid using keywords as variables, I don’t understand what you are actually complaining about…?

FWIW, you can get the same results without the for loop by using the built-in filters:

{{ states.sensor | selectattr('state', 'eq', 'down')
| map(attribute='name') | list | join('\n') }}

My preferred one is

{% for StateStateOfStates in states.sensor -%}

{% if StateStateOfStates.state == ‘down’ %} {{ StateStateOfStates.attributes.friendly_name }} {{‘\n’}} {% endif %}

{%- endfor %}

Just love that long variable name.
But luckily for you, you can choose anyone you want.
You used state and apparently you didn’t like that.
So what about just s?

{% for s in states.sensor -%}

{% if s.state == ‘down’ %} {{ s.attributes.friendly_name }} {{‘\n’}} {% endif %}

{%- endfor %}

Thanks for the responses people.

I have to be honest I picked this gem up somewhere on the interwebz and it just confused the hell out of me.

I am a 3 rate template jockey, so basically an idiot. I get by with the simple things. But these answers cleared up a LOT!

Especially this: “Using a keyword as a variable like this will make you insane. Just use another word.” I misunderstood that part completely.

Thank you!