Displaying state of HA binanry sensors using LED's on ESP8266

I am trying to figure out how to display the state of a binary sensor using a LED on an ESP8266 … But nothing really happens…

This is my config:

esphome:
  name: doorindicator-1
  friendly_name: DoorIndicator-1

esp8266:
  board: esp01_1m

# Enable logging
logger:
  level: debug

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

ota:
  password: "xxx"

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

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

captive_portal:

# Enable On-Board Status LED.
output:
  - platform: gpio
    id: status_led
    pin: GPIO4

binary_sensor:
  - platform: homeassistant
    id: bryggers_dorlas_contact
    entity_id: binary_sensor.bryggers_dorlas_contact
    on_state:
      then:
        - lambda: |-
            ESP_LOGD("bin_Sensor","State changed");
            if (id(bryggers_dorlas_contact).state) {
              id(status_led).turn_on();
            } else {
              id(status_led).turn_off();
            }

When the state of the sensor changes in HA, nothing happens/is logged on the ESP device…
Also - I am not sure what happens on boot of the device - will it read the state of the HA sensor, or will nothing happen before the state actually changes ?
I would really like it to fetch the state on reboot, so that the LED indication always is right…

Is the device integrated ok?

If you add say an uptime sensor, does that appear in HA?

While you lambda looks fine to me, on_press and on_release can be simpler to read and debug.

You can look at publish_initial_state too.

link the output to a light component. like…

output:
  - platform: gpio
    id: status_led
    pin: GPIO4
light: 
  - platform: binary
    name: "Status_LED"
    output: status_led

The state is fetched from HA constantly, over and over, you don’t need to add an on_boot for that. You don’t need to add the epsLOG function either, It will show the state change in the log without that already.

on_state:
      then:
        lambda: |-        
          if(id(sbryggers_dorlas_contact).state) {
                id(status_led).turn_on();
           } else {
              id(status_led).turn_off();
            }

Your board is integrated right? You’ve flashed it and its showing up in HA as “online”?

Ah… Doh! … Creating the device is ESPhome is not enough … :wink:

Adding it solved the issue

:wink: