Fill Input select

Good day, looking for a bit of help.
trying to set options in an input_select from sensor attributes.
Have modified the above python code as follows:

hass.services.call("input_select", "set_options", {
    "entity_id": "input_select.bruno_videos",
    "options": hass.states.get('sensor.moviesg').attributes['file_list']
})

Which works but i want to remove the “/media/moviesg/” part from the attribute and end up only with the file name. i.e. Alphabet.mp4

‘/media/moviesg/Alphabet.mp4’,
‘/media/moviesg/Baby_shark.mp4’,
‘/media/moviesg/Mighty_Little_Bheem.mp4’

I’ve tried adding .split(’/’)[3] as follows
“options”: hass.states.get(‘sensor.moviesg’).attributes[‘file_list’] .split(’/’)[3]

but it doesn’t work.

Thank you

Have you tried the replace function?

hass.states.get(‘sensor.moviesg’).attributes[‘file_list’].replace(‘/media/moviesg/','')

(that’s 2 single quotes before the last parenthesis btw)

thank you very much .
tried your suggestion, but it doesnt work.
produces the same output as without the replace function

Now that templates support native types, does this work?

  - service: input_select.set_options
    data:
      entity_id: input_select.bruno_videos
      options: "{{ state_attr('sensor.moviesg', 'file_list') }}"

Theoretically, if the result of:

{{ state_attr('sensor.moviesg', 'file_list') }}

looks like anything like a list, it will be converted to type list and supplied to options.

Thank you very much for your help.

I tested {{ state_attr('sensor.moviesg', 'file_list').split('/')[3] }}

but I get the error:

UndefinedError: ‘list object’ has no attribute ‘split’

edit: this does what I want. Thank you for your help.

"{{ state_attr('sensor.moviesg','file_list')|replace('/media/moviesg/','')}}"

1 Like

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.

sensors_to_show:
  name: Sensors To Show
  options:
    - sensor.ambient_light_l_dr_power
    - sensor.ambient_light_r_dr_power
    - sensor.main_light_dr_power
    - sensor.ambient_light_lr_power
    - sensor.main_light_lr_power
    - sensor.fridge_energy_power
    - sensor.oven_energy_power
    - sensor.boiler_1_power
    - sensor.boiler_2_power
    - sensor.boiler_3_power
    - sensor.naim_audio_nsub_energy_power
    - sensor.kettle_energy_power
    - sensor.plasma_energy_power
    - sensor.av_socket_top_lr_power
    - sensor.av_socket_bottom_lr_power
    - sensor.eq_3_temperature_dr
    - sensor.eq_3_temperature_lr
    - sensor.eq_3_temperature_mbedr
    - sensor.eq_3_valve_dr
    - sensor.eq_3_valve_lr
    - sensor.eq_3_valve_mbedr

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

Hi,
I am wondering if this would be correct to update input_select.destionation with the friendly names of all of my zones?

zones = hass.states.entity_ids('zone')
all_zones = []
for e in zones:
    all_zones.append(hass.states.get(e).attributes['friendly_name'])
service_data = {
    "entity_id": "input_select.destination",
    "options": hass.states.entity_ids("zone")
}
hass.services.call("input_select", "set_options", service_data)

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… :confused:

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:

input_select:
  sonos_speaker:
    name: Sonos Speaker
    options:
      - empty

group:
  sonos_speakers:
    name: Sonos Speakers
    entities:
      - media_player.office_sonos
      - media_player.living_room_sonos
      - media_player.master_bedroom_sonos
      
automation:
  - id: intialize_speaker_group_input_select
    alias: Initialize Speaker Group Input Select
    initial_state: true
    mode: single
    trigger:
      - platform: homeassistant
        event: start
    action:
      - service: python_script.populate_input_select_with_group
        target:
          entity_id: input_select.sonos_speaker
        data:
          group_entity_id: group.sonos_speakers

Seems to work great! Now I can use this with all my groupings. :smiley:

Thanks for your responses to this thread @pnbruckner!!

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:

- service: input_select.set_options
  data:
    entity_id: input_select.sonos_speaker
    options: >
      {% set entities = state_attr('group.sonos_speakers', 'entity_id') %}
      {{ entities if entities else ["none"] }}
2 Likes

This worked well but I had to convert the entities tuple into a list for HA to accept it. Here’s that code:

- service: input_select.set_options
  data:
    entity_id: input_select.sonos_speaker
    options: >
      {% set entities = state_attr('group.sonos_speakers', 'entity_id') %}
      {{ entities | list if entities else ["none"] }}

You are definitely on top of the latest definitions! Thanks again Phil!