Setting input_select based on state

Hi,
I’m havng trouble getting a very simple automation to work. Based on the state of a certain sensor It should set an input_select to that very same value.

- alias: SetPanasonicInputSelectionBox
  trigger:
    platform: state
    entity_id: sensor.panasonic_current_input
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.panasonic_input
      option: '{{states.sensor.panasonic_current_input.state}}'

So, the current_input sensor always holds a value that is also present in the input_select.
I tried a lot of combinations of “data” vs. “data_template” and so on but to no luck.

According to the Logbook, the automation is triggered, but It has no effect. Turning on logging in the configuration did not help either. I don’t see any relevant output such as “condition evaluated to false” or “action could not be executed because…”.

I assume it is an issue with the “option” line somewhere, but I could not find a relevant example anywhere. Typically, people seem to be putting plain text in these options and not expressions.

Anyone know how to solve this?

Thanks

Jochen

That’s odd. It looks correct to me. I know it’s a long shot but Sometimes it’s as simple as the wrong entity_id. Are you sure your input_select is input_select.panasonic_input and not input_select.panasonic_current_input?

also make sure your datatypes are correct.
if your sensor outputs a string, you have to convert it to int to use the value.

{{states.sensor.panasonic_current_input.state | int}}

Hi,
thank your for your suggestions.
Especially the data types are always something to look out for. In this case, both were strings, though.

I’ve now figured out what was wrong.

The automation itself is working like this:

- alias: SetPanasonicInputSelectionBox
  trigger:
    platform: state
    entity_id: sensor.panasonic_current_input
  action:
    service: input_select.select_option
    entity_id: input_select.panasonic_input
    data_template:
      option: '{{states.sensor.panasonic_current_input.state}}'

So, the variant with the data_template is a must-have according to the documentation:

Rule No.1 of HomeAssistant club:

You must use data_template in place of data when using templates in the data section of a service call.

So this was my mistake in the beginning.
However, I also tried that yesterday and it didn’t work. (Also, I found examples where the entity_id attribute was also within the data_template part which I still don’t know what it would do or not do.)

The reason seems to be the interference with the initial value of the input_select which was defined like this:

input_select:
  panasonic_input:
    name: Panasonic Input
    options:
     - HOME
     - SD
     - USB
     - IPOD
     - FM
     - AUX
     - ARC
     - DIN
     - HDMI1
     - HDMI2
    initial: HOME                   <------------
    icon: mdi:speaker-wireless

So, after booting up, the input_select was always set to HOME instead of the actual input that it requested from the Panasonic device via REST. (HOME should only be set if the device isn’t reachable.)

After I removed the initial value from the input_select, the automation worked and is synced with the sensor after the device comes up. This effect obstructed my tests with the different variants of automation syntax so it seemed like none of them were working. Subsequent state changes do work though with the automation above and the initial value. It is just the state change after booting up.

I don’t know about you, but this smells like a bug.

What do you think?

Jochen

1 Like

One addition:

According to the same documentation as mentioned above, the automation can be simplified to use the value from the trigger instead of retrieving it again from the sensor:

alias: SetPanasonicInputSelectionBox
  trigger:
    platform: state
    entity_id: sensor.panasonic_current_input
  action:
    service: input_select.select_option
    entity_id: input_select.panasonic_input
    data_template:
      option: '{{trigger.to_state.state}}'

This was not really apparent to me as a HA beginner because I couldn’t find good examples illustrating the use of automation triggers in the actions. Even in the official docs they almost always use hardcoded values in the actions for some reason.

2 Likes

Oh that’s a great tip. I have to keep that in mind. I have so many automations that work but I think they deserve a good cleanup. It’s coming on a year and some stuff I haven’t touched since I first started using HASS. Thanks!

I have a similar issue that I’ve been battling for 4 hours already. Automation triggers but doesn’t do anything. I’m trying to circle through 3 different light scenes on double-press of the button. Do you guys see what’s wrong with this one please?

- id: '1550970812168'
  alias: Change light scene
  trigger:
  - event_data:
      click_type: double
      entity_id: binary_sensor.switch_158d00017143ca
    event_type: click
    platform: event
  - event_data:
      click_type: double
      entity_id: binary_sensor.switch_158d0001a66e5c
    event_type: click
    platform: event
  condition: []
  action:
  - service: input_select.select_option
    data_template:
      entity_id: input_select.light_scene
      options: >
        {% if is_state('input_select.light_scene', 'Brightest') %}
          Night
        {%-elif is_state('input_select.light_scene', 'Night') %}
          Movie
        {%-elif is_state('input_select.light_scene', 'Movie') %}
          Surprise
        {%-elif is_state('input_select.light_scene', 'Surprise') %}
          Night
        {% else %}
          Night
        {% endif %}