Turn on a switch by brightness

hello.

I need to turn on a switch if the brightness value of a light is =>1 and turn it off is <1. Is it possible from esphome, from the device’s yaml configuration file?

thank you.

If the brightness value is known to your esphome device, for sure.

What is the way to do it?

I create a template of a binary sensor that when the brightness attribute is greater than 1 it is in the on state and if not in off, and that this binary senser activates the switch

{% if state_attr('light.light_0', 'brightness') | int(0) > 1%} 
A
{% else %}
B
{% endif %}

Am I well on my way?

Your code doesn’t look like esphome code.
Post a scheme of your circuit and your esphome yaml.

This is the current configuration of my device, now I want to put a contact online to the pwm regulator to deactivate it when the brightness is less than 1

esphome:
  name: esp01
  friendly_name: ESP01

esp32:
  board: esp32-poe-iso
  framework:
    type: arduino

# Enable logging
logger:
  #level: VERY_VERBOSE

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  - platform: esphome
    password: "xxx"

#wifi:
#  ssid: !secret wifi_ssid
#  password: !secret wifi_password

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

#captive_portal:

#esp32_ble_tracker:
#  scan_parameters:
#    interval: 1100ms
#    window: 1100ms

#bluetooth_proxy:

ethernet:
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO17_OUT
  phy_addr: 0
  power_pin: GPIO12

button:
  - platform: restart
    name: "Restart"

i2c:
  - id: bus_a
    sda: GPIO13
    scl: GPIO16
    scan: true

mcp23017:
  - id: 'mcp23017_hub'
    i2c_id: bus_a
    address: 0x20

light:
  - platform: monochromatic
    name: "Luz dormitorio"
    id: light_0
    output: light_0_out
    #gamma_correct: 0.8
    default_transition_length: 10ms

output:
  - platform: ledc
    pin: GPIO14
    id: light_0_out
    min_power: 0
    max_power: 1

#output:
#  - platform: ac_dimmer
#    id: light_0_out
#    gate_pin: GPIO5
#    method: leading
#    init_with_half_cycle: true
#    min_power: 0.15
    #max_power: 0.95
#    zero_cross_pin:
#      number: GPIO4
#      mode:
#        input: true
#        pullup: true
#      inverted: true

cover:
# Cover a
  - platform: time_based
    name: "Persiana aseo"
    id: cover_a
    device_class: shutter

    open_action:
      - switch.turn_on: open_cover_switch_a
    open_duration: 30.5s

    close_action:
      - switch.turn_on: close_cover_switch_a
    close_duration: 30.5s

    stop_action:
      - switch.turn_off: open_cover_switch_a
      - switch.turn_off: close_cover_switch_a

# Cover b
  - platform: time_based
    name: "Persiana dormitorio"
    id: cover_b
    device_class: shutter

    open_action:
      - switch.turn_on: open_cover_switch_b
    open_duration: 30.5s

    close_action:
      - switch.turn_on: close_cover_switch_b
    close_duration: 30.5s

    stop_action:
      - switch.turn_off: open_cover_switch_b
      - switch.turn_off: close_cover_switch_b


switch:
  - platform: gpio
    internal: true
    id: B7
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B7
      number: 15
      mode:
        output: true
      inverted: true
  - platform: gpio
    internal: true
    id: A1
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A1
      number: 1
      mode:
        output: true
      inverted: true
  - platform: gpio
    internal: true
    id: A2
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A2
      number: 2
      mode:
        output: true
      inverted: true
  - platform: gpio
    internal: true
    id: A3
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A3
      number: 3
      mode:
        output: true
      inverted: true

# Cover a
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A7
      number: 7
      mode:
        output: true
      inverted: true
    id: open_cover_switch_a
    interlock: [close_cover_switch_a]
    #interlock_wait_time: 200ms
    name: "shutter Up"
    icon: "mdi:window-shutter-open"
   
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A6
      number: 6
      mode:
        output: true
      inverted: true
    id: close_cover_switch_a
    interlock: [open_cover_switch_a]
    #interlock_wait_time: 200ms
    name: "shutter Down"
    icon: "mdi:window-shutter"

# Cover b
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A5
      number: 5
      mode:
        output: true
      inverted: true
    id: open_cover_switch_b
    interlock: [close_cover_switch_b]
    #interlock_wait_time: 200ms
    name: "shutter Up"
    icon: "mdi:window-shutter-open"
   
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A4
      number: 4
      mode:
        output: true
      inverted: true
    id: close_cover_switch_b
    interlock: [open_cover_switch_b]
    #interlock_wait_time: 200ms
    name: "shutter Down"
    icon: "mdi:window-shutter"


binary_sensor:

# Shutter a
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B0
      number: 8
      mode:
        input: true
        #pullup: true
      inverted: true
    name: shutter Up
    on_press:
      - lambda: |-
          if (id(cover_a).current_operation == CoverOperation::COVER_OPERATION_OPENING){
            auto call = id(cover_a).make_call();
            call.set_command_stop();
            call.perform();
          }
          else if (id(cover_a).current_operation == CoverOperation::COVER_OPERATION_CLOSING){
            auto call = id(cover_a).make_call();
            call.set_command_stop();
            call.perform();
          }
          else {
            auto call = id(cover_a).make_call();
            call.set_command_open();
            call.perform();
          }

  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B1
      number: 9
      mode:
        input: true
        #pullup: true
      inverted: true
    name: shutter Down
    on_press:
      - lambda: |-
          if (id(cover_a).current_operation == CoverOperation::COVER_OPERATION_CLOSING){
            auto call = id(cover_a).make_call();
            call.set_command_stop();
            call.perform();
          }
          else if (id(cover_a).current_operation == CoverOperation::COVER_OPERATION_OPENING){
            auto call = id(cover_a).make_call();
            call.set_command_stop();
            call.perform();
          }
          else {
            auto call = id(cover_a).make_call();
            call.set_command_close();
            call.perform();
          }

# Shutter b
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B2
      number: 10
      mode:
        input: true
        #pullup: true
      inverted: true
    name: shutter Up
    on_press:
      - lambda: |-
          if (id(cover_b).current_operation == CoverOperation::COVER_OPERATION_OPENING){
            auto call = id(cover_b).make_call();
            call.set_command_stop();
            call.perform();
          }
          else if (id(cover_b).current_operation == CoverOperation::COVER_OPERATION_CLOSING){
            auto call = id(cover_b).make_call();
            call.set_command_stop();
            call.perform();
          }
          else {
            auto call = id(cover_b).make_call();
            call.set_command_open();
            call.perform();
          }

  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B3
      number: 11
      mode:
        input: true
        #pullup: true
      inverted: true
    name: shutter Down
    on_press:
      - lambda: |-
          if (id(cover_b).current_operation == CoverOperation::COVER_OPERATION_CLOSING){
            auto call = id(cover_b).make_call();
            call.set_command_stop();
            call.perform();
          }
          else if (id(cover_b).current_operation == CoverOperation::COVER_OPERATION_OPENING){
            auto call = id(cover_b).make_call();
            call.set_command_stop();
            call.perform();
          }
          else {
            auto call = id(cover_b).make_call();
            call.set_command_close();
            call.perform();
          }

# Ventana aseo
  - platform: gpio
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B4
      number: 12
      mode:
        input: true
        #pullup: true
      #inverted: true
    device_class: window
    name: Ventana aseo

# Ventana dormitorio
  - platform: gpio
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B5
      number: 13
      mode:
        input: true
        #pullup: true
      #inverted: true
    device_class: window
    name: Ventana dormitorio

# Dimmer a
  - platform: gpio
    internal: true
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin B6
      number: 14
      mode:
        input: true
        #pullup: true
      inverted: true
    name: light_0_touch
    id: light_0_touch
    on_click:
      then:
        - if:
            condition:
              light.is_off: light_0
            then:
              light.turn_on: 
                id: light_0
                brightness: 1.0
            else:
              light.turn_off: light_0
    on_press:
      then:
      - if:
          condition: 
            light.is_off: light_0
          then:
          - delay: 0.5s
          - while:
              condition:
                binary_sensor.is_on: light_0_touch
              then:
                - light.dim_relative:
                    id: light_0
                    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: light_0
              then:
                - light.dim_relative:
                    id: light_0
                    relative_brightness: -5%
                    transition_length: 0.1s
                - delay: 0.1s


  - platform: gpio
    internal: true
    name: "MCP23017 Pin A0"
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A0
      number: 0
      mode:
        input: true
        #pullup: true
      inverted: true

Brightness is a value between 0 and 1, so it can’t be >1
For 1% it could be something like:

condition:
  lambda: 'return id(light_0).current_values.get_brightness() > 0.01f;'

Hello.

I have finally found a more practical way to manage the switch than using the brightness attribute.

I have used the on_turn_on and on_turn_off functions, and my code looks like this:

  - platform: monochromatic
    name: "Luz dormitorio"
    id: luz_dormitorio
    output: luz_dormitorio_out
    #gamma_correct: 0
    default_transition_length: 10ms
    on_turn_on:
      then:
        - switch.turn_on: luz_dormitorio_switch
    on_turn_off:
      then:
        - switch.turn_off: luz_dormitorio_switch

Thank you so much.