How can I force a restart?

I have a button defined in my node:

button:
  - platform: restart
    name: "Solar_Restart"
    id: solarrestart

I can restart the ESP from a button on my Home Assistant front-end.

Is there a way to force a restart without the button? Here is the script where I would like to reset the ESP when the OTA mode switch is detected as “off”. Alternately, can I force the ESP to go into deep sleep here?

# Script to test if the otamode switch is on or off
script:
  - id: test_ota
    mode: queued
    then:
      - delay: 10s
      - logger.log: "Checking OTA Mode"
      - if:
          condition:
            binary_sensor.is_on: otamode_button
          then:
            - logger.log: 'OTA Mode ON'
            - deep_sleep.prevent: deep_sleep_handler
          else:
            - logger.log: 'OTA Mode OFF'
            ### This is where I would like to reset the ESP ###
      - script.execute: test_ota

You can send a press event to your restart button:


          else:
            - logger.log: 'OTA Mode OFF'
            - lambda: id(solarrestart).press();

I really need to learn lambdas, especially considering that I am somewhat fluent in basic C++.
Where are events documented?

Well, a lot can be found in the documentation… I just looked at Button Component — ESPHome and TIL that a lambda wasn’t even necessary, you can also use:

- button.press: my_button

Given the fact that you are somewhat fluent in basic C++, looking at the .h files on the esphome github repo is the most complete documentation you can find :grinning:

Thanks for the tip.

The Home Assistant and ESPHome documentation is quite thorough, but there is a problem. To find anything, you need to know what to look for. I probably spent two hours looking in the documentation and on the forums for ‘restart’ or ‘reboot’ or ‘reset’ before I asked the question.

But, I learned something new today.

I came back to this because I was pondering the answer. @jsuanet 's solution works and is correct, but I also looked at the c++ code for both button_restart and switch_restart and noticed that both call pretty well the same code, which you could put into a lambda

https://esphome.io/api/restart__switch_8cpp_source.html

I have to learn to like Lambda’s. One of my long-term projects is to move the code for an Arduino project I wrote a couple of years ago. It is on a Wemos D1 and consists of two four-digit 14-segment LED displays. The data comes from Node Red over MQTT, but I have often wondered if I couldn’t send it using the API and have ESPHome on the Wemos D1.

It is not a need-to finish project, it’s supposed to be a learning experience. Maybe I’ll start by turning an LED on and off using Lambdas.

There is no need to turn a led on/off via a lambda as that functionality is built in. Lamda is really only for doing stuff that is not doable “natively” in esphome.

I know that, but I need to start understanding C++ in Lambda’s somewhere.

Ahhh OK fair enough.