Esphome get output state (light state)


Is there a way to get the value/state of an output/PWM light.
I have manual buttons on my esp with automation (inside esphome) that turns on the light in 3 steps (20%, 50%, off).
I need to get the value (inside of esphome) of the light because if i turn on the light in home assistant the first push of the button should turn the light off if its already on.

my code in esphome:

  - id: led_val
    type: int
    initial_value: '0'
  - id: led_off
    type: bool
    initial_value: 'false'


  - platform: esp8266_pwm
    pin: D7
    frequency: 600 Hz
    id: led_pwm

  - platform: monochromatic
    output: led_pwm
    id: podlaha_light
    name: "Podlaha LED"

  - platform: gpio
      number: D6
      mode: INPUT_PULLUP
      inverted: True
    name: Tlacidlo dialkove
      - delayed_on: 10ms
        - if:
              lambda: 'return id(led_val) == 50;'
              - logger.log: "je 50, vypinam"
              - light.turn_off: 
                  id: podlaha_light
                  transition_length: 1s
              - lambda: |-
                  id(led_off) = true; id(led_val) = 0;
        - if:
                - lambda: 'return id(led_val) == 20;'
                - lambda: 'return id(led_off) == false;'
              - logger.log: "is 20, changing "
              - lambda: |-
                  id(led_val) = 50;
              - light.turn_on:
                  id: podlaha_light
                  brightness: 50%
                  transition_length: 2s
        - if:
              - lambda: 'return id(led_val) == 0;'
              - lambda: 'return id(led_off) == false;'
              - logger.log: "is 0, turning on"
              - lambda: |-
                  id(led_val) = 20;
              - light.turn_on:
                  id: podlaha_light
                  brightness: 20%
                  transition_length: 2s
        - lambda: |-
            id(led_off) = false;

Hi there…Did you add the light to the esphome integration in HA? If you had done it, you should be able to see the brightness of the light as a state attribute of the light.podlaha_led in HA.

The brightness of the light is available in home assistant like any other light.

The automation is not in the home assistant. The automation is in the esp. I want to change the led_val if i turn the light on in home assistant. Because now my automation doesn’t now that home assistant turned on the light and always starts from zero.

Could you use the text sensor to get the state of the light from Home Assistant and use that in your ESPHOME automation ?

For example if i turn the light on in HomeAssistant. And then i push the button on the esp the light should turn off. Only if the light is turned off the esp automation should run.

I want the automation to work in esp without homeAssistent. Now it works without the homeAssistant but i am able to use it also inside homeAssistant. But i doesn’t have feedback from the homeAssistant if i turn on the light there. And therefore my automation starts from zero (20, 50, off)

Not sure I follow, above you say you want to know the HA state of the light in ESPHOME ?

In HA all works. But i want to get state from output in espHome inside the yaml program of the espHome.

Do you understand what I’m trying to say? Or am I not expressing myself good enough?

Do you just want to query the state of the light?

Then you do not need the globals at all.


will get you the current brightness value of the light (from 0 to 1. So 0.5 means 50%)

Thank you, this was what i needed. One question now. Where can i find this type of functions (and their description) of the esphome?

That is an excellent question.

1 Like

A litle problem. The

value doesnt go to zero. If i turn off the light the brightness value returns the last value before turning off.

On every components page in the ESPHome documentation is a link “API Reference” at the bottom. You will get to the documentation of the C++ API. But there is mostly no description. You have to guess or to look into the source code. The code is linked inside the API reference.

Yes that is by design. So that a light will have the same brightness when you switch if off and then on again.

You can combine it with the value from: id(podlaha_light).current_values.is_on()


Where is this type of documentation where i can find this type of functions?

Thank you

Every page of a component in the ESPHome documentation has a “API Reference” link at the bottom. You can use that and have a look around.

1 Like

Yeah, it’s too complicated for me :frowning: to find anything.
But thank you