Automation with multiple triggers & templates - Help

Hi all,

First I need to describe what I want to achieve;
I have a Yamaha receiver and I’ve made buttons for the different sources (AV1, AV2 etc). As long as I use HA to control the receiver, it all works. The challenge rises when the ladies in the household choose to use the remote. Then I want to update the buttons in HA.

Example (image tells it all I hope):

If I press ‘Apple TV’ it turns on and change the source to AV2. The button updates.

But, if anyone use the remote, I need HA to check at all times ‘what is the current source in use and update the corresponded button with status’. If somebody press AV2 on the remote, I want ‘Apple TV’ button to be turned on.

I was planning to use an automation for this, and up to the trigger part it is easy

I use input_bolean’s to control on/off combined with switches for the buttons.

    entity_id: input_boolean.yamaha_chromecast_on_off
    entity_id: input_boolean.yamaha_appletv_on_off
    entity_id: input_boolean.yamaha_btaudio_on_off

The rule should be:
IF source == AV2 THEN turn_on input_boolean.yamaha_appletv_on_off and turn_off input_boolean.yamaha_chromecast_on_off AND input_boolean.yamaha_btaudio_on_off

IF source == AV3 THEN turn_on input_boolean.yamaha_chromecast_on_off and turn_off input_boolean.yamaha_btaudio_on_off AND input_boolean.yamaha_appletv_on_off

IF source == AV4 THEN turn_on input_boolean.yamaha_btaudio_on_off and turn_off input_boolean.yamaha_chromecast_on_off AND input_boolean.yamaha_appletv_on_off

This worked in template editor to check what is concidered TRUE:

    - platform: template
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' }}"
    - platform: template
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV3' }}"
    - platform: template
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV4' }}"

So automation should start with:

- alias: Yamaha Status sjekk
  trigger:
    - platform: template
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' }}"
    - platform: template
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV3' }}"
    - platform: template
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV4' }}"

From here I’m quite stuck :thinking:

Is this possible to resolve within a single automation, perhaps using service_template and data_template ??

I would do it in a completely different way: create a switch for each option using template platform

2 Likes

As I said, as long as I use Home Assistant (and Google Home), it all works well. I do use switch templates for this.

The problem rises when somebody use the traditional remote to change source, then HA will not update to the correct source and ‘light up the corresponding button’.

Sure, I understand your issue. You can avoid a need to manually update input_booleans by using 3 template switches. They will be automatically updated to match state of media_player.

Example:

switch:
  - platform: template
    switches:
      yamaha_AV2:
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' }}"
        turn_on:
          service: media_player.select_source
          data:
            entity_id: media_player.yamaha
            source: AV2
        turn_off:
          service: media_player.turn_off
          data:
            entity_id: media_player.yamaha
1 Like

Ahh, create these in addition to the existing switches?

Here is an example of one switch;

  - platform: template
    switches:
      appletv_on_off:
        friendly_name: Apple TV 4K
        value_template: "{{ is_state('input_boolean.yamaha_appletv_on_off', 'on') }}"
        turn_on:
          - service: input_boolean.turn_off
            entity_id: input_boolean.yamaha_chromecast_on_off
          - service: input_boolean.turn_off
            entity_id: input_boolean.yamaha_btaudio_on_off
          - service: input_boolean.turn_on
            entity_id: input_boolean.yamaha_appletv_on_off
          - service: automation.trigger
            entity_id: automation.70_yamaha_skrur_over_til_apple_tv
        turn_off:
          - service: automation.trigger
            entity_id: automation.71_yamaha_skrur_over_til_tv
          - service: input_boolean.turn_off
            entity_id: input_boolean.yamaha_appletv_on_off

No, forget the input booleans. Replace them with template switches. You have the feedback available to know their state if changed externally to home assistant.

1 Like

Exactly. Additionally it will probably make dedicated input_booleans for media_player sources unnecessary (input_boolean.yamaha_chromecast_on_off, input_boolean.yamaha_btaudio_on_off, input_boolean.yamaha_appletv_on_off), which will also simplify other scripts and automations, etc.

1 Like

@3_14 & @tom_l THANK YOU !

I had really dug myself deep into complicated s**t, but the solution was so simple :slight_smile:

1 Like

@3_14 & @tom_l

I got a little issue… With automation I could use condition to check if the amplifier was on, so I tried to extend the value_template to this;

value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' and is_state('media_player.yamaha', 'on') }}"

I tested it in template editor and could see that it should work, meaning - only turn on if both are true (are source AV2 AND amplifier ON).

Though, I can press the Apple TV button and the signal appear on TV (button state remain off), even if the amplifier are off. I double checked it by looking at the state of media_player.yamaha confirming it was off.

Any idea why the rule fails ?

  - platform: template
    switches:
      yamaha_appletv_av2:
        friendly_name: Apple TV 4K
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' and is_state('media_player.yamaha', 'on') }}"
        turn_on:
          service: media_player.select_source
          data:
            entity_id: media_player.yamaha
            source: AV2
        turn_off:
          service: media_player.select_source
          data:
            entity_id: media_player.yamaha
            source: AV1

Check the actual state of media_player.yamaha in the developer tools states menu. It may be something like playing and/or idle rather than on.

In which case:

value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' and states('media_player.yamaha') in ['idle', 'playing'] }}"

As you can see, it is off

You’re not checking for off. You’re checking for on. What state is it when on?

As I said, it somehow run the turn_on but of course, the receiver doesn’t turn on physically (and the media_player.yamaha remain off). The reason for Apple TV show at the TV is because the receiver has a pass-through function I cannot disable, that is why I wanted the condition before running turn_on: If media_player.yamaha == off don’t turn_on AV2.

image

Can the availability_template be the solution?

       availability_template: >-
         {%- if is_state('media_player.yamaha', 'on') %}

EDIT: Now it seems to work fine :slight_smile:

Here is one switch as example for others to learn;

  - platform: template
    switches:
      yamaha_appletv_av2:
        friendly_name: Apple TV 4K
        availability_template: "{{ is_state('media_player.yamaha', 'on') }}"
        value_template: "{{ (state_attr('media_player.yamaha', 'source')) == 'AV2' }}"
        turn_on:
          service: media_player.select_source
          data:
            entity_id: media_player.yamaha
            source: AV2
        turn_off:
          service: media_player.select_source
          data:
            entity_id: media_player.yamaha
            source: AV1
1 Like