If state_attr contains multiple words, how to do that?

I have this template that it trigger when my station 108 jamz is playing.
But i want it to trigger as well when spotify is playing.

i did try this but doesn’t work, any idea how i can solve this ?
{{ 'hot | spotify' in state_attr("media_player.living_room", "media_content_id") }}

this works , but need multiple words
{{ 'hot' in state_attr("media_player.living_room", "media_content_id") }}

{{ ('hot' or 'spotify')  in state_attr("media_player.living_room", "media_content_id") }}
you can also use 'and' when match is required

Hi thanks, but i just tested it and it doesn’t work.

if i do this, it says false, while it should be true

{{ ('123' or 'test1' or 'spotify' ) in state_attr("media_player.living_room", "media_content_id") }}

but when spotify is in the first place it triggers… don’t know why

I did also try only 2 word’s, sadly it doesn’t trigger.
It only say’s true if the word is in the first place.

I tried this first with states now with state_attr (friendly name) and I have no problems either ‘or’ ‘and’ work fine. Can you post your attributes?
EDIT: indeed it does NOT work nicely

friendly_name: Message: shopping zone entry (Arjan only)
{{ ('Arjan' and 'entry') in state_attr("automation.shopping_notification_arjan_only",'friendly_name') }} > true
{{ ('pff' and 'entry') in state_attr("automation.shopping_notification_arjan_only",'friendly_name') }} > false
{{ ('Arjan' and 'essntry') in state_attr("automation.shopping_notification_arjan_only",'friendly_name') }} > false
{{ ('ere' OR 'entry') in state_attr("automation.shopping_notification_arjan_only",'friendly_name') }} > false
the last one should show true

Yes thats weird, why did the one you posted not work ?

if i do this, it works, but looks to messy if i have a lot of words in there…

{{ 'poop' in state_attr("media_player.living_room", "media_content_id") or 'spotify' in state_attr("media_player.living_room", "media_content_id")}}

If there only was a way to create some variable list somewhere, so it can read the data and check it in one go.

if i gonna add 20 words in there it looks terrible.

why i need this is that i have found a way to let the music play after an announcement on google speakers, and it works really good, but if i need to add this code to all my automations what have TTS it looks just terrible.

I know there is a solution here, on ecan split the attribute and then compare but too far from my mid-advanced brains … adding a request to a real specialist @Taras ?

I found a solution that works for now, i need to test and see how fast it is.
Using regex

{{ state_attr('media_player.living_room', 'media_content_id') | regex_search('poop|spotify|test1|test2') }}

or this from @pedolsky

{% set content = state_attr('media_player.living_room', 'media_content_id') %}
{{ content is search('playlist|spotify') }}

That is a good one… I myself use the one earlier described (per word) which is not nice. WHat I am hoping for is array-level matching but not sure if this exists

Just for completeness:

{% set content = state_attr('media_player.living_room', 'media_content_id') %}
{{ content is search('playlist|spotify') }}

is also possible.

Many thanks @vingerha @pedolsky for helping me.

I selected the solution from #8 and merged @pedolsky his answer into that post.
Now this solves a lot of my automations :slight_smile:

Have a nice day.

Thanks, I also merged the two lines into one … my template is a lot more readable now too :slight_smile:

1 Like

Hi Firends!!

It works, i applied it as a binary sensor for my HA. I let you the code as a example:

  • platform: template
    value_template: “{{ state_attr(‘sensor.enricfon_bluetooth_connection’, ‘connected_paired_devices’) is search(‘EE:5C:XX:XX:XX:XX’) }}”

Thank you all

1 Like