Switch state not remembered on boot

I have a switch like this:

switch:
  - platform: template
    name: Day/Night Switch
    id: day_night
    optimistic: True
    restore_mode: RESTORE_DEFAULT_OFF

This switch is always off when the device (a CYD ESP32) is (re)booted. Any clue why?

That’s a clue for sure

If it fails then it will restore off.
So the reason is that it’s not saved.
Do you get the saved to flash message in the log when you flip the switch?

That’s what your code asks if it can’t restore the state.
Do you have restore_from_flash option enabled (for Esp8266)?

1 Like

ESPHome Core Configuration — ESPHome

According to the first post it’s a ESP32.
As far as I know you don’t need any extra configurations for ESP32.
But if the time is too high or the fingers are to itchy to reboot then it might not have been saved.

Ahh, my bad.
One line of text was too much :sweat_smile:

What are you doing with a “day/night” switch?

This is all I see:

[12:11:53][D][switch:020]: 'Day/Night Switch' Toggling ON.
[12:11:53][D][switch:055]: 'Day/Night Switch': Sending state ON

thats the switch that we use to indicate we are sleeping. It turn off all the lights and makes sure that the bathroom lights are minimum brightness so that if you to the loo in the middle of the night the light doesn’t wake you up.

I realize this is not an answer to the question but a workaround.
But whatever logic is toggling the switch could be coded in ESP-Home as a binary sensor.

Could you try an input text or input number and see of it saves the values?
Maybe there is something wrong with the switch

Long time ago I had some weird behavior with template switch, I resolved it using dummy gpio switch instead (some unused gpio).

I just “fixed” it by using the code below:

time:
  - platform: homeassistant
    id: esptime
    on_time_sync:
      then:
        - logger.log: "Synchronized system clock"
        - if:
            condition:
                lambda: 'return (id(esptime).now().hour > 21 or id(esptime).now().hour < 6);'
            then:
               - display.page.show: time_page
               - switch.turn_on: day_night
type or paste code here