Call a rest_command based on trigger name and value

Hello, the general goal is in the title.

I want to turn on and off certain services (squeezebox, airplay, spotify) on each of my 3 music player.
I want it to be user friendly so that also my wife knows how to do it.

To do that, I have defined as many input_boolean as needed

and I created as many rest_command as needed, two for each variable

input_boolean:
  hfb_sala_spotify:
  hfb_cucina_spotify:
  hfb_bimbi_spotify:

  hfb_sala_airplay:

and many more

I need to write a single automation that calls the appropriate method based on the trigger.

If the trigger is hfb_sala_spotify, and it was turned on, I want to call rest_command.hfb_sala_airplay_on

This is not working

  trigger:
  - entity_id: input_boolean.hfb_sala_airplay
    platform: state
  - entity_id: input_boolean.hfb_sala_spotify
    platform: state
  - entity_id: input_boolean.hfb_cucina_spotify
    platform: state
  condition: []
    action:
  - service_template: >
      rest_command.{{trigger.entity_id.split(".") [1]}}_{{trigger.to_state.state}}
   

but if i can get the right notification if I change the action

action:
  - service: notify.mobile_app_sm_n960f
    data_template:
      message: >
        rest_command.{{trigger.entity_id.split(".") [1]}}_{{trigger.to_state.state}}

Any clue is welcome

The indentation in your automatin is incorrect. Also, it can be simplified a bit:

  trigger:
  - entity_id:
    - input_boolean.hfb_sala_airplay
    - input_boolean.hfb_sala_spotify
    - input_boolean.hfb_cucina_spotify
    platform: state
  condition: []
  action:
  - service_template: >
      rest_command.{{trigger.entity_id.split(".") [1]}}_{{trigger.to_state.state}}
1 Like

Thanks a lot, I will give it a shot. I used a mix of text editor and visual editor.

I will get back to you soon.

@pnbruckner,
I just started with templates and I thought my problem was there.
Thank you for spotting the problem.

1 Like