Problem detecting when Sonos ERA100 has changed to Bluetooth input

I am trying to set up an automation that detects when my Sonos ERA100 changes to Bluetooth input and then groups all the Sonos speakers together. I need this because connecting via bluetooth to a Sonos speaker will remove that speaker from any group it is currently in.
When I looked at the Sonos media player entity I observed that an attribute named “media_content_id” does change to include the work “RINCON” within a much longer string. So I made an automation to trigger on this change but it will not run.
I even made a trigger that detected any change on the attribute, but this will not run either. Below is the very simple trigger code from the automation. Can someone tell me why it is not working and how to fix it. Be gentle as I am very new to HA!

platform: state
entity_id:
  - media_player.kitchen_sonos
attribute: media_conent_id

Please post the full automation.

platform: state
entity_id:
  - media_player.kitchen_sonos
attribute: media_conent_id
                 ^^^^^^

Is that a copy-paste error or is it exactly as it appears in your automation? The word content is missing a t.

1 Like

OK - that is embarrassing!
Thanks for spotting the typo. I have no idea how that happened!
That has fixed that - now onto detecting the word RINCON in the string returned.

1 Like

An alternative is to use a Template Trigger.

platform: template
value_template: "{{ 'RINCON' in state_attr('media_player.kitchen_sonos', 'media_content_id') }}"
1 Like

Thanks so much for that.
I was so nearly there but missed the double quotes “…” around it.
You probably saved me hours!

Tip

If you’re going to use templates, the Templating section of the documentation is a must read. It’s dense with information but start by skimming it just to get an idea of what’s available. If nothing else, focus on this part:

Important Template Rules

Oh yes that would have done it:
“1. You must surround single-line templates with double quotes (") or single quotes (').”
Thanks again - and will do.

The issue appears to be a typo in the attribute name you’re trying to watch. You have media_conent_id instead of media_content_id.

Here’s the corrected version of your automation trigger:

platform: state
entity_id: 
  - media_player.kitchen_sonos
attribute: media_content_id

This should correctly watch for changes to the media_content_id attribute on the media_player.kitchen_sonos entity.

If this still doesn’t work, a few other things to check:

  1. Make sure you have the latest version of Home Assistant installed, as attribute triggers may have been introduced in a more recent version.

  2. Try using the state trigger instead of state_attribute and check the entire state object for changes:

platform: state
entity_id:
  - media_player.kitchen_sonos
platform: state
entity_id:
  - media_player.kitchen_sonos
  1. Add some debugging prints or notifications to your automation actions to confirm if the automation is getting triggered at all.

  2. Double-check the entity ID media_player.kitchen_sonos is correct for your Sonos speaker.

  3. As a last resort, you could try triggering on any state change to the entity, and then check the media_content_id attribute value in the automation’s condition:

trigger:
  platform: state
  entity_id: media_player.kitchen_sonos

condition:
  condition: template
  value_template: "{{ 'RINCON' in state.attributes.media_content_id }}"

This way, you can inspect the actual attribute value when the automation runs and debug further if needed. Doglikesbest

I can confirm it was a typo and now the automation is working well after using Taras Template suggestion above.
Thanks

1 Like