Force Reconnect WiFi when signal is bad (ESPHome)

I just noticed that 2023.06 added some commands to enable/disable wifi.
I have one Sonoff (TH16) which is constantly having issues staying connected.

I added an uptime sensor and wifi signal sensor to try to figure out what was going on.
The device was staying on, but the signal strength being reported was not great:

The range here is -70 (not great) to 30 (what?).

Anyway, I set up this automation in the ESPHome config, and I just want to be sure it makes sense?

...
sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi
    id: wifi_signal_sensor
    update_interval: 60s

interval:
  - interval: 2min
    then:
      - if:
          condition:
            or:
              - not: wifi.connected
              - lambda: 'return id(wifi_signal_sensor).state < -70;'
          then:
            - logger.log: WiFi signal is too weak or not connected. Reconnecting...
            - wifi.disable
            - delay: 5s
            - wifi.enable

I wanted to add a follow up question here that is related, but separate from my original post.

In the automation above, I have that log statement…

But, it looks like when I click on Logs in ESPHome, it’s always a fresh log (the first entry in the log has a timestamp of when I clicked on Logs).

For example:

INFO ESPHome 2023.6.4
INFO Reading configuration /config/esphome/sonoff-th16-1.yaml...
INFO Detected timezone 'America/New_York'
INFO Starting log output from sonoff-th16-1.local using esphome API
INFO Successfully connected to sonoff-th16-1.local
[12:05:25][I][app:102]: ESPHome version 2023.6.4 compiled on Jul 12 2023, 11:17:06
...

There must be a way to store some persistent logs and access them?

For example, let’s say I wanted to say, lower power usage by disabling wifi except for, say, 5 minutes a day to send back a data point. How would I view the logs that were created during the period where wifi was off? (Ignore that I can’t access the device while wifi is disabled - for example, assume I only need to check in during that 5 minute window - I don’t need an automated solution to this specific scenario).

This entire configuration or automation in other word makes no sense, but only one great thing happening this config just wasting energy (use by sonoff). WiFi disconnection not happening due esphome software, it’s happening due to environment noise in WiFi frequency. Leave espressif module alone they switch to b/g/n mode automatically.

Best way to deal with current senario:

  1. Access esphome via ip instead of mdns - use_address (Optional, string). Note re-add esphome in homeassistant with ip.
  2. Single Access point - use fast_connect (Optional, boolean)
  3. Mesh network - provide bssid (Optional, string). always connect to nearest AP

Thanks for responding!

I found that the bssid of an ap is likely to change whenever I make a change to the network(s) being broadcast by the ap (Unifi specifically, but I’ve read this is standard for others as well). This would mean locking to a bssid could cause problems if I make any network changes (radios, networks, etc). I’m going to try to avoid this option for now I think.

  1. Access esphome via ip instead of mdns - use_address (Optional, string). Note re-add esphome in homeassistant with ip.

I typically use static IPs.

For this specific case I’m going to see if I can move the ESP to get a better signal. I have another one in the same room and it doesn’t have the same problem - so I’m guessing there must be interference in the spot it’s mounted right now.

I’m curious, do you have any idea on the logging question I had asked?

1 Like