STYRBAR blueprint to dimmer/change color with bulb

Hi guys, this is my first blueprint. I’ve bought this device from Ikea (model STYRBAR E2001/E2002) to play with blueprints and automations.
I’ve connected this device with my Yeelight dimmerable/colorable bulb and everything’s fine for events like button up/down/left/right clicked.
My problem is about long press events like for example brightness_move_up when hold, brightness_stop when released. In blueprint I’ve tried to create a loop to increase/descrease brightness while holding button but my bulb receive an error with message:

Error when calling async_set_brightness for bulb Yeelight Color 0x12c713a3 at 192.168.1.14: {‘code’: -1, ‘message’: ‘client quota exceeded’}

It seems related to a bug (???) or to many call sent in my loop, but I don’t understand why even if I set a loop delay of 500 millisec.

If I set the delay to 1 sec. the error occur rarely (but occurs) and anyway the effect is to much slow for a nice experience. I tried to set automation mode to parallel/queued/single and it seems the the last settings is preferable but not perfect.
How can I manage long pressing in correct/kindly mode, please? Thanks.
Below the blueprint:

blueprint:
  name: Ikea STYRBAR automation light
  description: Turn a light on/off - increase/decrease brightness - set color and temperature
  domain: automation
  source_url: http://localhost.it
  input:
    remote:
      name: Ikea STYRBAR 2001/2002
      description: This remote device will manage the light.
      selector:
        entity:
          domain: sensor
    target_light:
      name: Light color
      description: The light color to keep in sync.
      selector:
        entity:
          domain: light
    brightness_step:
      name: Brightness increment
      description: Value to increment brightness.
      default: 20
      selector:
        number:
          min: 10
          max: 100
    target_hue:
      name: Hue increment
      description: Value to increment hue.
      default: 30
      selector:
        number:
          min: 10
          max: 360
    target_sat:
      name: Saturation increment
      description: Value to increment saturation.
      default: 90
      selector:
        number:
          min: 10
          max: 100
    target_color_temp:
      name: Color temperature increment
      description: Value to increment color temperature.
      default: 40
      selector:
        number:
          min: 153
          max: 500
trigger_variables:
  target_light_variable: !input target_light
  target_hue_variable: !input target_hue
  target_sat_variable: !input target_sat
  target_color_temp_variable: !input target_color_temp
  brightness_step_variable: !input brightness_step
trigger:
  - platform: state
    entity_id: !input remote
condition: []
action:
  - variables:
      command: "{{ trigger.to_state.state }}"
      loop_delay: 1000
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ command == 'on' }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: !input target_light
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ command == 'off' }}"
        sequence:
          - service: light.turn_off
            target:
              entity_id: !input target_light
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ command == 'arrow_left_click' }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: !input target_light
            data_template:
              color_temp: >-
                {% if (target_color_temp_variable +
                (state_attr(target_light_variable, 'color_temp') or
                0) > 500)  %}
                  153
                {% elif (target_color_temp_variable +
                (state_attr(target_light_variable, 'color_temp') or
                0) < 153)  %}
                  500
                {% else %}
                  {{ (target_color_temp_variable + (state_attr(target_light_variable, 'color_temp') or 0)) }}
                {% endif %}
      - conditions:
          - condition: template
            value_template: "{{ command == 'arrow_right_click' }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: !input target_light
            data_template:
              hs_color:
                - >-
                  {{ (target_hue_variable +
                  (state_attr(target_light_variable, 'hs_color')[0]
                  or 0)) % 360 }}
                - "{{ target_sat_variable }}"
      - conditions:
          condition: and
          conditions:
            - condition: template
              value_template: "{{ command == 'brightness_move_up' }}"
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: !input target_light
                  data:
                    brightness_step: "{{ brightness_step_variable }}"
                - delay:
                    milliseconds: "{{ loop_delay }}"
              until:
                condition: or
                conditions:
                  - condition: template
                    value_template: "{{ command == 'brightness_stop' }}"
                  - condition: template
                    value_template: "{{ state_attr(target_light_variable, 'brightness') >= 255 }}"
      - conditions:
          condition: and
          conditions:
            - condition: template
              value_template: "{{ command == 'brightness_move_down' }}"
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: !input target_light
                  data:
                    brightness_step: "{{ - brightness_step_variable }}"
                - delay:
                    milliseconds: "{{ loop_delay }}"
              until:
                condition: or
                conditions:
                  - condition: template
                    value_template: "{{ command == 'brightness_stop' }}"
                  - condition: template
                    value_template: "{{ state_attr(target_light_variable, 'brightness') <= -200 }}"

Every suggest aimed to improve my code is wellcome :slight_smile:

Ok I think I’ve improved my blueprint.
Changed logic, refactored code and indentation.
Now loop works nicely (I suppose I can still improve it). Yet not implementation for left/right long press button (still working on it)

blueprint:
  name: Ikea STYRBAR automation light
  description: Turn a light on/off - increase/decrease brightness - set color and temperature
  domain: automation
  source_url: http://localhost.it
  input:
    remote:
      name: Ikea STYRBAR 2001/2002
      description: This remote device will manage the light.
      selector:
        entity:
          domain: sensor
    target_light:
      name: Light color
      description: The light color to keep in sync.
      selector:
        entity:
          domain: light
    force_brightness:
      name: Force turn on brightness
      description: Force the brightness to the set level below, when the "on" button
        on the remote is pushed and lights turn on.
      default: false
      selector:
        boolean: {}
    brightness:
      name: Brightness
      description: Brightness of the light(s) when turning on
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          mode: slider
          step: 1.0
          unit_of_measurement: '%'
    hue:
      name: Hue
      description: Hue of light(s) when turning on.
      default: 180.0
      selector:
        number:
          min: 0.0
          max: 360.0
          mode: slider
          unit_of_measurement: 'rad'
    saturation:
      name: Saturation
      description: Saturation of light(s) when turning on.
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          mode: slider
          step: 1.0
          unit_of_measurement: '%'
    kelvin:
      name: Color temperature
      description: Color temperature of light(s) when turning on.
      default: 4000.0
      selector:
        number:
          min: 2000.0
          max: 6500.0
          mode: slider
          step: 100.0
          unit_of_measurement: '°K'
mode: restart
max_exceeded: silent
variables:
  force_brightness: !input 'force_brightness'
  hue: !input 'hue'
  saturation: !input 'saturation'
trigger:
- platform: state
  entity_id: !input remote
trigger_variables:
  input_light: !input target_light
action:
- variables:
    command: "{{ trigger.to_state.state }}"
    loop_delay: 1000
- choose:
  - conditions:
    - "{{ command == 'on' }}"
    sequence:
    - choose:
      - conditions: '{{ force_brightness }}'
        sequence:
        - service: light.turn_on
          target:
            entity_id: !input target_light
          data:
            brightness_pct: !input 'brightness'
            kelvin: !input kelvin
      default:
      - service: light.turn_on
        target: 
          entity_id: !input target_light
  - conditions:
    - "{{ command == 'off' }}"
    sequence:
    - service: light.turn_off
      target:
        entity_id: !input target_light  
  - conditions:
    - "{{ command == 'brightness_move_up' }}"
    sequence:
    - repeat:
        count: 10
        sequence:
        - service: light.turn_on
          target: 
            entity_id: !input target_light
          data:
            brightness_step_pct: 10
            transition: 1
        - delay: 0.5
  - conditions:
    - "{{ command == 'brightness_move_down' }}"
    sequence:
    - repeat:
        count: 10
        sequence:
        - service: light.turn_on
          target: 
            entity_id: !input target_light
          data:
            brightness_step_pct: -10
            transition: 1
        - delay: 0.5
  - conditions:
    - "{{ command == 'arrow_left_click' }}"
    sequence:
    - service: light.turn_on
      target:
        entity_id: !input target_light
      data:
        hs_color:
        - "{{ state_attr(input_light, 'hs_color')[0] }}"
        - >-
            {% if ((10 + (state_attr(input_light, 'hs_color')[1] or 0)) > 100)  %}
              0
            {% else %}
              {{ (10 + (state_attr(input_light, 'hs_color')[1] or 0)) }}
            {% endif %}
  - conditions:
    - "{{ command == 'arrow_right_click' }}"
    sequence:
    - service: light.turn_on
      target:
        entity_id: !input target_light
      data:
        hs_color:
        - >-
            {% if ((30 + (state_attr(input_light, 'hs_color')[0] or 0)) > 360)  %}
              0
            {% else %}
              {{ (30 + (state_attr(input_light, 'hs_color')[0] or 0)) }}
            {% endif %}
        - "{{ state_attr(input_light, 'hs_color')[1] }}"

Below the import badge:

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: Ikea STYRBAR automation light
  description: Turn a light on/off - increase/decrease brightness - set color and temperature
  domain: automation
  source_url: https://github.com/robertomontinaro80/ha-blueprint/blob/f65b9647750cb39f228620ebe9b25e96dc9c60f2/ikea_styrbar_light.yaml
  input:
    remote:
      name: Ikea STYRBAR 2001/2002
      description: This remote device will manage the light.
      selector:
        entity:
          domain: sensor
    target_light:
      name: Light color
      description: The light color to keep in sync.
      selector:
        entity:
          domain: light
    force_brightness:
      name: Force turn on brightness
      description: Force the brightness to the set level below, when the "on" button
        on the remote is pushed and lights turn on.
      default: false
      selector:
        boolean: {}
    brightness:
      name: Brightness
      description: Brightness of the light(s) when turning on
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          mode: slider
          step: 1.0
          unit_of_measurement: '%'
    hue:
      name: Hue
      description: Hue of light(s) when turning on.
      default: 180.0
      selector:
        number:
          min: 0.0
          max: 360.0
          mode: slider
          unit_of_measurement: 'rad'
    saturation:
      name: Saturation
      description: Saturation of light(s) when turning on.
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          mode: slider
          step: 1.0
          unit_of_measurement: '%'
    kelvin:
      name: Color temperature
      description: Color temperature of light(s) when turning on.
      default: 4000.0
      selector:
        number:
          min: 2000.0
          max: 6500.0
          mode: slider
          step: 100.0
          unit_of_measurement: '°K'
mode: restart
max_exceeded: silent
variables:
  force_brightness: !input 'force_brightness'
  hue: !input 'hue'
  saturation: !input 'saturation'
trigger:
- platform: state
  entity_id: !input remote
trigger_variables:
  input_light: !input target_light
action:
- variables:
    command: "{{ trigger.to_state.state }}"
    loop_delay: 1000
- choose:
  - conditions:
    - "{{ command == 'on' }}"
    sequence:
    - choose:
      - conditions: '{{ force_brightness }}'
        sequence:
        - service: light.turn_on
          target:
            entity_id: !input target_light
          data:
            brightness_pct: !input 'brightness'
            kelvin: !input kelvin
      default:
      - service: light.turn_on
        target: 
          entity_id: !input target_light
  - conditions:
    - "{{ command == 'off' }}"
    sequence:
    - service: light.turn_off
      target:
        entity_id: !input target_light  
  - conditions:
      - "{{ command == 'brightness_move_up' }}"
      - "{{ (10 + (state_attr(input_light, 'brightness_step_pct') or 0)) <= 100 }}"
    sequence:
    - repeat:
        count: 10
        sequence:
        - service: light.turn_on
          target: 
            entity_id: !input target_light
          data:
            brightness_step_pct: 10
            transition: 1
        - delay: 0.5
  - conditions:
      - "{{ command == 'brightness_move_down' }}"
      - >-
        {{ (10 + (state_attr(input_light, 'brightness_step_pct') or 0)) <= 100
        }}
    sequence:
    - repeat:
        count: 10
        sequence:
        - service: light.turn_on
          target: 
            entity_id: !input target_light
          data:
            brightness_step_pct: -10
            transition: 1
        - delay: 0.5
  - conditions:
    - "{{ command == 'arrow_left_click' }}"
    sequence:
    - service: light.turn_on
      target:
        entity_id: !input target_light
      data:
        hs_color:
        - "{{ state_attr(input_light, 'hs_color')[0] }}"
        - >-
            {% if ((10 + (state_attr(input_light, 'hs_color')[1] or 0)) > 100)  %}
              0
            {% else %}
              {{ (10 + (state_attr(input_light, 'hs_color')[1] or 0)) }}
            {% endif %}
  - conditions:
    - "{{ command == 'arrow_right_click' }}"
    sequence:
    - service: light.turn_on
      target:
        entity_id: !input target_light
      data:
        hs_color:
        - >-
            {% if ((30 + (state_attr(input_light, 'hs_color')[0] or 0)) > 360)  %}
              0
            {% else %}
              {{ (30 + (state_attr(input_light, 'hs_color')[0] or 0)) }}
            {% endif %}
        - "{{ state_attr(input_light, 'hs_color')[1] }}"

I noticed tha the loop

- conditions:
      - "{{ command == 'brightness_move_up' }}"
      - "{{ (10 + (state_attr(input_light, 'brightness_pct') or 0)) <= 100 }}"
    sequence:
    - repeat:
        count: 10
        sequence:
        - service: light.turn_on
          target: 
            entity_id: !input target_light
          data:
            brightness_step_pct: 10
            transition: 1
        - delay: 0.5

Doesn’t work properly. It looks like that the condition

   - "{{ (10 + (state_attr(input_light, 'brightness_step_pct') or 0)) <= 100 }}"

Is always true. How can I see the value of state_attr function? Is there a sort of debug or a way to check the values in my automation when I run it? Via UI there’s a section (trace) for automation but read the step values is a bit complicated