Esp32 who will automatically restart the router if internet is off

HI every ones,

Newbie on esphome, i am building a device with esp32 who will automatically restart my 4g router if internet is off, true on relay (yellowLED).

So far it works not so bad but i would like to get some recommendations / tips / cleanings about the code i am writing and forget a little bit about my “Arduino ide way to think”

Thanks in advance :slightly_smiling_face:

esphome:
  name: myesp
  friendly_name: myesp

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

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

ota:
  password: ""

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.109
    gateway: 192.168.1.1
    subnet: 255.255.255.0


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

globals:
  - id: connect_check
    type: int
    restore_value: no
    initial_value: '0'  

  - id: connect_status
    type: bool
    restore_value: no
    initial_value: '0'  

output:
  # Success connection LED
  - platform: gpio
    pin:
      number: 32
      mode: output
    id: redLED
    inverted: false

  # Relay LED
  - platform: gpio
    pin:
      number: 33
      mode: output
    id: yellowLED

interval:
  - interval: 30s
    then:
      - sensor.template.publish:
                id: connect_check_sens
                state: !lambda '
                id(connect_check) += 1;
                return id(connect_check) ;'

      - sensor.template.publish:
                id: connect_status_sens
                state: !lambda '
                return id(connect_status) ;'   

      - if:
          condition:
             lambda: 'return (id(connect_check) > 5 && id(connect_status) == 0);'

          then:        
            # relay off
            - lambda: 'id(restart_wifi).execute();'
                      
          else: 
            # relay on
            - output.turn_off: yellowLED
            - delay: 5s
      
      - if:
          condition:
             lambda: 'return id(connect_check) > 10;'

          then:        
            - lambda: |-
               id(connect_check) = 0;

      - lambda: |-
          ESP_LOGI("main", "connect stat: %d",id(connect_status));
          ESP_LOGI("main", "check time : %d",id(connect_check));
      
script:
  - id: restart_wifi
    then:
      - output.turn_on: yellowLED
      - delay: 20s 
      - switch.turn_on: restart_esp  
      
switch:
  - platform: restart
    name: "Living Room Restart"  
    id: restart_esp       

sensor:
  - platform: template
    name: "connect check Sensor"
    id: connect_check_sens 
    update_interval: never

  - platform: template
    name: "connect status Sensor"
    id: connect_status_sens 
    update_interval: never

  - platform: ping
    ip_address: 8.8.8.8
    id: ping_ex
    num_attempts: 4
    timeout: 1sec
    loss:
      name: loss
      on_value_range:
        - below: 90.0
          then:
            - output.turn_on: redLED           
            - sensor.template.publish:
                id: connect_status_sens
                state: !lambda '
                id(connect_status)=1;
                return id(connect_status) ;'         
            - sensor.template.publish:
                id: connect_check_sens
                state: !lambda '
                id(connect_check)=0;
                return id(connect_check) ;'
            
        - above: 90.0
          then:
            - output.turn_off: redLED            
            - sensor.template.publish:
                id: connect_status_sens
                state: !lambda '
                id(connect_status)=0;
                return id(connect_status) ;'           
            - sensor.template.publish:
                id: connect_check_sens
                state: !lambda '
                id(connect_check)=0;
                return id(connect_check) ;'
    update_interval: 60s 
1 Like