Esphome switch turn off if no ping HA or if HA is out

Hello, i want to know if its possible to check if HomeAssistant is UP and can communicate to ESP32 ? (with ping or something else)

If i turn_on switch on HA dashboard, i want every 15s ESP32 check if HA is up, if not i want ESP turn off all switches with switch.turn_off: relais_1 for exemple.

If i turn ON wathering, the automatisation to turn_off is on HA, but if ES32 can’t communicate with HA i want this switch turn off alone.

switch:
  - platform: gpio
    pin: GPIO32
    name: "Wathering"
    id: "relay_1"
    icon: mdi:sprout
    on_turn_on:
      then:
        - switch.turn_off: relay_2
        - switch.turn_off: relay_3
        - switch.turn_off: relay_4

Thanks

This is a good example of a scenario where it might be better to do the automation on the ESP instead of HA, but I get if there’s reasons that you’d prefer to use HA.

If you’re using the API, you can check if it’s connected to HA:

You could do this in a 15s interval:
https://esphome.io/components/interval.html

Can create binary_sensor as follows:

platform: template
name: "API connected"
id: sensor_api_connected
internal: True
entity_category: "diagnostic"
device_class: 'connectivity'
lambda: return global_api_server->is_connected();

and use it whatever You need in automations.
Sensor will be true when connection HA->device is established.

Sovled :slight_smile:

api:
  on_client_disconnected:
    - logger.log: "API client disconnected!"
    - switch.turn_off: relais_1
    - switch.turn_off: relais_2
    - switch.turn_off: relais_3
    - switch.turn_off: relais_4


binary_sensor:
  - platform: status
    id: "esp_carte_relais_status"
    name: "ESP Carte Relais Status"

switch:
  - platform: gpio
    pin: GPIO32
    name: "Arrosage - Arrière - GAG"
    id: "relais_1"
    icon: mdi:sprout
    on_turn_on:
      then:
        - switch.turn_off: relais_2
        - switch.turn_off: relais_3
        - switch.turn_off: relais_4
        - delay: 5s

  - platform: gpio
    pin: GPIO33
    name: "Arrosage - Arrière 1"
    icon: mdi:sprinkler-variant
    id: "relais_2"
    on_turn_on:
      then:
        - switch.turn_off: relais_1
        - switch.turn_off: relais_3
        - switch.turn_off: relais_4
        - delay: 5s

  - platform: gpio
    pin: GPIO25
    name: "Arrosage - Arrière 2"
    id: "relais_3"
    icon: mdi:sprinkler-variant
    on_turn_on:
      then:
        - switch.turn_off: relais_1
        - switch.turn_off: relais_2
        - switch.turn_off: relais_4
        - delay: 5s

  - platform: gpio
    pin: GPIO26
    name: "Arrosage - Avant 1"
    id: "relais_4"
    icon: mdi:sprinkler-variant
    on_turn_on:
      then:
        - switch.turn_off: relais_1
        - switch.turn_off: relais_2
        - switch.turn_off: relais_3
        - delay: 5s

Thanks for helping

1 Like