How to automate/fix this input_select template

Hi, @anon43302295

I have a set of switches i like to command. On, Off, State. for each command i have separate scripts, or even shell_commands and rest_commands available.

Id like to compress that in the Frontend by having 2 input selects,1 for selecting the Switch, the second for selecting the Command. Press activate and all set. Exactly like with the hue_scenes, which i based the below package on.

I need a template of course for the final command, filling in the selected state from the input selectors. but need some help there…

this is what ive got so far, all in one package:

##########################################################################################
# Small package for selecting and activating Switches: On. Off, State
# 17103018
# MtHvdB
##########################################################################################

homeassistant:
  customize:
    script.run_switch_command:
      icon: mdi:power
      show_last_changed: true
  
    group.select_switch_command:
      control: hidden
#      hide_control: true


#### script to be adjusted

script:
  run_switch_command:
    alias: Run switch command
    sequence:
      - service: xxx.xxx.xxx?
        data_template:
          switch_name: '{{ states.input_select.switch.state }}'
          command_name: '{{ states.input_select.switch_command.state }}'

### Automation to be added?? Is that even necessary?


automation:
  select_command_switches:
    alias: Select and command switches
    id: 
    trigger:
    condition:
    action:

group:
  select_switch_command:
    name: Hue Light Control
    icon: mdi:spotlight
    entities:
      - input_select.switch
      - input_select.switch_command
      - script.run_switch_command

input_select:
  switch:
    name: Select Switches
    icon: mdi:toggle-switch
    options:
      - Select
      - Audio Auditorium
      - Tv Auditorium
      - Espresso Keuken
      - Tv Library
      - Kantoor
      - Audio Gym
      - Tester
      - Dorm
      - Boiler Bijkeuken
      - Quooker Keuken
    initial: Select

  switch_command:
    name: Select Switch Command
    icon: mdi:toggle-switch-off
    options:
      - 'On'
      - 'Off'
      - 'State'
    initial: 'Off' 

the scripts i have available are in this form for all of the mentioned switches:

audio_auditorium_force_off:
  alias: "Audio Auditorium Off"
  sequence:
    - service: switch.turn_off
      data:
        entity_id: switch.sw_audio_auditorium_cl

audio_auditorium_force_on:
  alias: "Audio Auditorium On"
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.sw_audio_auditorium_cl

audio_auditorium_state:
  alias: "Audio Auditorium State"
  sequence:
    - service: rest_command.rc_audio_auditorium_state

or the shell_commands:

sc_audio_auditorium_on: >-
    curl -X POST -d '{"seq":1, "method":"object_prop_set", "arguments":{"oid":"0e56e15c", "prop":"command", "value":"on"}}' http://192.168.xxx.xxx/iungo/api_request

sc_audio_auditorium_off: >-
    curl -X POST -d '{"seq":1, "method":"object_prop_set", "arguments":{"oid":"0e56e15c", "prop":"command", "value":"off"}}' http://192.168.xxx.xxx/iungo/api_request

sc_audio_auditorium_state: >-
    curl -X POST -d '{"seq":1, "method":"object_prop_get", "arguments":{"oid":"0e56e15c", "prop":"state"}}' http://192.168.xxx.xxx/iungo/api_request

could i create a template that uses something like this:

script.{{states.input_select.switch.state}}_force_{{states.input_select.switch_command.state}}

or the equivalent with the shell_commands?
Also, do i need an extra automation to do this?

this is what is showing now, so you get the idea:

42

just need to tie together in the backend :wink:
Please have a look?
Cheers, and thanks,
Marius

I think you are over complicating this in your head. You are very close, you just need a service_template and a entity id template in your script:

script:
  run_switch_command:
    alias: Run switch command
    sequence:
      - service: script.turn_on
        data_template: 
           entity_id: >
             {% set on_dict ={'Audio Auditorium':'script.audio_auditorium_force_on'} %}
             {% set off_dict = {'Audio Auditorium':'script.audio_auditorium_force_on'} %}
             {% set script_dict = {'Audio Auditorium':'script.audio_auditorium_state'} %}
             {% if is_state('input_select.switch_command', 'On') %}
             {{ on_dict[states.input_select.switch.state] }}
             {% elif is_state('input_select.switch_command', 'Off') %}
             {{ off_dict[states.input_select.switch.state] }}
             {% elif is_state('input_select.switch_command', 'State') %}
             {{ state_dict[states.input_select.switch.state] }}
             {% endif %}

You may want to build some safety and not have the word Select in your input_select configs:

input_select:
  switch:
    name: Select Switches
    icon: mdi:toggle-switch
    options:
      - Audio Auditorium
      - Tv Auditorium
      - Espresso Keuken
      - Tv Library
      - Kantoor
      - Audio Gym
      - Tester
      - Dorm
      - Boiler Bijkeuken
      - Quooker Keuken
    initial: Audio Auditorium

ok thanks!
Ill check that.
the thing is, id love to have just one automation if possible, not one for every switch.
I have something like that, and is takes a whole page of configuration…
Moreover, that hard-codes all switches in the automation, while the template would enable me to just be safe on the side, and if anything changes on the switches side, no braking would occur.

ive seen template where {{somevalue}} was concatenated to another value, hoped this would be possible here too?
Cheers,
Marius

You are going to need a script for every option because you cannot use empty entity_id templates.

mixing this:

  sequence:
    - service: rest_command.rc_audio_auditorium_state

and this:

sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.sw_audio_auditorium_cl

is not possible without getting errors/warnings because one requires an entity_id the other does not.

and if id only want to do it for switching on and off? Is my most wanted setup anyway.

use a service_template and an entity_id template:

script:
  run_switch_command:
    alias: Run switch command
    sequence:
      - service_template: >
          {% set sdict = {'On':'switch.turn_on','Off':'switch.turn_off'}
          {{ sdict[states.input_select.switch.state] }}
        data_template: 
           entity_id: >
             {% set edict= {'Audio Auditorium':'switch.sw_audio_auditorium_cl'} %}
             {{ edict[states.input_select.switch_command.state] }}

delete all the other crap except your two input_selects and that script.

1 Like

lol. thanks.

but this would still need a script per switch wouldn’t it?

please let me get back on the empty entity_id issue, im not sure that is completely necessary.

for all on, off (and state commands) i have identically setup scripts.

so run {switch name}_{command} should work for all. Id just need to be able to run a script as service, with a template creating the correct script name and selecting that.

selecting Off and audio auditorium would run script.audio_auditorium_off
selecting State and Espresso_keuken would run script.espresso_keuken_state
selecting On and tv_library would run script.tv_library_on

etc etc

Nope, you only need the main script.

i don’t understand then, because it contains Audio_auditorium…??

data_template: 
           entity_id: >
             {% set edict= {'Audio Auditorium':'switch.sw_audio_auditorium_cl'} %}

yeah that is a dictionary. Dictionarys have a key and a value. The key is linked to the value. So when You access the value, you provide the key.

in this case, your key comes from your input_select, ‘Audio Auditorium’. So if i were to request it:

edict[‘Audio Auditorium’] the returned value is ‘switch.sw_audio_auditorium_cl’

Notice the colon between ‘Audio Auditorium’:‘switch.sw_audio_auditorium_cl’, that makes it 1 item in the dictionary

to add items you place a comma:

set dictionary = {‘key1’:‘value1’, ‘key2’:‘value2’}

and i woud have to add that for all other switches, correct?

sorry, cross-posted i guess.

the thing is, I wanted to automate that process also. using a template of things…instead of completely creating that list manually, thus making it more difficult to maintain, and less flexible/futureproof/expandable…

please forget about the switch command, just use the script option i showed above.

create script.{name}_{command} from input_select.name and input_select.command.

yes

side note: there is a 10 character limit so i cannot just say yes to you in this forum.

that is possible but all your switch names need to follow that convention. Otherwise you need to create a dictionary that links the selection to the device

they do already, have them ready for use

please let me know how, id be most grateful…

so they all have switch.sw_{name}_cl?

yes, if have scripts for all, using the naming convention:

script.{name}_{command}

these scripts call the switches in their turn

script:
  run_switch_command:
    alias: Run switch command
    sequence:
      - service_template: >
          {% set sdict = {'On':'switch.turn_on','Off':'switch.turn_off'}
          {{ sdict[states.input_select.switch.state] }}
        data_template: 
           entity_id: >
             {% set name = states.input_select.switch.state | replace(' ','_') %}
             switch.sw_{{name.lower()}}_cl

You still won’t need scripts, just your 2 dropdowns and this script

this is getting there i thing, magic.
still please have it use the scripts in the data_template, using both name and command? that way i could use all i need (maybe even including the State).

entity_id would need to be script.{{name}}_{{command}}

If you try and include the state, then you need to create 3 scripts, on, off, state for each device. There won’t be a way around that. Then you only need an entity_id template for the script. If you followed the same naming convection it would be script.sc_{{name.lower()}}_{{command.lower()}} and your service woudn’t need to be a template, it would just be script.turn_on