Input_select: how to use service_template and data_template together

Hassio on RP3. I use input_select in an automation to cycle through different light scenes when a button is pushed. Works like a charm. Another button switches the lights off. When I turn the lights on again using the first button, it will use the next scene in the input_next. Instead, I’d like it to use the last state IF the lights were off when the input_select is used. So. This works:

  action:
    - entity_id: input_select.sykle_sove
      service_template: >
        {% if is_state("light.lesestripa", "on") %}
          input_select.select_next
        {% else %}
          input_select.select_next
        {% endif %}

This does not. Nothing happens when the lights are off.

  action:
    - entity_id: input_select.sykle_sove
      service_template: >
        {% if is_state("light.lesestripa", "on") %}
          input_select.select_next
        {% else %}
          input_select.select_option
        {% endif %}
        data_template:
          data:
            option: >
              {{states('input_select.sykle_sove')}}

How can I pass the ‘option’ data to the service?

  action:
    - entity_id: input_select.sykle_sove
      service_template: >
        {% if is_state("light.lesestripa", "on") %}
          input_select.select_next
        {% else %}
          input_select.select_option
        {% endif %}
    data_template:
      option: >
        {{states('input_select.sykle_sove')}}

but as far as I know the “_template” isn’t needed any longer.

  action:
    - entity_id: input_select.sykle_sove
      service: >
        {% if is_state("light.lesestripa", "on") %}
          input_select.select_next
        {% else %}
          input_select.select_option
        {% endif %}
      data:
        option: >
          {{states('input_select.sykle_sove')}}

I don’t think this will work, because if the service is “input_select.select_next” then option is not a vaild option (haha).
This can be solved with a choose action.

1 Like

I have a bad habit of assuming that all of the “other” parts of someone’s code has been verified to be correct. I usually don’t have the time to research every integration to make sure that the code is all correct for every permutation of a users code. :slightly_smiling_face:

I saw the bad indentation and pointed that out as a potential problem.

Thanks for the correction. :slightly_smiling_face:

1 Like

Try this:

action:
  - choose:
      - conditions: "{{ is_state('light.lesestripa', 'on') }}"
        sequence:
          - service: input_select_next
            entity_id: input_select.sykle_sove
    default:
      - service: input_select.select_option
        entity_id: input_select.sykle_sove
        data:
          option: "{{ states('input_select.sykle_sove') }}"

Thanks for replies! I tried the latest suggestion from @Burningstone, and it does, unfortunately, not work, for some reason. This, however, works, so the choose/default statements are ok. But the option-passing is still a challenge.

  action:
    - choose:
        - conditions: "{{ is_state('light.lesestripa', 'on') }}"
          sequence:
            - service: input_select.select_next
              entity_id: input_select.sykle_sove
      default:
        - service: input_select.select_next
          entity_id: input_select.sykle_sove

Can you please show the whole automation? What do you mean with “not work”? Does it choose the next action or not choose an action at all? This action reads like this: IF light.lesestripa is on THEN choose next option of input select ELSE choose the same value as is.

Thanks. Sorry for the sloppy error description. «not work» means no feedback when the button is pressed. The logbook shows that the automation is triggered. Here is the whole automation.

- alias: 'Sykle lys på soveværelset'
  trigger:
    - entity_id: remote.sengebryter
      platform: state
  condition:
    - condition: state
      entity_id: remote.sengebryter
      state: '4_click_up'
  action:
    - choose:
        - conditions: "{{ is_state('light.lesestripa', 'on') }}"
          sequence:
            - service: input_select.select_next
              entity_id: input_select.sykle_sove
      default:
        - service: input_select.select_option
          entity_id: input_select.sykle_sove
          data:
            option: "{{ states('input_select.sykle_sove') }}"

I have now verified that the first part «choose» works, but the «default» gives no response.
It works, however, if I replace

"{{ states('input_select.sykle_sove') }}"

with

Sove3

which is one of the states.

Probably it doesn’t do anything because it’s the same option. I anyway don’t see why you need to set the input select to the same state it already is in?