Help me select which speaker group my alarm plays on when triggered

All,

GOAL: I’m trying to use an input select drop-down on my HA dashboard to select which speaker group my alarm plays on when it’s next triggered.

I’ve created an input_select.room_speaker_list that lists the groups I want in plain language.

  • All Speakers
  • Office Speaker
  • Common Area

Etc… I have added this input select list to an entity card on my dashboard and am able to see/ select the options without issue.

Additionally, I have created an alarm automation based on the wonderful work posted on this forum.

When the alarm is triggered, it does a number of things including

  • media_player.volume_set
    –> to set chromecast group volume to a specific amount, currently sets media_player.all_speakers to vol .9
  • media_player.play_media
    –> currently plays a short jingle on media_player.all_speakers
  • tts.google_cloud_say
    –> currently announces the time based on if/elif statement using above input select (see down below for code) <== this appears to be working

While this input select statement works with tts.google_cloud_say, it does not appear to work with either media service (vol set/ play media) resulting in the following message

Message malformed: not a valid value for dictionary value @ data['action'][4]['entity_id']

So, my question is how do I use an input select to substitute for entity_id: for the media_player services

I’ve tried using if/elif, I’ve tried using entity_id: input_select.room_speaker_list, I’ve tried using entity_id: ‘{{states(“input_select.room_speaker_list”)}}’

I’m stumped and raising the white flag. Appreciate any help you can offer.

entity_id: |
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%-elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%-elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}

Try this

entity_id: |-
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%- elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%- elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}

Thanks so much for the suggestion!
It’s not working in my case though, still getting
Message malformed: not a valid value for dictionary value @ data['action'][4]['entity_id']

Is the media_player.all_speakers working with services in the developer console?

did you use data_template: or data: ?

If you use a template for the entity_id, then the entity_id has to be in the data: block as well.

Yes, it works fine for playing media to all speakers as long as it’s referenced directly on entity_id

Looks like the action is using data:

Please show all of the alarm automation.

Like this? Added entity_ID to the data block as well, but unfortunately, I rec’d the same malformed message

service: media_player.volume_set
data:
  volume_level: 0.9
  entity_id: |
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%-elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%-elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}
entity_id: |
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%-elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%-elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}

Now you have entity_id twice for the same service.
You can also simplify this, as your input selects options are the entity_ids just in proper case and with spaces. Try this:

service: media_player.volume_set
data:
  volume_level: 0.9
  entity_id: >
    media_player.{{ states("input_select.room_speaker_list").lower().replace(" ","_") }}

But as Taras said, please show the whole automation, could as well be that the error is coming from another place.

It’s duplicated and incorrectly indented. There may be other errors but we will only know for sure when you post the entire automation.

wake_1_timer_started (test)

Trigger 
platform: event
event_type: timer.started
event_data:
  entity_id: timer.wake_1

#(I've removed conditions from this automation as it's not relevant to testing)

Actions
scene: scene.bedroom_lights_red

service: media_player.volume_set
data:
  volume_level: 0.9
entity_id: |
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%-elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%-elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}

delay:
  hours: 0
  minutes: 0
  seconds: 0
  milliseconds: 500

service: input_select.select_next
data: {}
entity_id: input_select.wake_up_alarms

service: media_player.play_media
data:
  media_content_id: |
    {% if is_state("input_select.wake_up_alarms", "jump_start") %}
      https://filerun.bluwater.pw/wl/?id=zdwirnbvPry6JUcbJHRVOtZwW1v2NevL&download=1
    {%-elif is_state("input_select.wake_up_alarms", "gentle_breeze") %}
      https://filerun.bluwater.pw/wl/?id=sAzBf8ZhzZOmRBZgpKvFok9yAFKjtjCL&download=1
    {%-elif is_state("input_select.wake_up_alarms", "bright_morning") %}
      https://filerun.bluwater.pw/wl/?id=9C9rxeL1nM000QiQVuTo0qx8rf58Ahx2&download=1
    {% else %}
      none
    {% endif %}
  media_content_type: music
entity_id: |
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%-elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%-elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}

delay:
  hours: 0
  minutes: 0
  seconds: 14
  milliseconds: 500

scene: scene.bedroom_lights_up

service: input_select.select_next
data: {}
entity_id: input_select.wake_up_list

service: tts.google_cloud_say
data:
entity_id: |
  {% if is_state("input_select.room_speaker_list", "All Speakers") %}
    media_player.all_speakers 
  {%-elif is_state("input_select.room_speaker_list", "Common Area") %}
    media_player.common_area 
  {%-elif is_state("input_select.room_speaker_list", "Office") %}
    media_player.office 
  {% else %}
    none
  {% endif %}
  message: |
    It's {{states('sensor.time')}}, {{states('input_select.wake_up_list')}} 

This is what I would like to work, from an actions perspective, currently though only the tts section is functioning properly with the if/elif statement as entity_id

Please format it correctly. In its current form, it’s impossible to determine if it is indented properly.

I tried to decipher the unformatted automation you posted and then correct it. Here is the result which also includes several techniques to reduce duplication of code. This version passes “Check Configuration”.

- alias: wake_1_timer_started (test)
  trigger: 
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.wake_1
  condition:
  action:
  - variables:
      media: >
        { 'jump_start': 'https://filerun.bluwater.pw/wl/?id=zdwirnbvPry6JUcbJHRVOtZwW1v2NevL&download=1',
          'gentle_breeze': 'https://filerun.bluwater.pw/wl/?id=sAzBf8ZhzZOmRBZgpKvFok9yAFKjtjCL&download=1',
          'bright_morning': 'https://filerun.bluwater.pw/wl/?id=9C9rxeL1nM000QiQVuTo0qx8rf58Ahx2&download=1'
        }
      speaker: >
        {% set speakers = ['All Speakers', 'Common Area', 'Office'] %}
        {% set s = states('input_select.room_speaker_list') %}
        {{'media_player.' ~ s.replace(' ', '_')|lower if s in speakers else 'none' }}
  - scene: scene.bedroom_lights_red
  - service: media_player.volume_set
    data:
      entity_id: '{{ speaker }}'
      volume_level: 0.9
  - delay: 0.5
  - service: input_select.select_next
    entity_id: input_select.wake_up_alarms
  - service: media_player.play_media
    data:
      entity_id: '{{ speaker }}'
      media_content_id: >
        {% set a = states('input_select.wake_up_alarms') %}
        {{ media[a] if a in media.keys() else 'none' }}
      media_content_type: music
  - delay: 14.5
  - scene: scene.bedroom_lights_up
  - service: input_select.select_next
    entity_id: input_select.wake_up_list
  - service: tts.google_cloud_say
    data:
      entity_id: '{{ speaker }}'
      message: "It's {{states('sensor.time')}}, {{states('input_select.wake_up_list')}}"

What’s missing are the automation’s conditions you had excluded from your previous post. You will need to add them to this version.

Thanks for helping out with this, planning to try this evening. One question, I don’t see a variables option in automation actions under the UI, I assume I simply add one action then combine everything in yaml in the UI?

Thanks again!

I assume I’m doing this wrong, but here’s what I added to the action section

I’m also confused at how HA will know which media player entity id to use as that’s not mapped in the helper list

Resulting message on save
Message malformed: expected dictionary @ data['action'][0]

I was hoping this was a simple issue of having my input_select.room_speaker_list syntax wrong or something, but it looks like this is fairly difficult to accomplish. Appreciate all the help so far!

  1. You never posted a properly formatted version of your automation so what I posted is based on a best guess of what you did post (which was a mess).
  2. What I posted is confirmed to pass ‘Check Configuration’ without errors. The error message you are getting now is because you (somehow) introduced a syntax error during the copy-paste process.

The media_player entities I used in my example come from what you had posted. If you haven’t actually defined these entities yet then you should.

I don’t use the UI’s Automation Editor because it has too many limitations; can’t help you with that.

Hmn… I prefer the UI editor cause I can edit on the fly.

What if I approach this differently, it’s not as optimized, but I can duplicate the working automation per speaker group, update the entities and then add a condition to check state of the input select?

  1. Configuration > Automations > Add Automation > Start with an Empty Automation
  2. Open the overflow menu (three vertical dots) in the extreme upper right hand corner.
  3. Select Edit as YAML
  4. Erase the default code that is displayed
  5. Paste the code I posted above
  6. On the first line, replace the hyphen in front of the word alias with a space
  7. Click Save
  8. Optionally, click the overflow menu and select Edit with UI. It will convert whatever it can into UI mode and whatever it can’t (like variables) will be left as YAML.