I have a script to setup input_select menus… It works as is but if I want to reuse as a function it does not work… It should be exactly the same thing…
this works:
group_entities = hass.states.get('group.all_switches').attributes['entity_id']
# group_entities = ["switch.switch1","switch.sw2","switch.light1","switch.sonoff2,switch.yellow_room","switch.kmc_a","switch.kmc_b","switch.kmc_c"]
all_switches = []
for e in group_entities:
if "kmc" in e and ("_a" in e or "_c" in e):
all_switches.append(e)
service_data = {'entity_id': 'input_select.box_1',
'options': all_switches}
hass.services.call('input_select', 'set_options', service_data)
service_data = {'entity_id': 'input_select.box_2',
'options': all_switches}
hass.services.call('input_select', 'set_options', service_data)
but if I put it into a function then nothing happens…
you can just do another for loop instead of a function. Typically functions in python scripts are very limited because the environment is limited. It’s not like normal python.
group_entities = hass.states.get('group.all_switches').attributes['entity_id']
# group_entities = ["switch.switch1","switch.sw2","switch.light1","switch.sonoff2,switch.yellow_room","switch.kmc_a","switch.kmc_b","switch.kmc_c"]
all_switches = []
for e in group_entities:
if "kmc" in e and ("_a" in e or "_c" in e):
all_switches.append(e)
service_data = {'options': all_switches}
for n in ['box_1', 'box_2']:
service_data['entity_id'] = 'input_select.{}'.format(n)
hass.services.call('input_select', 'set_options', service_data)