Indexing light input to use one at time (Template problem)

I found this blueprint for the moes knob to control dimable lights, and im trying to adapt it so instead of control the brightness of all input lights it uses the single press event to select one of the input lights and only aply the automation too that one. When one clicks the button it goes to the next one a so on. So basically the button selects the light from the inputs and the rotation knob applies the change in brightness. What i have so fare looks like this:

blueprint:
  name: Knob for livingroom lights
  description: "Switch between lights and control light brightness"
  domain: automation
  input:
    remote:
      name: Remote
      description: Moes Knob to use
      selector:
        device:
          integration: zha
          manufacturer: _TZ3000_qja6nq5z
          model: TS004F
          multiple: false
    lights:
      name: Lights
      description: The light(s) to control
      selector:
        target:
          entity:
            domain: light
mode: restart
max_exceeded: silent
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input remote
action:
  - variables:
      command: "{{ trigger.event.data.command }}"
      cluster_id: "{{ trigger.event.data.cluster_id }}"
      args:
        "{% if trigger.event.data.args[0] == 0 %} 0 {% elif trigger.event.data.args[0]
        == 1 %} 1 {% elif trigger.event.data.args[0] == 3 %} 3 {% endif %}"
      selected_light: 0 # Selected index by the single press event
  - choose:
      - conditions: # this is the single press event
          - "{{ command == 'toggle' }}"
          - "{{ cluster_id == 6 }}"
        sequence: 
       # Here goes the code to switch the index on button press ??
       # My idea is to change the selected_light variable to use as index
       # on the next automations
      - conditions:
          - "{{ command == 'step' }}"
          - "{{ cluster_id == 8 }}"
          - "{{ args == 0 }}"
        sequence:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index < 2 }}"
              sequence:
                - service_template: lights.turn_on
                  target: !input lights # Instead of this, get the target from the variable selected_light
                  data_template:
                    brightness_step_pct: 5
                    transition: 0.5

I added some comments on what i’ve been trying, my thought process. But im not being able to get my head around this. And to be honest i dont even know if this is possible.

Any ideas on this? tks in advance

EDIT:
It would be awesome to be able to provide a blueprint of this to the community, but unfortunately i wasn’t able to do this. And i dont really think, now that i learned a bit more about how this things work, this would be possible with a single blueprint.
So, what i did to make this automation to work was:
First of all, make sure the knob is in the scene mode (triple press the knob changes the mode).
I created an input number helper, in my case input_number.knob_selected_device that is used to identifies which light the knob should affect (Ex: 1 → living room lights, 2 → dining room lights, etc).
Then I created an automation for when pressing the knob changes the input to the right number. Because of the way automations work i need one of this automatons for each light i want to use. EX:

alias: knob-chose-living-room
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: <<KNOB-DEVICE-ID>>
condition: []
action:
  - variables:
      command: "{{ trigger.event.data.command }}"
      cluster_id: "{{ trigger.event.data.cluster_id }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ command == 'toggle' }}"
          - condition: template
            value_template: "{{ cluster_id == 6 }}"
          - condition: state
            entity_id: <<INPUT-NUMBER-HELPER>>
            state: "<<PREV_STATE_VAL>>" # HERE GOES THE PREVIOUS VALUE, EX: "1.0"
        sequence:
          - service: input_number.set_value
            metadata: {}
            data:
              value: "<<NEXT_VAL>>" # HERE GOES THE PREV_STATE_VAL + 1, OR 1 IF THE PREV VALUE IS THE LAST ONE, Ex: "2"
            target:
              entity_id: <<INPUT-NUMBER-HELPER>>
          - if:
              - condition: state
                entity_id: <<LIGHT-TO-AFFECT-ID>>
                state: "off"
            then:
              - service: light.turn_on
                target:
                  entity_id: <<LIGHT-TO-AFFECT-ID>>
                data:
                  brightness_pct: 25
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 0
                  milliseconds: 500
              - service: light.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: <<LIGHT-TO-AFFECT-ID>>
          - if:
              - condition: state
                entity_id: <<LIGHT-TO-AFFECT-ID>>
                state: "on"
            then:
              - service: light.turn_off
                target:
                  entity_id: <<LIGHT-TO-AFFECT-ID>>
                data: {}
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 0
                  milliseconds: 500
              - service: light.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: <<LIGHT-TO-AFFECT-ID>>
mode: single

This, not only puts the helper with the right index, but blinks the selected light so i can have a visual input of what light is selected.
Then I have the automation that affects the brightness. Again, one for each light i want to control:

alias: knob-living-lights
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: <<KNOB-DEVICE-ID>>
action:
  - variables:
      command: "{{ trigger.event.data.command }}"
      cluster_id: "{{ trigger.event.data.cluster_id }}"
      args: >-
        {% if trigger.event.data.args[0] == 0 %} 0 {% elif
        trigger.event.data.args[0] == 1 %} 1 {% elif trigger.event.data.args[0]
        == 3 %} 3 {% endif %}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ command == 'step' }}"
          - condition: template
            value_template: "{{ cluster_id == 8 }}"
          - condition: template
            value_template: "{{ args == 0 }}"
          - condition: state
            entity_id: <<INPUT-NUMBER-HELPER>>
            state: "<<THIS-LIGHT-INDEX>>" # EX. "2.0"
        sequence:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index < 2 }}"
              sequence:
                - service: light.turn_on
                  metadata: {}
                  data:
                    brightness_step_pct: 10
                  target:
                    entity_id: <<LIGHT-TO-AFFECT-ID>>
      - conditions:
          - condition: template
            value_template: "{{ command == 'step' }}"
          - condition: template
            value_template: "{{ cluster_id == 8 }}"
          - condition: template
            value_template: "{{ args == 1 }}"
          - condition: state
            entity_id: <<INPUT-NUMBER-HELPER>>
            state: "<<THIS-LIGHT-INDEX>>" # EX. "2.0"
        sequence:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index < 2 }}"
              sequence:
                - service: light.turn_on
                  metadata: {}
                  data:
                    brightness_step_pct: -10
                  target:
                    entity_id: <<LIGHT-TO-AFFECT-ID>>
max_exceeded: silent
mode: single

And yeah, with this all seems to work just fine. Just a bit cumbersome than i would likebut it is what it is.

I realize this is a Blueprint so you were good posting this here.
I tweaked your title a bit to attract one of the template Gurus to help with this as I would struggle to get something to work there. I’m confident it can be made to happen, however.

1 Like