HA sensor that represents the ESP32 LEDC output state

I have ESP32. I control the LEDC output from HA. But how can I check the actual state of the pin? For example in case of rebooting or power loss.

output:
  - platform: ledc
    pin: GPIO12
    frequency: 9765 Hz
    id: push_fan_speed

api:
  services:
    - service: set_push_value
      variables:
        push_value: int
      then:
        - output.set_level:
            id: push_fan_speed
            level: !lambda 'return push_value / 100.0;'

  - platform: template
    name: check pwm
    id: checkPWM
    lambda: return state(push_fan_speed);

You mean rebooting of Home Assistant?

There doesn’t seem a way to access the state of the ledc output. But you can use a global variable. Set the value in the “then” section of the service with a lambda and then return the value of the variable in the template sensors lambda. You don’t need to change very much to do it :slight_smile: