Sonoff D1 dimmer - how to read current brightness state?

I’m playing with D1 dimmer in my ESPhome. I’ve pretty much all figured it out, except dimming down. I use “relative_brightness” for dimming, and dimm up works fine. BUT… if i dimm down, when it comes to zero light kinda flashes, then it turns off. First, i don’t want that flash, but more important: i’d like to dimm down to 1%, not to zero (which turns light off).
How to read and take into account current brightness status? I guess i need some kind of lambda function to implement into “IF” sentence.
Any help very appreciated…

My current working principle:

  • short push: if OFF, light goes to 100%, if ON goes OFF.
  • long push: if OFF, light goes to 1%, if ON goes into dimming. Release and long re-press reverses direction.

I use global variable “dimanje” for dimm direction.

my current code:

substitutions:
  devicename: d1_dimmer
  friendly_name: Dimmer

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m
  # Include our custom code
  includes:
    - d1_dimmer_no_rf.h
    
globals:
   - id: dimanje
     type: bool
     initial_value: 'true'
         
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  #use_address: power.local
  manual_ip:
    static_ip: 192.168.0.95
    gateway: 192.168.0.1
    subnet: 255.255.255.0
    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Power Fallback Hotspot"
    password: !secret ota_password
    ap_timeout: 5min

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret ota_password

ota:
  password: !secret ota_password

# TODO: Set these as your own web server credentials
web_server:
  port: 80

# Uses the onboard LED as status indicator
status_led:
  pin:
    number: GPIO13
    inverted: True

# Report basic device information like it's Wi-Fi signal strength and uptime
sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi Signal
    update_interval: 60s
  - platform: uptime
    name: ${friendly_name} Uptime
    filters:
      - lambda: return x / 60.0;
    unit_of_measurement: minutes
    
# Basic switch to allow you to restart the device remotely
switch:
  - platform: restart
    name: ${friendly_name} Restart
          
binary_sensor:
  - platform: status
    name: "Sonoff D1 Status"
    
  - platform: gpio
    id: tipka
    internal: true
    pin:  
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: true

    on_multi_click:
      - timing:
          - ON for at most 0.5s
        then:
          if:
            condition:
              light.is_on: main
            then:
                - light.turn_off:
                    id: main
            else:
                - light.turn_on:
                    id: main
                    brightness: 100%
                #označim, da je naslednja faza dim DOL
                - lambda: |-
                          id(dimanje) = (false);

      - timing:
        - ON for at least 0.51s
        then:
          #če je izključena, vklopim na 1%
          if:
            condition:
              light.is_off: main
            then: 
              - light.turn_on:
                  id: main
                  brightness: 1%
            #sicer dimmam  
            else:
              - if:
                  condition:
                    lambda: |-
                      ESP_LOGD("Stanje", "Boolean: %d", id(dimanje));
                      return id(dimanje);
                  then:
                    - while:
                        condition:
                          binary_sensor.is_on: tipka
                        then:
                          - light.dim_relative:
                              id: main
                              relative_brightness: 10%
                              transition_length: 0.5s
                          - delay: 0.5s   
                          - lambda: |-
                               id(dimanje) = (false);
                  else:
                    - while:
                        condition:
                          binary_sensor.is_on: tipka
                        then:
                          - light.dim_relative:
                              id: main
                              relative_brightness: -20%
                              transition_length: 0.5s
                          - delay: 0.5s    
                          - lambda: |-
                              id(dimanje) = (true);
                  

# Define our custom light component
light:
  - platform: custom
    lambda: |-
      auto dimmer_light = new Sonoff_D1_Dimmer();
      App.register_component(dimmer_light);
      return {dimmer_light};
    lights:
      - name: $friendly_name
        default_transition_length: 100 ms
        id: main
        restore_mode: RESTORE_DEFAULT_OFF
        gamma_correct: 0