Toggling an LED between two values (50% & 100%)

Working my way through a a program to drive a LOLIN 2.4" Display (ILI9341 & XPT2046) step by step, I have got this far:

output:
  - platform: ledc
    pin: GPIO22 #Silkscreen D1 jumpered to TFT-LED (backlight)
    id: backlight
  
light:
  - platform: monochromatic
    output: backlight
    id: backlightctl
    name: backlightctl
    restore_mode: ALWAYS_ON

touchscreen:
  platform: xpt2046
  id: my_touchscreen
  cs_pin: GPIO17 #touch T_CS
  update_interval: 50ms
  threshold: 400
  on_touch:
    - if:
        condition:
          - light.is_on:
              id: backlightctl
        then:
          - light.dim_relative: 
              id: backlightctl
              relative_brightness: -50%
        else:
          - light.turn_on:
              id: backlightctl

With the above my display will switches between three states: 100%, 50% & OFF.
How can I test the current state of backlightctl so that I can toggle between 100% and 50%?

Regards, Martin

1 Like

You need an if-then-else

If - brightness is 100%

then - brightness 50%

else

if - brightness 50%

then - brightness 100%

Its much easier if you just think about what you want to happen step by step.

Your also using “relative brightness” which if you read the documentation dims a light by the % you set so, at 50% if you press it again it will keep going down by -50%.

How do you get from 50% to 100%? Not by setting relative brightness-50%.

Also, it looks like your setting this up wrong. Anytime you touch the screen then it will toggle tour brightness. That doesnt make a “touch” screen very useful if it bounces between 100% and 50% each time you use it.

Seeing as were in 2024, a lot has changed since April of 2021 and those old posts arent all that helpful for thing, especially simple things like changing brightness.

Unless I need to change my glasses, OP is asking how to read brightness from an ESPHome light.

I’m pretty sure that didn’t change since 2021.

Thank you all for your advice. For the benefit of any esphomer’ers that are looking for clues to address this problem, this was my solution:

output:
  - platform: ledc
    pin: GPIO22 #Silkscreen D1 jumpered to TFT-LED (backlight)
    id: backlight
 

light:
  - platform: monochromatic
    output: backlight
    id: backlightctl
    name: backlightctl
    restore_mode: ALWAYS_ON

touchscreen:
  platform: xpt2046
  id: my_touchscreen
  cs_pin: GPIO17 #touch T_CS
  update_interval: 50ms
  threshold: 400
  on_touch:
    - if:
        condition:
          - lambda: 'return id(backlightctl).current_values.get_brightness() == 0.5;'
        then:
          - light.dim_relative: 
              id: backlightctl
              relative_brightness: +50%
        else:
          - light.dim_relative: 
              id: backlightctl
              relative_brightness: -50%

Regards, M.

1 Like