Template usage - turn on 2 devices in one step

Hi all,

is there a way to turn on more than one device in one line for example:

        - service: homeassistant.turn_on
  data_template: 
    entity_id: >- 
      {% if is_state("input_select.test_powertoggle", "testpoweron") %}group.av_devices_power
      {% elif is_state("input_select.test_powertoggle", "testpoweron2") %}
      {% else %}
      {% endif %}

I want to turn on the group and send a command to my harmony at the same time…
so to say 2 actions in one when a certain option is chosen in an input select:

  • turn on group (switch 1 and switch 2) (entity_id:)
  • send a command through the harmony hub (command:)

Greetings and thanks in advance

 - service: homeassistant.turn_on
   data_template:
     entity_id: group.av_devices_power
 - condition: template
   value_template: "{{ is_state('input_select.test_powertoggle', 'testpoweron2') }}"
 - service: ...

daisy chain as many services as you want. Use conditions to execute or not execute everything below the condition.

1 Like

Thanks very much i will try :slight_smile:

Would it be that way ? (ignore the empty testpoweron2 option for now :)) Cause something seems to be wrong here:

    - id: test_powertoggle
  alias: test_powertoggle
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.test_powertoggle
    from: 'Select PowerSwitch'
  action:
    - service: switch.toggle
      data_template: 
        entity_id: >- 
          {% if is_state("input_select.test_powertoggle", "testpoweron") %}group.av_devices_power
    - service: remote_send_command
        entity_id: remote.harmonyhub
      data_template:
        device: 54887591
        command: L005
          {% elif is_state("input_select.test_powertoggle", "testpoweron2") %}
          {% else %}
          {% endif %}
    - service: input_select.select_option
      entity_id: input_select.test_powertoggle
      data_template:
        option: "Select PowerSwitch"

Error:
Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line 95, column 18

I have no clue what you are trying to do here. Your formatting is wrong all over the place and you have templates in the wrong spots, and your templates themselves are wrong. Have you read the documentation on templating? Are you familiar with yaml? Have you looked at Jinja2?

Are you trying to always toggle group.av_devices_power regardless of selection?

:neutral_face: Hm well i just tried to change this one which is working without any issues:

- id: harmony_power_toggle
  alias: harmony_power_toggle
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.harmony_powertoggle
    from: 'Select PowerSwitch'
  action:
    - service: remote.send_command
      entity_id: remote.harmonyhub
      data_template:
        device: 54887591
        command: >-
          {% if is_state("input_select.harmony_powertoggle", "AV Devices Power on") %}L001
          {% elif is_state("input_select.harmony_powertoggle", "AV Devices Power off") %}L005
          {% elif is_state("input_select.harmony_powertoggle", "Oppo On/Off") %}L003
          {% elif is_state("input_select.harmony_powertoggle", "Hometheater On/Off") %}L004
          {% elif is_state("input_select.harmony_powertoggle", "Network On/Off") %}L002
          {% else %}
          {% endif %}
    - service: input_select.select_option
      entity_id: input_select.harmony_powertoggle
      data_template:
        option: "Select PowerSwitch"

But now i need to use different devices. When the input select changes not only “L003” should be send via the harmony but the group of av switches should be toggled or turned on as well for example.

Probably i was a little bit confusing … i know that my example is wrong - sorry :no_mouth:

Ok i think i got it:

> - id: test_powertoggle
>   alias: test_powertoggle
>   hide_entity: True
>   trigger:
>     platform: state
>     entity_id: input_select.test_powertoggle
>     from: 'Select PowerSwitch'
>   action:
>     - service: switch.turn_on
>       data_template: 
>         entity_id: >- 
>           {% if is_state("input_select.test_powertoggle", "testpoweron") %}group.av_devices_power
>           {% elif is_state("input_select.test_powertoggle", "testpoweroff") %}
>           {% else %}
>           {% endif %}
>     - service: input_boolean.turn_on 
>       data_template:
>         entity_id: >-
>           {% if is_state("input_select.test_powertoggle", "testpoweron") %}input_boolean.test_powertoggle_boolean
>           {% elif is_state("input_select.test_powertoggle", "testpoweroff") %}
>           {% else %}
>           {% endif %}		
>     - service: input_select.select_option
>       entity_id: input_select.test_powertoggle
  data_template:
>       data_template:
>         option: "Select PowerSwitch"

And i think i can/need to work with conditions or a third service call (switch.turn_off) if i want to turn the group av_device and the boolean off when testpoweroff is chosen right?:no_mouth:

You will get errors if your entity id is unpopulated when executing services. Currently you only populate an entity id if the state of your input_select is ‘testpoweron’, that means if you select ‘testpoweroff’ you will get errors related to entity_id’s in your first 2 services. You should separate this into 2 scripts and call each script if you always want the power off to perform different actions.

For reference, this is what is bad about your 2 services:

entity_id: >
  {% if is_state("input_select.test_powertoggle", "testpoweron") %}
    group.av_devices_power
  {% elif is_state("input_select.test_powertoggle", "testpoweroff") %}
    # <--- NO ENTITY_ID
  {% else %}
    # <--- NO ENTITY_ID
  {% endif %}

I know “testpoweroff” will give me errors its just a test so far thats why its still empty :slight_smile:
But thanks for your input!!

My question is (sorry for beeing that unclear) if i need another section for powering off (Section 2) or if i could call the service switch.turn_off within Section 1 somehow…

Section 1 Poweron:

- service: switch.turn_on
  data_template: 
    entity_id: >- 
      {% if is_state("input_select.test_powertoggle", "testpoweron") %}group.av_devices_power
      {% elif is_state("input_select.test_powertoggle", "testpoweroff") %}
      {% else %}
      {% endif %}

Section 2 Poweroff:

- service: switch.turn_off
  data_template: 
    entity_id: >- 
      {% if is_state("input_select.test_powertoggle", "testpoweroff") %}group.av_devices_power
      {% else %}
      {% endif %}

I guess i will need a section for every different service i have to call.
But the possibility of using different scripts would possibly make it “cleaner”.

Use a service_template. Also, you should swap switch.turn_on for homeassistant because switch may not work on all groups. homeassistant will.

- service_template: >
    {% if is_state("input_select.test_powertoggle", "testpoweron") %}
      homeassistant.turn_on
    {% elif is_state("input_select.test_powertoggle", "testpoweroff") %}
      homeassistant.turn_off
    {% else %}
      homeassistant.turn_off
    {% endif %}

Aaaaaaah ok ok i got it!! Thank you :grinning: Learning by doing :yum:

But wouldn`t it be easier to use different scripts and call the script within the automation as you proposed earlier (ignore the boolean)?

Like:

av_devices_poweron_script:
  alias: av_devices_poweron_script
  sequence:
    - service: switch.turn_on
      data:
        entity_id: group.av_devices_power
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.test_powertoggle_boolean

I suggest you use a good naming convention for your input select. Example:

input_select:
  living_room_preset:
    options:
      - Select Power Switch
      - Test Power On
      - Test Power Off
      - Living Room Power On
      - Living Room Power Off

Then for your automation, you can simplify your templates:

 - id: test_powertoggle
   alias: test_powertoggle
   hide_entity: True
   trigger:
     platform: state
     entity_id: input_select.test_powertoggle
     from: 'Select Power Switch'
   condition:
     - condition: template
       value_template: >
         {{ not is_state('input_select.test_powertoggle', 'Select Power Switch') }}
   action:
     - service_template: >
          {% if 'On' in states('input_select.test_powertoggle') %}
            homeassistant.turn_on
          {% else %}
            homeassistant.turn_off
          {% endif %}
       data_template: 
         entity_id: >- 
           {% if "Test Power" in states("input_select.test_powertoggle") %}
             group.av_devices_power
           {% elif "Living Room" in states("input_select.test_powertoggle") %}
             group.living_room
           {% else %}
             group.living_room
           {% endif %}
     - service_template: >
          {% if 'On' in states('input_select.test_powertoggle') %}
            input_boolean.turn_on
          {% else %}
            input_boolean.turn_off
          {% endif %}
       data_template:
         entity_id: >-
           {% if "Test Power" in states("input_select.test_powertoggle") %}
             input_select.test_powertoggle
           {% elif "Living Room" in states("input_select.test_powertoggle") %}
             input_boolean.living_room
           {% else %}
           {% endif %}		
     - service: input_select.select_option
       entity_id: input_select.test_powertoggle
       data:
         option: "Select Power Switch"
1 Like

Yes, scripts would also do the job. You have lots of options.

Yea i will do so! These names are just for quick and dirty testing thats why :sunglasses:
Thank you very much for you input… helped me a lot understanding stuff!

1 Like