Restart if wifi connection is lost

Hi,
one of my esphome nodes has some troubles with wifi (weak signal).
So I would like to reboot it if wifi signal is lost.
I tried this, but of course it reboots on start as wifi is still low (I think):

sensor:
  - platform: wifi_signal
    name: "klo_vorne_signal"
    update_interval: 120s
    state_topic: klo_vorne/signal
    on_value_range:
      - below: 0.1
        then:
          - button.press: restartit
      - above: 0.1
        then:
          - logger.log: WIFI OK

restartit is a button used for restarting the device
however. does someone know a solution on how to monitor wifi strength and reboot if the signal is too weak (or connection is lost)?
Best regards and thanks in advance,
Otto

Esphome when experiencing a loss of wifi or rather can’t connect to the api because of loss of wifi, will restart on it’s own. If it’s not reconnecting, it’s due to the weak signal and no setting will over come that.

Esphome restarts by default already if no wifi connection is established. It’s 15 minutes by default:

  • reboot_timeout (Optional, Time): The amount of time to wait before rebooting when no WiFi connection exists. Can be disabled by setting this to 0s, but note that the low level IP stack currently seems to have issues with WiFi where a full reboot is required to get the interface back working. Defaults to 15min.

It also restarts if there’s no API connection to HA - also after 15 mintues. You can change that interval, though.

You should improve signal. Why is signal a problem?

thanks for your answers!
will try to reduce timeout.
signal strength is a problem cause this esp is sitting under wall (replacement of former mysensors device)

If you can’t move either closer there are esp’s with external antennae that will boost it’s signal.

Have you tried to optimize the wifi connection on the ESP at a software level?

wifi:
  ssid: "yourSSID"
  password: "yourPassword"
  manual_ip:
    static_ip: 192.168.1.80
    gateway: 192.168.1.1
    subnet: 255.255.255.0
  fast_connect: true
  output_power: 20.4 dB
  power_save_mode: none

I’ve been using the above settings somewhat reliably for an ESP which is 2 concrete floors & a bunch of thick brick walls up from the nearest AP.

1 Like

Well, the ultimate answer for long-term stability is: build a stable signal mesh throuout your house/parcel. If you don’t do that same problem will occur sooner or later as you’ll add more devices. Whatever you do now you’re just patching things up. Just get additional wifi repeater (or two), they are not that expensive. I have 4 of them: main router and 3 AP’s - for reliable wifi signal no matter where i put my device.

what I do is a restart everyday

time:
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 4
        days_of_week: MON-SUN
        then:
           - switch.toggle: restartit
      - seconds: 0
        minutes: 0
        hours: 16
        days_of_week: MON-SUN
        then:
           - switch.toggle: restartit
switch:
  - platform: restart
    name: "Restart"
    id: restartit

Thanks for your answers!
I reduced output power of wifi cause the higher I am putting it, the more false positives I am getting from the pir. altough I isolated it using aluminium foil.
increased power, we will see…

You can have up to 3 APs/repeaters, if you set them up with fixed channels.
More than 3 and you need a WiFi controller and APs that are supported or you will probably just make your WiFi setup worse.

With fixed channels you can set them at 1, 6 and 11 and they do not overlap then.
With more than 3 APs/repeaters some of them have to use the same channel and if they can see each other then their sending power needs to be lowered.

It is a typical misunderstanding that more APs/repeaters are better and when some even then try to increase the sending power to get even more coverage.

1 Like

I have Asus AiMesh system and all AP’s are managed by master router, so i should be ok. Anyway, such mesh sets are readily available from various companies.

Then it is fine, but it is just not a general recommendation you can make.
Your setup is not a general one.

Partly you’re right, but, i recommended only one (or two) which won’t be a problem no matter what you choose. I personally installed tons of AP’s (cheap ones, too) at my friends etc… over the years and I’ve had no problems so far with either or them.
Solving above problem with software, reboots etc… doesn’t really permanently solves the problem, but you only patch things up and postpone them to a later time. We all know that wifi signal varies with time - if it’s at the limit it’s only the matter of time when it will be below threshold and device will stay offline for hours until signal is good again.

A good wifi signal is a must for reliable HA system.

(regarding my “not general” system: most of asus routers offer aimesh by default, i bet others do, too, so it’s really nothing “special”.)

I had a similar problem with weak wifi and switched to an esp32 w/external antenna. It fixed the problem and id recommend keeping some around or in your rapidly growing component drawers/bins.

Each case will vary but. I gained roughly 25-30% wifi signal from an external antenna.

well. I am not really sure, if the signal is the problem or if the esp mini is near his eol.
it is only some months old and worked like a charm, but it is inside a switch box, so not really much space - and it is hard to exchange because the PIR is so close and if the aluminium foil is not placed exactly as it must be I am getting tons of false positives.
luckily if I power-cycle the esp, it seems to work again. so I think I will create a presence detection for this esp and power-cycle it if it is not reachable anymore. the good thing is, that I can switch the power within HA :slight_smile:

I have esp boards in service 24/7 for over 3 years. Only having a few months on yours is nowhere close to its lifespan unless you’ve been flashing it every other day.

Your not sure if its even the wifi signal? It’s really easy to be sure by just checking its signal strength and then you know.

Foil? Aluminum foil? What are you using? the RCWL-0516? If aluminum foil is necessary ro make a motion sensor work then Id argue its time to upgrade to better quality stuff and i wouldnt recommend building out a smart home thats held together with duck tape and bubblegum or youll be fixing it constantly.

I also have many ESPs in service. Most of them have been working for years. But some are going bad pretty well.
the pir is a SR501 - and it is known that it interferes with the wifi signal of ESPs.
you can place them away from the ESP or you can shield them. In this case I can not seperate it from the ESP, so I am using aluminium foil.
as far as I know, there are no PIR sensors which do not interfer with the wifi signal.
and the foil is not necessary to make the PIR work. it is necessary to avoid false positives.
thanks a lot, I am aware of this. I have been building my home automation since about ten years now. but until now I have been using fhem. now I am trying HA…

10 years, wow! You must be familiar with a capacitor then? Adding a ceramic capacitor and using a clean/stable PSU is widely known and accepted as how you fix tix those cheap PIR sensors. If buying something quality that doesnt need modified inorder to work reliably isnt an option then id suggest that over wrapping it in bubblegum and foil.

I think the few i still have in service are the am312 PIR and i dont have false positive issues and some are in a 2"×2" project box with d1 mini’s.

On a side note. If something is unreliable, needs modified when brwnd new. spitting out nonstop false positives which making it unusable, that is most peoples definition of, “doesnt work”

If something works then it doesnt need tin foil wrapped around it inorder to reliably use the thing.

well, as I mentioned, the setup is old and capacitors do not fix the issues.
you can do whatever you want in your home, so do I.