Help on multiple entity-id automation

Try to change the boolean by select different input option, here is the code:

- id: harmony
  alias: Harmony
  trigger:
    platform: state
    entity_id: input_select.harmonyscene
    from: 'Select'
  action:
    - service: input_boolean.turn_on
      data_template:  
        entity_id: >
          {% if is_state("input_select.harmonyscene", "Music") %}
            input_boolean.remote_harmony_smartmusic
          {% elif is_state("input_select.harmonyscene", "Apple TV") %}
            input_boolean.remote_harmony_appletv
          {% elif is_state("input_select.harmonyscene", "TV box") %}
            input_boolean.remote_harmony_tvbox
          {% elif is_state("input_select.harmonyscene", "PS4") %}
            input_boolean.remote_harmony_ps4
          {% endif %}

Then I get this error every time once I select an option. [ Invalid service data for input_boolean.turn_on: Entity ID is an invalid entity id for dictionary value @ data[‘entity_id’]. Got ’ ’ ]

Are these two lines on two rows in your code too? I’m thinking that yaml is very picky on indentations, maybe you should try to put all of this one one single line.

In your data_template, you have no “else” section. So if the state of input_select.harmonyscene is neither Music, Apple TV, TV Box nor PS4 the data_template returns an empty entity_id.

You should check the state by adding a log statement or prevent the action by adding an appropriate condition.

@bonterra Tried but no luck.
@m0wlheld I don’t think it’s an ‘else’ issue, no matter which option I select, same error popup. And also I tried to add else part, but still…

First off, I’d get rid of the from: ‘Select’ in your trigger. Because if you’re switching from one activity to another, it won’t trigger. Second, you should use >- to take away spaces and line breaks. But also putting each if on the same line will probably solve your problem. Try this:

- id: harmony
  alias: Harmony
  trigger:
    platform: state
    entity_id: input_select.harmonyscene
  action:
    - service: input_boolean.turn_on
      data_template:  
        entity_id: >-
          {% if is_state("input_select.harmonyscene", "Music") %}input_boolean.remote_harmony_smartmusic
          {% elif is_state("input_select.harmonyscene", "Apple TV") %}input_boolean.remote_harmony_appletv
          {% elif is_state("input_select.harmonyscene", "TV box") %}input_boolean.remote_harmony_tvbox
          {% elif is_state("input_select.harmonyscene", "PS4") %}input_boolean.remote_harmony_ps4
          {% endif %}

Thanks bro, will update later!
@Jer78 Still no luck… same error there.

Can you paste your input_select config? Could it be an issue with the options not matching?
We can also try another method as below…

- id: harmony
  alias: Harmony
  trigger:
    platform: state
    entity_id: input_select.harmonyscene
  action:
    - service: input_boolean.turn_on
      data_template:  
        entity_id: >-
          {% set harmony = states.input_select.harmonyscene.state %}
          {% if harmony == "Music" %}input_boolean.remote_harmony_smartmusic
          {% elif harmony == "Apple TV" %}input_boolean.remote_harmony_appletv
          {% elif harmony == "TV box" %}input_boolean.remote_harmony_tvbox
          {% elif harmony == "PS4" %}input_boolean.remote_harmony_ps4
          {% else %}input_boolean.dummy
          {% endif %}

@Jer78 Here is my config, and the activity part works great.

- id: harmony
  alias: Harmony
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.harmonyscene
    from: 'Select'
  action:
    - service: remote.turn_on
      entity_id: remote.harmony_hub
      data_template:  
        activity: >
          {% if is_state("input_select.harmonyscene", "Music") %}
            28186269
          {% elif is_state("input_select.harmonyscene", "Apple TV") %}
            28185952
          {% elif is_state("input_select.harmonyscene", "TV box") %}
            28186422
          {% elif is_state("input_select.harmonyscene", "PS4") %}
            28186383
          {% else %}
          {% endif %}
    - service: input_select.select_option
      entity_id: input_select.harmonyscene
      data_template:
        option: "Select"
    - service: input_boolean.turn_on
      data_template:  
        entity_id: >-
          {% if is_state("input_select.harmonyscene", "Music") %}input_boolean.remote_harmony_smartmusic
          {% elif is_state("input_select.harmonyscene", "Apple TV") %}input_boolean.remote_harmony_appletv
          {% elif is_state("input_select.harmonyscene", "TV box") %}input_boolean.remote_harmony_tvbox
          {% elif is_state("input_select.harmonyscene", "PS4") %}input_boolean.remote_harmony_ps4
          {% endif %}

Ok I see what’s happening here. You are resetting the input_select back to “Select”, then trying to read the input_select. You need to reverse the order. Make it the last thing you do, like this…

- id: harmony
  alias: Harmony
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.harmonyscene
    from: 'Select'
  action:
    - service: remote.turn_on
      entity_id: remote.harmony_hub
      data_template:  
        activity: >
          {% if is_state("input_select.harmonyscene", "Music") %}
            28186269
          {% elif is_state("input_select.harmonyscene", "Apple TV") %}
            28185952
          {% elif is_state("input_select.harmonyscene", "TV box") %}
            28186422
          {% elif is_state("input_select.harmonyscene", "PS4") %}
            28186383
          {% else %}
          {% endif %}
    - service: input_boolean.turn_on
      data_template:  
        entity_id: >-
          {% if is_state("input_select.harmonyscene", "Music") %}input_boolean.remote_harmony_smartmusic
          {% elif is_state("input_select.harmonyscene", "Apple TV") %}input_boolean.remote_harmony_appletv
          {% elif is_state("input_select.harmonyscene", "TV box") %}input_boolean.remote_harmony_tvbox
          {% elif is_state("input_select.harmonyscene", "PS4") %}input_boolean.remote_harmony_ps4
          {% endif %}
    - service: input_select.select_option
      entity_id: input_select.harmonyscene
      data:
        option: "Select"

Works like a charm. Thank you so much, @Jer78

This is an interesting work around to deal with the harmony auto shutting off the other activities when a different one is activated.

Just curious, what happens when you switch to Music, then switch to Apple TV, then switch back to Music? From looking at your code, it doesn’t handle this and you may into issues. I would expect your input_booleans over time to all be turned on. You may want to swap those booleans to template switches. The beauty of switch templates is that the state of the switch is independent from the user interactions. Basically, Your hardware can turn off the switch without triggering the ‘turn_off’ home assistant.

switch:
  - platform: template
    switches:
      remote_harmony_smartmusic:
        value_template: "{{ is_state(('input_select.harmonyscene', 'Music') }}"
        turn_on:
          service: remote.turn_on
          entity_id: remote.harmony_hub
          data:
            activity: 28186269
        turn_off:
          service: remote.turn_off
          entity_id: remote.harmony_hub
          data:
            activity: -1

Then you don’t need any automation and you can just click the buttons in the interface instead of using a dropdown list.

So how this works is the state of the switch is changed by the value_template. When that changes, it does not trigger the on/off in home assistant. So you don’t need to build an automation to turn_on/off like you do with input booleans. Super helpful.

@sephrioth Actually, I forgot to mention that earlier, @petro is correct, instead of input_booleans, use template switches. Otherwise, you’d need to add another service to turn off all other input_booleans which activities aren’t running. Much simplier using switches though.

@petro @Jer78
I tried Template Switch in the very beginning, but the issue is the switch CAN NOT keep the states, once I turn on a template switch, it will jump to off in seconds automatically. Then I did some google work and found someone said I should use a boolean to save the current state…

BTW, I have soma additional code to switch between activities…

@sephrioth Use this instead. I just looked up mine and it’s how I do it.

switch:
  - platform: template
    switches:
      remote_harmony_smartmusic:
        value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Music') }}"
        turn_on:
          service: remote.turn_on
          entity_id: remote.harmony_hub
          data:
            activity: 28186269
        turn_off:
          service: remote.turn_off
          entity_id: remote.harmony_hub
          data:
            activity: -1
1 Like

But what will happen if I directly switch to another activity? The current switch is still on? But the harmony only has one running activity, you switch to another and the previous one goes off.

when you switch to another activity, the template switch will turn off because the current_activity is not equal to ‘Music’

1 Like

Got it, will try later. Thank you guys! @Jer78 @petro

1 Like

@Jer78 @petro Switches work fine, couldn’t be better~

2 Likes