Cleaning up an automation... has to be a better way to do this

The following automation sets the volume up on my alexa’s, makes an announcement and sets the volume back down.

I am using the Alexa Media integration installed via HACS. There is a media_player.everywhere, but in operation it caused all of my original Dots not to speak. So, I had to set volume on each device and change it again on each device.

There has to be a way to clean this up, I tried 1 media_player.volume_set with multiple entity_id’s but I could not get it formatted to pass muster on the automation verify.

Any help would be appreciated as this all slows down the entire automation.

Thanks Matt

- alias: Announce Input to all alexa
  trigger:
    - platform: state
      entity_id: input_text.alexa_tts
  action:
  - service: media_player.volume_set
    data:
      entity_id: media_player.work
      volume_level: '0.9'
  - service: media_player.volume_set
    data:
      entity_id: media_player.kids
      volume_level: '0.9'
  - service: media_player.volume_set
    data:
      entity_id: media_player.kitchen
      volume_level: '0.9'
  - service: media_player.volume_set
    data:
      entity_id: media_player.bedroom
      volume_level: '0.9'
  - service: notify.alexa_media
    data:
        target: 
          - media_player.living_room
          - media_player.work
          - media_player.kids
          - media_player.bedroom
          - media_player.echo
          - media_player.kitchen
        #title: "My title for Echo show"
        data:
          type: announce
          #method: all
        message: '{{ states.input_text.alexa_tts.state }}'
  - service: media_player.volume_set
    data:
      entity_id: media_player.work
      volume_level: '0.5'
  - service: media_player.volume_set
    data:
      entity_id: media_player.kids
      volume_level: '0.5'
  - service: media_player.volume_set
    data:
      entity_id: media_player.kitchen
      volume_level: '0.5'
  - service: media_player.volume_set
    data:
      entity_id: media_player.bedroom
      volume_level: '0.5'

How about

- service: media_player.volume_set
  data:
    volume_level: '0.9'
    entity_id:
      - media_player.living_room
      - media_player.work
      - media_player.kids
      - media_player.bedroom
      - media_player.echo
      - media_player.kitchen

cannot test it as don’t have any media players but it should accept a list.

That did it… I could not get the format nailed to save my soul earlier today.
Thanks Matt

Cool.
Just for fun, try this as well (untested but passes config checks)

- alias: Announce Input to all alexa
  trigger:
    - platform: state
      entity_id: input_text.alexa_tts
  action:
  - service: &volume_set media_player.volume_set
    data:
      volume_level: '0.9'
      <<: &entities
        entity_id: &all_players
          - media_player.living_room
          - media_player.work
          - media_player.kids
          - media_player.bedroom
          - media_player.echo
          - media_player.kitchen
  - service: notify.alexa_media
    data:
        target: *all_players
        #title: "My title for Echo show"
        data:
          type: announce
          #method: all
        message: '{{ trigger.to_state.state }}'
  - service: *volume_set
    data:
      volume_level: '0.5'
      <<: *entities

Here and here is to do that.

I tried it… It appears to set the volume the first time and then never announces and does not lower the volume. That is an interesting approach!

hm… cannot comment further but you have some fresh ideas/docs to digest :wink:

Yes, every time I try something… I learn something. Have been running this instance for years and still consider myself a newb.

That’s because it’s still changing rapidly, not surprised… “Use” != “know everything about it”. Think smartphones :wink:

1 Like

Did this ever work? As you use template for your message, it should be data_template below service.

How about this - requires a script:

script:
  set_volume_media_players:
    sequence:
      service: media_player.volume_set
      data_template:
        volume_level: "{{ volume_level }}"
        entity_id: "{{ entity_id }}"

- alias: Announce Input to all alexa
  trigger:
    - platform: state
      entity_id: input_text.alexa_tts
  action:
    - service: script.set_volume_media_players
      data:
        volume_level: '0.9'
        entity_id: &all_players
          - media_player.living_room
          - media_player.work
          - media_player.kids
          - media_player.bedroom
          - media_player.echo
          - media_player.kitchen
          
    - service: notify.alexa_media
      data_template:
          message: '{{ trigger.to_state.state }}'
          target: *all_players
          data:
            type: announce
          
    - service: script.set_volume_media_players
      data:
        volume_level: '0.5'
        entity_id: *all_players

Yes… that worked well. The automation is smaller and faster now. Have not tried the last one.

1 Like