"initial_state: off" for ESPHome light

Is it possible to set an initial state for an ESPHome light component as shown in HA on HA restart? I may just be missing something with the Sonoff Basic set up with the configuration below…

When I restart HA, light.under_stair_cupboard shows as on, regardless of the actual state of the device. I tried setting the initial state off in customize.yaml, but it did not have any effect.

Thanks.

# Substitutions
substitutions:
  devicename: light_under_stair_cupboard
  staticip: 192.168.xxx.xxx
  mqttclientid: lt_under_stair_cupboard
  birthtopic: light_status/under_stair_cupboard/availability
  lwttopic: light_status/under_stair_cupboard/availability
  lightid: under_stair_light
  lightname: "Under Stair Cupboard"
  binarysensorid: under_stair_cupboard_light_toggle
  

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m

# Enable WiFi
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pwd
  manual_ip:
    static_ip: $staticip
    gateway: 192.168.xxx.xxx
    subnet: 255.255.255.0

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# MQTT
mqtt:
  broker: !secret mqtt_broker
  port: !secret mqtt_port
  client_id: $mqttclientid
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery: clean
  birth_message:
    topic: $birthtopic
    payload: 'online'
  will_message:
    topic: $lwttopic
    payload: 'offline'
  keepalive: 5s

# Set up output to control relay - Required for light platform to control relay
output:
  - platform: gpio
    pin: GPIO12
    id: relay
    

# Set up light in light platform for HA
light:
 - platform: binary
   name: $lightname
   output: relay
   id: $lightid

# Set up binary sensor tied to Sonoff button and hide it from HA   
binary_sensor:
  - platform: gpio
    pin: GPIO0
    on_press:
      then:
        - light.toggle: $lightid
    id: $binarysensorid
    internal: true

status_led:
  pin:
    number: GPIO13
    inverted: false

I’ve never tried this, but, perhaps esphome: on_boot: can do what you want?

maybe you have some kind of retained message for the state of the device in your broker. check that and if it’s there then delete it and see what happens.

@swiftlyfalling Thanks, that looked promising. It did not work, but that turned out to be my own fault. See below. In any case, you prompted me to look more at ESPHome automations, which I hadn’t really explored yet.

@finity You were on the right track with it being an MQTT issue. Turns out that I didn’t change the birth and LWT topics pasted in from light state project when I moved to using substitutions for this light’s config, as well. So, the automation in that project was being triggered by the birth message to update the light’s status to on. Oops. My fault.

Thanks for your time, guys.

1 Like

Hi,

You mean this?

esphome:
  name: 'blitzwolf2'
  platform: ESP8266
  board: esp01_1m
  
  # Ensure the light turns on by default if the physical switch is actuated.
  on_boot:
    priority: 100 # Highest priority, ensures light turns on without delay.
    then:
      - light.turn_on: light
2 Likes

I appreciate this is old but just incase anybody comes across it like i did while searching there is now a
restore mode

light:
  - platform: binary
    name: "${plug_name}"
    output: relay
    id: light
    restore_mode: ALWAYS_ON
1 Like

If some how we can add ‘toggle’ option on_boot , this will help to control the light through switch as well. Lets say on boot light is first ON then If I again re press the switch it will set light to OFF so we will have added advantage of controlling esp-01 from switch as well as with dashboard.