I just found this thread and it is exactly what I was looking for to list all my sensors.
But it seems I have a syntax problem somewhere.
I created a python script. The input_select.sensors_apexcharts is the output, correct? So a new input_select should be created under that name?
group_entities = hass.states.get('group.all_sensors').attributes['entity_id']
all_sensors = []
for e in group_entities:
all_sensors.append(hass.states.get(e).attributes['friendly_name'])
service_data = {'entity_id': 'input_select.sensors_apexcharts',
'options': all_sensors}
hass.services.call('input_select', 'set_options', service_data)
and then called it manually from Developer Tools to test it, but I get the error:
Logger: homeassistant.components.python_script.input_select_sensors.py
Source: components/python_script/__init__.py:222
Integration: Python Scripts (documentation, issues)
First occurred: 17:05:43 (1 occurrences)
Last logged: 17:05:43
Error executing script: 'NoneType' object is not subscriptable
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 222, in execute
exec(compiled.code, restricted_globals)
File "input_select_sensors.py", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/RestrictedPython/Eval.py", line 35, in default_guarded_getitem
return ob[index]
TypeError: 'NoneType' object is not subscriptable
For more context:
Summary
I am trying to populate my input_select on-the-fly.
I am currently using a manually created input_select.yaml which I would like to get filled by this script. I hope I am not misunderstanding this script.
This then goes into a lovelace UI card (currently with input_select.sensors_to_show which would be replaced by the input_select.sensors_apexcharts) once working)
type: vertical-stack
cards:
- type: entities
entities:
- entity: input_select.days_back_to_show
name: Number Of Days To Show
- entity: input_select.sensors_to_show
name: Select Sensor
- type: custom:config-template-card
entities:
- input_select.days_back_to_show
- input_select.sensors_to_show
variables:
sensor: states['input_select.sensors_to_show'].state
span: states['input_select.days_back_to_show'].state+'d'
days: |
-states['input_select.days_back_to_show'].state+'d'+'1d'
card:
type: custom:apexcharts-card
graph_span: ${span}
span:
start: day
offset: ${days}
series:
- entity: ${sensor}
stroke_width: 2
group_by:
func: raw
Using python in this way is really awesome @pnbruckner. I’m not too familiar with using python like this in Home Assistant. I wonder if there’s a way to genericize the python implementation so that you can pass the dependent arguments in (like input_select entity and group of sensors). I’d like to use this for several groups/input selects but I can’t wrap my head around how to pass that info in…
EDIT
I just figured this out after reviewing the Python Scripts - Home Assistant page. I made a generic file called populate_input_select_with_group.py in my python_scripts directory with this content:
input_select_entity_id = data.get("entity_id")
group_entity_id = data.get("group_entity_id")
if input_select_entity_id is not None and group_entity_id is not None:
group_entities = hass.states.get(group_entity_id).attributes['entity_id']
all_entities = []
for e in group_entities:
all_entities.append(e)
service_data = {'entity_id': input_select_entity_id, 'options': all_entities}
hass.services.call('input_select', 'set_options', service_data)
Then I created the necessary input_select, group, and automation to use it:
Well, this is a very old topic. Templates in older versions of HA could only result in a string. But that has not been the case for a while. You can now do this much more directly: