Trying to create an automation to increase light brightness on a press and hold command and stop when released

Ok it’s time to ask for help. I have no coding experience so i apologize if i have made any obvious errors. I have spent weeks searching, reading the docs and learning as much as i can.

I am trying to create a multi press button for our bedside table lamps.
I have currently have a Nodemcu ESP32 with a push button that i have programed to send home assistant events for single click, double click, hold and release from EspHome.

The light is a bulb which i have setup as 3 entities, White, Colour and All (both white and colour for a warm white light) also from EspHome

That works as expected and i can receive all events.

I have single click toggling on the entity id from an input_select. that works fine.

i have Double click changing modes on the input_select and turning the light to that mode.

The problem i have is getting the press and hold to ramp the lights up to full brightness and stop at what ever point i release the button.

I have the hold activate an input_boolean which i use to trigger an automation to run a script to start the lights of a brightness of 5.

I then am trying to get the brightness attribute change to trigger an automation that will loop and run as long as the condition of the input boolean it on. once i release the button the input boolean turns off and so the automation loop should then stop.

I believe where I am having a problem is getting the brightness attribute to trigger the automation.

I have it all in a package which i have included below.

Thanks in advance for any assistance.

    bedroom_lamp:
      name: "Bedroom Lamp"
      initial: false

    bedroom_lamp_dimmer:
      name: "Bedroom Lamp Dimmer"
      initial: false

input_select:
    bedroom_lamp_mode:
      name: Bedroom Lamp Mode
      options:
        - All
        - Colour
        - White
      initial: All
      icon: mdi:lamp

script:
  bedroom_lamp_turn_on:
    alias: Bedroom Lamp Turn On
    sequence:
    - data: {}
      entity_id: light.bedroom_lamps
      service: light.turn_off
    - data_template:
        entity_id: >
            {% if is_state("input_select.bedroom_lamp_mode", "White") %}
              light.bedroom_lamps_white
            {%-elif is_state("input_select.bedroom_lamp_mode", "Colour") %}
              light.bedroom_lamps_colour
            {%-elif is_state("input_select.bedroom_lamp_mode", "All") %}
              light.bedroom_lamps
            {% endif %}
      service: light.turn_on

  bedroom_lamp_dimmer_start:
    alias: Bedroom Lamp Dimmer Start
    sequence:
    - data_template:
        entity_id: >
            {% if is_state("input_select.bedroom_lamp_mode", "White") %}
              light.bedroom_lamps_white
            {%-elif is_state("input_select.bedroom_lamp_mode", "Colour") %}
              light.bedroom_lamps_colour
            {%-elif is_state("input_select.bedroom_lamp", "All") %}
              light.bedroom_lamps
            {% endif %}
        brightness_step: 5
      service: light.turn_on

  bedroom_dimmer_loop:
    alias: Bedroom Lamp Dimmer 
    sequence:
    - condition: state
      entity_id: input_boolean.bedroom_lamp_dimmer
      state: 'on'
    - below: '251'
      condition: numeric_state
      entity_id: light.bedroom_lamps
      value_template: "{{ state.attributes.brightness}}"
    - data_template:
        entity_id: >
            {% if is_state("input_select.bedroom_lamp", "White") %}
              light.bedroom_lamps_white
            {%-elif is_state("input_select.bedroom_lamp", "Colour") %}
              light.bedroom_lamps_colour
            {%-elif is_state("input_select.bedroom_lamp", "All") %}
              light.bedroom_lamps
            {% endif %}
        brightness_step: 5
      service: light.turn_on

automation:

  - id: 'bedroom_lamp_input_boolean_toggle'
    alias: Bedroom Lamp Input Boolean Toggle
    description: ''
    trigger:
    - event_data:
        title: single_click
      event_type: esphome.test_esp_32
      platform: event
    condition: []
    action:
    - data: {}
      entity_id: input_boolean.bedroom_lamp
      service: input_boolean.toggle

  - id: 'bedroom_lamp_turn_on'
    alias: Bedroom Lamp Turn On
    description: ''
    trigger:
    - entity_id: input_boolean.bedroom_lamp
      from: 'off'
      platform: state
      to: 'on'
    condition: []
    action:
    - data: {}
      service: script.bedroom_lamp_turn_on

  - id: 'bedroom_lamp_turn_off'
    alias: Bedroom Lamp Turn off
    description: ''
    trigger:
    - entity_id: input_boolean.bedroom_lamp
      from: 'on'
      platform: state
      to: 'off'
    condition: []
    action:
    - data: {}
      entity_id: light.bedroom_lamps
      service: light.turn_off

  - id: 'bedroom_lamp_mode'
    alias: Bedroom Light Mode
    description: ''
    trigger:
    - event_data:
        title: dbl_click
      event_type: esphome.test_esp_32
      platform: event
    condition: []
    action:
    - data: {}
      entity_id: input_select.bedroom_lamp_mode
      service: input_select.select_next
    - data: {}
      service: script.bedroom_lamp_turn_on

  - id: 'bedroom_lamp_dimmer_input_boolean_turn_on'
    alias: 'Bedroom Lamp Dimmer Input Boolean Turn On'
    trigger:
    - event_data:
        title: hold
      event_type: esphome.test_esp_32
      platform: event
    action:
      - data: {}
        service: input_boolean.turn_on
        entity_id: input_boolean.bedroom_lamp_dimmer

  - id: 'Bedroom_lamp_dimmer_input_boolean_turn_off'
    alias: 'Bedroom Lamp Dimmer Input Boolean Turn Off'
    trigger:
    - event_data:
        title: release
      event_type: esphome.test_esp_32
      platform: event
    action:
      - data: {}
        service: input_boolean.turn_off
        entity_id: input_boolean.bedroom_lamp_dimmer

  - id: 'bedroom_lamp_dimmer_loop_start'
    alias: Bedroom Lamp Dimmer Loop Start
    description: ''
    trigger:
    - entity_id: input_boolean.bedroom_lamp_dimmer
      platform: state
      to: 'on'
    condition: []
    action:
    - data: {}
      service: script.bedroom_lamp_dimmer_start 

  - id: 'bedroom_lamp_dimmer_loop'
    alias: Bedroom Lamp Dimmer Loop
    description: ''
    trigger:
      platform: state
      entity_id: light.bedroom_lamps
    condition:
      - condition: template 
        value_template: >
          {{ trigger.to_state.attributes.brightness !=
            trigger.from_state.attributes.brightness }} 
      - condition: state
        entity_id: input_boolean.bedroom_lamp_dimmer
        state:  'on'
    action:
    - delay: 100ms
    - data: {}
      service: script.bedroom_lamp_dimmer_loop

group:
  bedroom_lamp:
    name: Bedroom Lamp
    entities:
      - input_boolean.bedroom_lamp
      - input_boolean.bedroom_lamp_dimmer
      - input_select.bedroom_lamp_mode
      - script.bedroom_lamp_turn_on
      - script.bedroom_lamp_dimmer_start
      - script.bedroom_dimmer_loop
      - automation.bedroom_lamp_input_boolean_toggle
      - automation.bedroom_lamp_turn_on
      - automation.bedroom_lamp_turn_off
      - automation.bedroom_lamp_mode
      - automation.bedroom_lamp_dimmer_input_boolean_turn_on
      - automation.bedroom_lamp_dimmer_input_boolean_turn_off
      - automation.bedroom_lamp_dimmer_loop_start
      - automation.bedroom_lamp_dimmer_loop

The main problem, I think, with what you have is, the following is not valid:

    - delay: 100ms

The correct way is:

    - delay:
        milliseconds: 100

Having said that, please note that the delay will actually be anywhere from 100 ms to 1.1 s, since the current implementation of the delay step is to only check once a second if the specified time has elapsed.

Also, a more typical way to implement a loop like this is to call the second script from the first, and then split that second script into two (instead of using your automation technique.) E.g.:

  bedroom_lamp_dimmer_start:
    alias: Bedroom Lamp Dimmer Start
    sequence:
    - data_template:
        entity_id: >
            {% if is_state("input_select.bedroom_lamp_mode", "White") %}
              light.bedroom_lamps_white
            {%-elif is_state("input_select.bedroom_lamp_mode", "Colour") %}
              light.bedroom_lamps_colour
            {%-elif is_state("input_select.bedroom_lamp", "All") %}
              light.bedroom_lamps
            {% endif %}
        brightness_step: 5
      service: light.turn_on
    - service: script.bedroom_dimmer_loop

  bedroom_dimmer_loop:
    alias: Bedroom Lamp Dimmer Part 1
    sequence:
    - condition: state
      entity_id: input_boolean.bedroom_lamp_dimmer
      state: 'on'
    - below: '251'
      condition: numeric_state
      entity_id: light.bedroom_lamps
      value_template: "{{ state.attributes.brightness}}"
    - service: script.bedroom_dimmer_loop_2

  bedroom_dimmer_loop_2:
    alias: Bedroom Lamp Dimmer Part 2
    sequence:
    - delay:
        milliseconds: 100
    - data_template:
        entity_id: >
            {% if is_state("input_select.bedroom_lamp", "White") %}
              light.bedroom_lamps_white
            {%-elif is_state("input_select.bedroom_lamp", "Colour") %}
              light.bedroom_lamps_colour
            {%-elif is_state("input_select.bedroom_lamp", "All") %}
              light.bedroom_lamps
            {% endif %}
        brightness_step: 5
      service: light.turn_on
    - service: script.bedroom_dimmer_loop
1 Like