Automation trigger on attribute change

I want an automation that triggers when I switch source on the receiver.
When you switch between two sources it’s not uncommon that the volume is too high for the source you switch too.

What can I do to trigger on the attribute source change?

  trigger:
  - platform: template
    value_template: '{{ states.media_player.yamaha_receiver.attributes.source }}'
  condition:
  - condition: state
    entity_id: media_player.yamaha_receiver
    state: 'On'
  - condition: template
    value_template: '{{ states.media_player.yamaha_receiver.attributes.volume_level
      > 0.72 }}'
  action:
  - data:
      volume_level: 0.65
    entity_id: media_player.yamaha_receiver
    service: media_player.volume_set

The attribute source has the following options so making one automation per change is not really ideal:
source_list: AUDIO, AV1, AV2, AV3, AV4, AV5, AV6, AirPlay, HDMI1, HDMI2, HDMI3, HDMI4, NET RADIO, SERVER, TUNER, USB, V-AUX, iPod (USB)

You can use the ‘or’ syntax to add multiple templates to the trigger. I’m not sure quite what your attributes looks like but you can do something like this for the value_template:

{% if
(is_state_attr('media_player.yamaha_reciever', 'source', 'AUDIO') or is_state_attr('media_player.yamaha_reciever', 'source', 'AV1') or is_state_attr('media_player.yamaha_reciever', 'source', 'AV2')) %}
true
{% endif %}

Obviously adding all your sources. That means you can add it all to one automation.

1 Like

That wouldn’t work. A template trigger will only fire (after the first time) when it changes back to False and then back to True. If the template remains True it won’t trigger.

@Hellis81 what you can do is use a state trigger without specifying to or from, and then use a template condition that checks if the source attribute has changed:

  trigger:
  - platform: state
    entity_id: media_player.yamaha_receiver
  condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state == 'on' and
         trigger.from_state.attributes.source !=
         trigger.to_state.attributes.source and
         trigger.to_state.attributes.volume_level > 0.72 }}
  action:
  - data:
      volume_level: 0.65
    entity_id: media_player.yamaha_receiver
    service: media_player.volume_set

You could even remove that last part of the condition (i.e., checking the volume), and use a template in the service call to set the volume according to the new source selection.

7 Likes

@EverythingSmartHome I couldn’t get that to work.

@pnbruckner Thank you that worked!

1 Like

I’m trying to build a custom led light show when my hockey team scores and I’m using the ESPN team tracker integration but the only thing that can call triggers is attribute changes to the score. How would I use the code above to incorporate that? Some sort of now() +1?

The discussion above is extremely old. I doubt there is anything there that would be useful given the way HA works today.

Automations can now be triggered by changes in entity attributes. I suggest you check out the standard documentation. If that doesn’t help, then open a new topic to ask for help.

Good luck!