Detect loss of connect to device? Specifically ESP32?

I have searched the forum but I am unable to find what I am looking for.

What I want to do is perform X actions if I lose connect to an ESP32. I am using ESPHome. I would like to turn off X devices if I lose connect to an ESP32 providing sensor data. I dont see an obvious way to do this using an automation. Is there another way or a way to do it via an automation?

You could have a state trigger that fires when the state changes to unknown or unavailable

1 Like

Include this sensor in your ESPHome config.

Trigger if it goes from ‘on’.

1 Like

That sounds like a simple solution! I will look into that. Thanks.

The status binary sensor I linked to is specifically for this scenario. But do it the hard way if you wish.

I have an automation for noticing when my D1 Mini hall thermostat goes down, which it does roughly twice a week due to memory issues with the OLED screen connected to it — and the ESPHome API reboot_timeout doesn’t seem to work.

The wifi reboot_timeout does, though, so my automation warns me that it’s down, turns off the heating (otherwise, if on, it would just keep running!), and blocks it from the wifi for 5 minutes (Unifi integration) which is enough to kick it back into life.

Here’s the automation — the 30 second delay gets around the HA startup problem, and when I deliberately restart the node with new code:

- alias: Hall sensor unavailable
  trigger:
    - platform: state
      entity_id: sensor.hall_temperature
      to: 'unavailable'
      for:
        seconds: 30
  condition: []
  action:
    - service: notify.admin
      data_template:
        message: 'Hall temperature sensor offline! Trying to reconnect...'
    - service: switch.turn_off
      entity_id: switch.hall_temperature_sensor
    - service: switch.turn_off
      entity_id: switch.chc_central_heating_relay
    - delay: 300
    - service: switch.turn_on
      entity_id: switch.hall_temperature_sensor
    - service: notify.admin
      data_template:
        message: 'Done. If still offline, please solve manually.'
2 Likes

The solution from @tom_l is better since most stuff will be unavailable or unknown on Home Assistant startup, so you do need to be aware of that :slight_smile:

2 Likes

I dont have mqtt set up. It would seem if it is possible to do with a simple automation that would be easier? This is not mission critical stuff so a delay of 5-10 mins is not unreasonable to handle reboots etc.

That said - I set up an automation similar to what was shown above and it is not detecting the ESP32/sensor as unavailable… hehe

alias: Connect.loss ESP05 turn off SO01
description: Connect.loss ESP05 turn off SO01
trigger:
  - platform: state
    entity_id: sensor.05_temp
    to: unavailable
    for: 10s
condition: []
action:
  - type: turn_off
    device_id: 6d41b25dbe1d35a7a5997d31f3d29750
    entity_id: switch.so01_relay
    domain: switch
mode: single

'unavailable' in quotes, I think; and you’re making up your own syntax for the for (docs) — it’s not the same as ESPHome.

I just copied what the GUI made for yaml. I tried it with quotes around unavailable in gui and it added 3 quotes to it in yaml. Weird. I will try changing the format of the for:

As tom has said there’s a built in component for this.
Why overcomplicate things?

Care to expand? I dont see a ‘status’ trigger type in automations GUI for this. Nor do I see a way to set up a binary sensor in automations or devices. I am still pretty new to HA. I dont know what I dont know.

Have you set up that status sensor in esphome for that device?
Once you have you just use that as the trigger for your automation

You don’t need mqtt it works with the api.

I just worked out why you think that. Ignore the text in that link preview it is misleading. The actual text on the site reads:

The Status Binary Sensor exposes the node state (if it’s connected to via MQTT/native API) for Home Assistant.

I guess I am unclear as to where to create/access the binary sensor at? I remember seeing it somewhere before but now cant find it.

I have some binary sensors in the lovelace gui editor. But not one for this ESP32 device. They are for some sonoff31s I have.

In your ESP node config, not in Home Assistant.

In the yaml file for that node,
Add these lines:

binary_sensor:
  - platform: status
    name: mynodename

Upload it, allow it to connect and it will then be available in home assistant

1 Like

Ah ok so I add the binary node config to the yaml file for each device? That makes more sense. It is not clear as to context in the docs. I will try that in a bit. No more time to play right now.

Thanks for the hand holding peoples!

Well, it was a link to the ESPHome docs…

1 Like

Well for dummies like me it needs to say - add this to your yaml for the device. I am just a lowly luser trying to figure this out. Watching all the brain_rockets on here fly by. Or even better, a checkbox in the gui that says ‘add this binary sensor’. That would be slick!