In one of my last posts, I showed how to automatically stop playback on Channels when your TV turns off.
If you have multiple instances of Channels, you’d have to create that automation multiple times. Here’s how you can create an automation for every TV in your house with a single automation.
automation:
- alias: Turn off Channels when TV turns off
trigger:
platform: state
entity_id: media_player.kitchen_tv, media_player.family_room_tv, media_player.bedroom_tv, media_player.office_tv, media_player.desk_tv
to: 'off'
action:
- service: media_player.media_stop
data_template:
entity_id: "{{ trigger.entity_id.replace('_tv', '_channels') }}"
There are a couple of tricks here:
- Add multiple
entity_ids to the trigger so it watches for all of your TVs. - Use a template to define the
entity_idthat should be used when calling the service. - Use normalized names that let you use this template.
Data templates let you use variables and process them for output into the YAML. This lets you fudge some things to work out exactly how you want.
As you can see, my TV entity_ids are named with this format: media_player.family_room_tv and my Channels entity_ids use the format media_player.family_room_channels. Because of this, we can use a simple replace method in the template to swap out _tv for _channels
This gives us an automation that listens to multiple TVs, uses their entity_id in the data template to get changed to the entity_id of its corresponding Channels app to tell it to stop playback.
I hope this helps clean up your automations!