Help creating yaml for ESPHome Auto-Restart Script

I came across this but I do not have a sonoff.

Do I have to remove the comments in my yaml?

Is there anything I would have to modify or delete since I’m not using a Sonoff ESPHome device?

I’m using another ESPHome smart plug.

# Basic Config
esphome:
  name: sonoff-cable-modem
  platform: ESP8266
  board: esp01_1m


  libraries:
    - ESP8266WiFi
    - https://github.com/akaJes/AsyncPing#95ac7e4

external_components:
  - source:
      type: git
      url: https://github.com/trombik/esphome-component-ping
      ref: main

wifi:
  ssid: xxxxxxxxxxxxxxxxxxx
  password: xxxxxxxxxxxxxxxxxxx

  ap:
    ssid: "Sonoff5"  # in case it can't connect and you need to reprogram it
    password: "xxxxxxxxxxxxxxxxxxx"

logger:
  baud_rate: 0 # (UART logging interferes with cse7766)

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

ota:
  password: "xxxxxxxxxxxxxxxxxxx"

# Device Specific Config
uart:
  rx_pin: RX
  baud_rate: 4800

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Cable Modem Button"
    on_press:
      - script.execute: power_cycle_relay
  - platform: status
    name: "Sonoff Cable Modem Status"

sensor:
  - platform: wifi_signal
    name: "Sonoff Cable Modem WiFi Signal"
    update_interval: 60s
  - platform: cse7766
    current:
      name: "Sonoff Cable Modem Current"
      accuracy_decimals: 1
    voltage:
      name: "Sonoff Cable Modem Voltage"
      accuracy_decimals: 1
    power:
      name: "Sonoff Cable Modem Power"
      accuracy_decimals: 1
  - platform: ping
    ip_address: 8.8.8.8
    id: ping_response
    num_attempts: 1
    timeout: 1sec
    loss:
      name: "Packet Loss to Internet"
      id: loss
    latency:
      name: "Latency to Internet"
      accuracy_decimals: 3
    update_interval: 30s

switch:
  - platform: gpio
    name: "Sonoff Cable Modem Relay"
    pin: GPIO12
    id: relay
    restore_mode: ALWAYS_ON

status_led:
  pin: GPIO13

globals:
  - id: last_power_cycle
    type: time_t
    restore_value: no
    initial_value: '0'
  - id: packet_loss_count
    type: int
    restore_value: no
    initial_value: '0'

# Ping time (in seconds)
interval:
  - interval: 60s
    then:
      - script.execute: ping_check


# Script for checking packet loss
script:
  - id: ping_check
    then:
      - if:
          condition:
            # Condition checks if packet loss has occurred
            lambda: 'return id(loss).state > 0;'
          then:
            # If packet loss has occurred, increment packet_loss_count
            - lambda: |- 
                id(packet_loss_count) += 1;
                // If there are 3 consecutive packet losses, reset the count and execute power_cycle_relay script
                if (id(packet_loss_count) >= 3) {
                  id(packet_loss_count) = 0;
                  id(power_cycle_relay).execute();
                }
          else:
            # If there's no packet loss, reset the packet_loss_count
            - lambda: |- 
                id(packet_loss_count) = 0;

  # Script for power cycling the relay
  - id: power_cycle_relay
    then:
      - lambda: |-
          time_t now = id(esphome_time).now().timestamp;
          // Do not power cycle during the first 15 minutes after boot
          if (now < 900) {
            ESP_LOGD("power_cycle_relay", "Skipping power cycle during the first 15 minutes after boot");
            return;
          }
          // Do not power cycle if the last power cycle was less than 15 minutes ago
          if ((now - id(last_power_cycle)) < 900) {
            ESP_LOGD("power_cycle_relay", "Skipping power cycle since it's been less than 15 minutes since the last one");
            return;
          }
          // Update the last power cycle timestamp and execute the power cycle
          id(last_power_cycle) = now;
          ESP_LOGD("power_cycle_relay", "Power cycling the relay due to button press or packet loss.");
          id(relay).turn_off();          
      - delay: 5s
      - lambda: |- 
          id(relay).turn_on();




time:
  - platform: sntp
    id: esphome_time
    servers:
      - 0.pool.ntp.org

They’re comments. Why would you think they need removed?

Uh, potentially yes. What device are you using?

Are you doing all this just for an auto-restart script? If so, theres much easier ways than this. The simplest would be to just use the wifi component and its on_disconnect: function to do a reboot if it becomes disconnected.

I’m trying to do exactly what this script does. Ping/monitor if my internet goes down. Restart until it goes back up. This is for the cable modem.

This would be for the smart plug connected to my router.

Then use the esphome, on_disconnect to control the smart plug or trigger a automation that restarts the device till it is up.