How do I increase brightness while button held with ESPHome via MQTT

Tags: #<Tag:0x00007f7395c76860>

I have a H801 LED controller with ESPHome and a button attached. Pressing the button, turns the light on/off, holding it dims the light - I need an additional button at a place where physical wiring is impossible - and it needs to work without direct connection to HA (hence api disabled). I have made a configuration that works to turn the light on/off, but I can not figure out how to recreate the dimming while holding the button (both code pieces below). I guess I need some sort of while loop that increments a number from 0 to 255 (brightness), before publishing the MQTT topic - but I can not figure it out. Some advise would be appreciated. It works on the H801 because I can use the relative_brightness function there - but that’s not an option on the remote ESPHome device.

Here’s the configuration for the H801 LED controller with the physical button attached:


esphome:
  name: esp28
  platform: ESP8266
  board: esp01_1m

wifi:
  use_address: 192.168.2.108
  networks:
  - ssid: "SEL_Wifi"
    password: "xxx"
  - ssid: "Boatnet"
    password: "xxx"
    manual_ip:
      static_ip: 192.168.2.108
      gateway: 192.168.2.1
      subnet: 255.255.255.0
      dns1: 192.168.2.1
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp28 Fallback Hotspot"
    password: "XX"

captive_portal:

# Enable logging
logger:

# Enable MQTT
mqtt:
  broker: xxx
  username: xxx
  password: xxx
  port: xxx
  

# Enable Home Assistant API
#api:

ota:
sensor:
  - platform: wifi_signal
    name: "WiFi Signal ESP28 h801"
    update_interval: 60s
#RGBWarm
output:
  - platform: esp8266_pwm
    pin: 12
    frequency: 1000 Hz
    id: pwm_b
  - platform: esp8266_pwm
    pin: 13
    frequency: 1000 Hz
    id: pwm_g
  - platform: esp8266_pwm
    pin: 15
    frequency: 1000 Hz
    id: pwm_r
  - platform: esp8266_pwm
    pin: 14
    frequency: 1000 Hz
    id: pwm_w
  - platform: esp8266_pwm
    pin: 16
    frequency: 1000 Hz
    id: pwm_cw
light:
  - platform: rgb
    name: "H801 RGB"
    id: h801_rgb
    red: pwm_r
    green: pwm_g
    blue: pwm_b
    #white: pwm_w
  - platform: cwww
    name: "H801 white"
    id: h801_ww
    #red: pwm_r
    #green: pwm_g
    #blue: pwm_b
    cold_white: pwm_cw
    warm_white: pwm_w
    cold_white_color_temperature: 153 mireds
    warm_white_color_temperature: 500 mireds
binary_sensor:
  - platform: gpio
    pin: GPIO03
    id: light_0_touch
    on_click:
      then:
        - if:
            condition:
              light.is_off: h801_ww
            then:
              light.turn_on: 
                id: h801_ww
                brightness: 1.0
                color_temperature: 500 mireds
            else:
              - light.turn_off: h801_ww
              - light.turn_off: h801_rgb
    on_press:
      then:
      - if:
          condition: 
            light.is_off: h801_ww
          then:
          - delay: 0.5s
          - while:
              condition:
                binary_sensor.is_on: light_0_touch
              then:
                - light.dim_relative:
                    id: h801_ww
                    relative_brightness: 5%
                    transition_length: 0.1s
                - delay: 0.1s
          else:
          - delay: 0.5s
          - while:
              condition:
                and:
                  - binary_sensor.is_on: light_0_touch
                  - light.is_on: h801_ww
              then:
                - light.dim_relative:
                    id: h801_ww
                    relative_brightness: -5%
                    transition_length: 0.1s
                - delay: 0.1s

Heres the code on the remote ESPHome, it currently works fine for on/off functionality, but this is where I need some way of increasing the brightness while holding the button.

esphome:
  name: esp41
  platform: ESP8266
  board: nodemcuv2

wifi:
  use_address: 192.168.2.109
  networks:
  - ssid: "Boatnet"
    password: "xxx"
    manual_ip:
      static_ip: 192.168.2.109
      gateway: 192.168.2.1
      subnet: 255.255.255.0
      dns1: 192.168.2.1
  - ssid: "SEL_Wifi"
    password: "xxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp41 Fallback Hotspot"
    password: "xxx"

captive_portal:

# Enable logging
logger:

# Define virtual sensor to store state of light
text_sensor:
  - platform: template
    name: "ESP28 light state"
    id: light_state
    internal: true

# Enable MQTT
mqtt:
  broker: xxx
  username: xxx
  password: xxx
  port: xxx
  
  on_json_message:
    topic: esp28/light/h801_white/state
    then:
      - logger.log: "JSON payload received"
      - text_sensor.template.publish:
          id: light_state
          state: !lambda 'return x["state"];'
      
# Enable Home Assistant API
#api:

ota:

sensor:
  - platform: wifi_signal
    name: "WiFi Signal ESP41"
    update_interval: 60s
binary_sensor:
  - platform: gpio
    pin: 
      number: D2
      mode: INPUT_PULLUP
    id: touch
    
    on_press:
      then:
        - logger.log: "Press registered"
        - if:
            condition:
              text_sensor.state:
                id: light_state
                state: 'OFF'
            then:
              - logger.log: "Publishing ON command"
              - mqtt.publish_json:
                  topic: esp28/light/h801_white/command
                  payload: |-
                    root["state"] = "ON";
                    root["brightness"] = "255";
                    root["color_temp"] = "500";
        - if:
            condition:
              text_sensor.state:
                id: light_state
                state: 'ON'
            then:
              - logger.log: "Publishing OFF command"
              - mqtt.publish_json:
                  topic: esp28/light/h801_white/command
                  payload: |-
                    root["state"] = "OFF";