Can I trigger an automation based on a media_player’s source?

I’m trying to set an automation based on my TV’s source (ie make things happen if it’s set to HDMI1 vs HDMI2, etc), but I”m not seeing a way to do it.

The TV has a media_player entry, and its Attributes includes a source_list that I’ve been able to use as targets in automations’ actions without an issue. But when I try to Add Trigger, I don’t see any options for media_player and the TV’s “device” entry doesn’t include triggers for input state.

Is there something I’m missing? Does this need to be done by editing automations.yaml instead of through the UI interface? If neither is an option, is there a workaround that someone’s found?

You'll need to use a state trigger

trigger: state
entity_id:
  - media_player.xxxx
attribute: source
to:
  - HDMI 1

1 Like

I'm just guessing because you didn't share what you're seeing. However, chances are you're using a device trigger, so you only get a limited subset of options when using it.

Switch to using a state trigger & use the attributes section to set the source you want to trigger off.

EDIT: Ninja'd by @LiQuid_cOOled :ninja:

Thanks! Yep, I’d tried device triggers as well as searching the available triggers for “source” and the names of my sources, but hadn’t found anything. In the UI the answer was Add Trigger > Entity > State, then choosing my TV’s media player and (weirdly) adding a custom attribute named “source”. Once that was done, the “to” and “from” drop-down lists populated with the TV’s sources.

I’m not shocked that I didn’t know how to use the right triggers (I’ve mostly been using MQTT signals from my SofaBaton remote), but I am legitimately shocked that “sources” needs to be a custom entry and couldn’t be searched for!

Thanks for the quick fix, guys!

You may want to add the following to avoid issues when devices are powered on

trigger: state
entity_id:
  - media_player.xxxx
attribute: source
to:
  - HDMI 1
not_from:
  - unknown
  - unavailable

Edit

As @ShadowFist mentioned, you may need to add a power condition based on the media device you are using

Example

conditions:
  - condition: media_player.is_on
    target:
      entity_id: media_player.monitor
    options:
      behavior: any
      for:
        hours: 0
        minutes: 0
        seconds: 1

The TV needs to be on to get all the attributes populated in the automation dropdown.

Just checked on mine & the only attribute I saw while it was off was Assumed State. Turned on the TV & now I see all these:

Also, I suggest you follow the excellent provided by @LiQuid_cOOled above & add the not_from to exclude unknown & unavailable triggers. You'll have to switch to Yaml mode to do it, but it's quite straightforward to add those 3 lines.

Why would you want to do that?
IF the input is set to HDMI1 you want to do something, eg set the volume to 0.1 to prevent too loud sound. It doesn't matter if this happens at startup or not I would think. As you can not be sure what the status was prior to startup it's best to perform the action. Not?

Anyhow I would do a trigger on just the state change and then choose based on the current source which action is required.

You are assuming all media players interact and report their status with HA the same way.

Then include the media device’s system settings…

Imagine you're in the middle of a movie.

Your router restarts due to some software update or a glitch. Or maybe you restart your HA server as part of an update while someone else is watching a movie. Or maybe your media player simply had a hiccup

All scenarios above will cause the media player to go briefly unknown or unavailable, triggering the automation & setting the volume to 0.1.

See how annoying that might get? Now you're left wondering what happened to the sound, fixing it via the remote & rewinding back to the part you missed.

1 Like

In my case I’m having HA change my remote control’s mode to control a specific device - it now changes it to AppleTV mode, Blu Ray mode, PlayStation mode, and Nintendo Switch mode even if another remote or a video game controller was used to turn the respective device on.

I don’t think there are cases in which a device could power on and cause issues for me given my use case, but I could probably be persuaded otherwise!

Suppose you want a light to turn on when your LG TV switches to HDMI 1, but only while the TV is already powered on. How would you prevent the Home Assistant automation from triggering when the TV source changes from null, unknown, or unavailable (because the TV was off and not reporting a source) directly to HDMI 1 during power-on?

1 Like

It's just adding 4 words in an automation which would save you the headache of troubleshooting if anything ever goes wrong. That's almost 20x less words than your latest post.

Just go ahead and add them - your future self will thank you.

That’s fair, but in my case if the TV is off and is turned on to HDMI1 I absolutely want the event to trigger. I think my use case might have different needs, and this might not apply to me.