Run the same automation for multiple entities

Hey guys,

I have an automation to detect TV being turned on, turn the volume down to 10 and then open an app.
Right now I have an automation for each TV; I’m wanting to combine this into a single automation if possible.

I can’t just select the entities of all the TV’s in the below example, as then if any TV is turned on, all the other TV’s that are on, will suddenly trigger the automation.

alias: Source - Change Lounge TV
description: ""
trigger:
  - platform: device
    device_id: 91fae41481f566942434e33da158021f
    domain: media_player
    entity_id: media_player.lounge_tv
    type: turned_on
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: media_player.volume_set
    data:
      volume_level: 0.1
    target:
      device_id: 91fae41481f566942434e33da158021f
  - service: media_player.select_source
    data:
      source: SmartOne IPTV
    target:
      device_id: 91fae41481f566942434e33da158021f
mode: single

Any help would be appreciated

This definitely seems doable. One way to go about this is to create a trigger for each device, and assign each one a unique trigger_id (in the UI click on the 3 dots top right of the trigger). Then you can use each trigger ID as a case in an if-then action.

Alernatively, maybe if you made each trigger_id the same as each devce_id, you could just use a template trigger variable as your device target. For example:

trigger:
  - platform: device
    device_id: abcd
    trigger_id : abcd
    domain: media_player
    entity_id: media_player.lounge_tv
    type: turned_on
    for:
      hours: 0
      minutes: 0
      seconds: 1
  - platform: device
    device_id: wxyz
    trigger_id : wxyz
    domain: media_player
    entity_id: media_player.bedroom_tv
    type: turned_on
    for:
      hours: 0
      minutes: 0
      seconds: 1

And then in your action…

- service: media_player.volume_set
    data:
      volume_level: 0.1
    target:
      device_id: {{ trigger.id }}

I’m not sure if this would work exactly but hopefully it will lead you down a useful path.

3 Likes

Thanks, I had just figured it out and came back to post a working solution, thanks so much for your help though

alias: TV Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.master_tv
      - media_player.master_tv_2
    from: "off"
    to: "on"
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: media_player.volume_set
    data:
      volume_level: 0.1
    target:
      entity_id: "{{ trigger.entity_id }}"
  - service: media_player.select_source
    data:
      source: SmartOne IPTV
    target:
      entity_id: "{{ trigger.entity_id }}"
mode: parallel
max: 4