Is there a way to perform different startup behaviors based on restart vs power loss with ESPHome?

I have both ESPHome bulbs and shellys running ESPHome on the same fixtures (I want both color/temp changing and detached switches). Sometimes the bulbs misbehave so the shelly’s are set up to toggle the relay if the bulbs aren’t responding. For this reason, I have the bulbs configured to restore_mode: RESTORE_AND_ON so they light when power is turned back on and I don’t have a screaming wife. The problem is, when the bulbs decide to restart in the middle of the night because of API or wifi issues, the lights also turn on and I have a screaming wife.

Is there a way to differentiate between a restart and a power cycle and perform different startup behaviors based on this (i.e. restart: lights do not restore on. power cycle: Lights restore on)? If not, is this something that is possible to implement if I submitted a feature request?

Thanks in advance.

You could take a look at what reset_reason: produces and look to use that with an on_boot: automation?

You could also look at extending or removing reboot_timeout (for both api and WiFi).

I also have a “sleep mode” for my place and conditions in the automations that block these kinds of things at night etc…

Finally if you do nothing you probably won’t have a screaming wife, or even a wife at all :wink:

1 Like

I didn’t know you could create a text sensor for reset reason, so thanks for this. Are there downsides to using the debug level logger like memory use or flash health?

I have an automation but that doesn’t stop the light from turning on immediately after the reboot since it takes a bit to connect.

lol. She’s been pretty cool with it overall but definitely gets annoyed when it doesn’t work. I often remind her that the main reason I got into automation is because she can’t turn off a %&$!ing light in the house (not that we’ve saved any time or money switching to automation)

1 Like

Ok I’m trying to use on_boot with the reset reason text sensor but I’m having trouble getting to work. I’m guessing there is something wrong with the lambda function?

[15:51:20][D][text_sensor:064]: 'Reset Reason': Sending state 'Software/System restart'
esphome:
  name: ${name}
  friendly_name: ${friendlyname}
  name_add_mac_suffix: false
  on_boot:
    - priority: -100
      then:
        if:
          condition:
            - lambda: 'return id(reset_reason).state == "Software/System restart";'
          then:
            - light.turn_off:
                id: light_1
          else:
            - light.turn_on:
                id: light_1

text_sensor:
  - platform: debug
    reset_reason:
      id: reset_reason
      name: "Reset Reason"

light:
  - platform: rgbww
    id: light_1

Lambda of such can be used:

            - if:
                condition:
                  - lambda: 'return system_get_rst_info()->reason == 0;'

0 - Power reboot
1 - Hardware WDT reset
2 - Fatal exception
3 - Software watchdog reset
4 - Software reset
5 - Deep-sleep
6 - Hardware reset

2 Likes

This is the way. Can I ask where you found this at?

1 Like

Are you not able to control light color within on_boot?
I am able to turn the lights on/off but it seems to default to a color temp somewhere around around 3500K.

  on_boot:
    - priority: 600
      then:
        if:
          condition:
            - lambda: 'return system_get_rst_info()->reason == 4;'
          then:
            if:
              condition:
                light.is_on: light_1
              then:
                - light.turn_on:
                    id: light_1
                    brightness: 100%
                    red: 100%
                    green: 0%
                    blue: 0%
                    warm_white: 100%
              else:
                - light.control:
                    id: light_1
                    state: off                
#            - lambda: |-
#                if (id(light_1).state) {
#                id(light_1).turn_on();
#                } else {
#                id(light_1).turn_off();
#                }
          else:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 100%
                green: 0%
                blue: 0%
                warm_white: 100%

I was able to get the color to work with a lambda call

For anyone looking for a similar feature this is my final test code:

  on_boot:
    - priority: 600
      then:
        if:
          condition:
            - lambda: 'return system_get_rst_info()->reason == 4;'
          then:
            if:
              condition:
                light.is_on: light_1
              then:
                - lambda: |-
                    auto call = id(light_1).turn_on();
                    call.set_brightness(1.0);
                    call.set_red(1.0);
                    call.set_green(0.0);
                    call.set_blue(0.0);
                    call.set_cold_white(0.0);          
                    call.set_warm_white(1.0);
                    call.set_save(false);
                    call.perform();
              else:
                - light.control:
                    id: light_1
                    state: off                
          else:
            - lambda: |-
                auto call = id(light_1).turn_on();
                call.set_brightness(1.0);
                call.set_red(1.0);
                call.set_green(0.0);
                call.set_blue(0.0);
                call.set_cold_white(0.0);          
                call.set_warm_white(1.0);
                call.set_save(false);
                call.perform();

This obviously is setup for an rgbww bulb with the id light_1 so you’ll have to change for your setup.

This was very helpful:

the note under here was also helpful:

Thanks Everyone

2 Likes

Come to think of it, I’m not sure that debug sensor will be set up in time for when you need it (very early during boot).

To that extent, you may be better off trying something lower level like Maxim has suggested. Not sure though.

Edit: reading on I see you’ve got that as a solution…

In the espressif pdf

Can you share the complete code?
I am getting the error below:
error: ‘system_get_rst_info’ was not declared in this scope

I’ll try to later, I can’t right now. In the meantime, I believe you are getting the error because you probably don’t have debug as the logger level and and the debugger component setup.

See Mahko_Mahko’s comment above, but you don’t need to set up the text_sensor:

adding these should correct the error:

logger:
  level: debug  ##<---- place under your logger entry

debug: