RESOLVED - Homeassistant.service "light.turnon" not functioning when I dynamically set the white_level using rotary_encoder

Hi,

I have recently discovered ESPhome and am very impressed with how easy it is to use and most importantly its stability, its great!.

I have however been struggling to make a certain function work as I want it to, I’ve bought 4 Zemismart downlighters which via. Tuya convert now have ESPhome flash onto them and are working brilliantly. I would like to replicate the function of a standard dimmer switch using a rotary encoder + a D1 mini, this is where I’m having issues.

from the D1mini I am able to control both the colour, brightness and white_level that are applied to each light when a button is pressed and/or released if I set static values in the code. Unfortunately I’m having issues getting the same to work when I use the output from the rotary_dimmer to set the white_level (or brightness).

Am I missing something simple? After a bit of searching I discovered that the value of white_level needs to be an integer so limited the accuracy to 0 decimal places. However this does not appear to have resolved the issue. I’m fairly certain the syntax of the !lambda is correct (it compiles and is formatted the same as others code snippets).

Here’s my code, its a bit messy as I’ve been experimenting with scripts etc. but functionally it work (apart from the dimmer bit).

esphome:
  name: test_node
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "xxxxxxxxxxxx"
  password: "xxxxxxxxxxxx"

# Enable logging
logger:
  level: VERBOSE
# Enable Home Assistant API
api:
  password: 'xxxxxxxxxxxx'

ota:
  password: 'xxxxxxxxxxxx'

globals:
  - id: brightness
    type: int
    initial_value: '1024'
  - id: white_level
    type: int
    initial_value: '256'

sensor:
  - platform: rotary_encoder
    name: "test_dimmer"
    id: rotary_encoder
    pin_a: GPIO5
    pin_b: GPIO4
    min_value: 0
    max_value: 16
    accuracy_decimals: 0
    filters:
      - or:
        - debounce: 0.1s
        - delta: 10
      - lambda: |-
          if (x < 0) return 0;
          if (x > 16) return 16;
          return x;
    on_value:
      then:
        - lambda: |-
              id(white_level) = x*16;
        - script.execute: test_script
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: light.zemismart_light1
              white_value: !lambda |- 
                return x*16;

binary_sensor:
  - platform: gpio
    pin:
      number: 14
      mode: INPUT_PULLUP
    name: "test_dimmer_switch"
    id: test_dimmer_switch
    on_press:
      then:
        - homeassistant.service:
            service: light.toggle
            data:
              entity_id: light.extension_lights
  - platform: gpio
    pin:
      number: 12
      mode: INPUT_PULLUP
    name: "color_switch"
    id: color_switch
    on_press:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: light.zemismart_light1
              white_value: 255
              brightness: 0
    on_release:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: light.zemismart_light1
              white_value: 0
              brightness: 255
              color_name: "orange"
script:
  - id: test_script
    then:
      - logger.log:
          format: "value of whitelevel is %i"
          args: [ 'id(white_level)' ]

Its had me stumped for the best part of a week with little progress. Hopefully someone can point out my likely simple mistake! :slight_smile:

Thanks.

after more digging and searching I came across this post which worked a treat with some tweaking.