Deactivate wifi for a certain time

Hi,

I want to make a sleep training-clock for my daughter and want to check 1 or 2 times a day for the hour when the alarm should go off. Because it’s in the bedroom, I want to disable the wifi for the most of the time. The internal clock should stay working so that’s why no deep sleep is wanted.

I tried with following code every

interval:
  - interval: 15s
    then:
      - lambda: |-
          ESP_LOGD("main", "Wifi turned off");
          WiFi.disconnect(); 
          delay(1000);
          WiFi.mode(WIFI_OFF);
          WiFi.forceSleepBegin();
          delay(100);

This works but every time this happens:

[16:54:59][D][main:205]: Wifi turned off
[16:54:59][W][wifi_esp8266:394]: Event: Disconnected ssid=‘’ bssid=B4:FB:E4:91:3D:15 reason=‘Association Leave’
[16:55:00][W][wifi:100]: WiFi Connection lost… Reconnecting…
[16:55:00][W][wifi:495]: Restarting WiFi adapter…
[16:55:00][I][wifi:194]: WiFi Connecting to ‘’…
[16:55:00][W][wifi_esp8266:191]: wifi_apply_hostname_(clock_test): lwIP error -16 on interface st (index 0)

And the wifi connects again.

Has somebody an idea how to tackle this problem? Of where I can look for the solution?

Esphome is dependent on wifi, unless in deep sleep.

Thank you for the feedback. It did me think and … .

I’ve found following. When I go for a short time in deep sleep with WAKE_RF_DISABLED then the wifi is disabled until I do a new deep sleep with WAKE_RF_DEFAULT.

binary_sensor:
   - platform: gpio
     pin: 
       number: D2
       inverted: yes
       mode: INPUT_PULLUP
     name: "Turn off Wifi"
     on_click:
       then:
         lambda:  |-
           ESP_LOGD("main", "Wifi turned off");
           ESP.deepSleep(10, WAKE_RF_DISABLED);
           yield();
           WiFi.mode(WIFI_OFF);
           WiFi.forceSleepBegin();
           yield();
         
   - platform: gpio
     pin: 
       number: D3
       inverted: yes
       mode: INPUT_PULLUP
     name: "Turn on Wifi"
     on_click:
       then:
         lambda:  |-
           ESP_LOGD("main", "Wifi turned on");
           ESP.deepSleep(10, WAKE_RF_DEFAULT     );
           yield();
           WiFi.mode(WIFI_STA);
           yield();

Also important is to disable power_save_mode

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pass
  **#power_save_mode: HIGH**
  fast_connect: true
  reboot_timeout: 0s
  output_power: 10

Esphome will keep trying to connect to the wifi but the wifi-chip is disabled.

[10:27:27][I][wifi:194]: WiFi Connecting to ‘’…
[10:27:29][W][wifi_esp8266:391]: Event: Disconnected ssid=‘WLan’ reason=‘Probe Request Unsuccessful’
[10:27:29][W][wifi:458]: Error while connecting to network.
[10:27:29][W][wifi:495]: Restarting WiFi adapter…
[10:27:30][I][wifi:194]: WiFi Connecting to ‘’…

I’ll do some further testing and see if this works for my project.

For heaven’s sake, post your code properly. Three backticks before and after the code.

Better this way? My first time … .

That looks better :slight_smile:

I don’t know why you want to work around deep sleep though.

Are you scared your daughter’s brain is going to be fried or something?

Thanks Kristof, for sharing this code!
I have a similar need - I want to save battery power.
I have a PIR sensor that wakes up the ESP from deep sleep but I don’t want to activate WIFI on every wakeup, but only once in a while.
I’ll give it a try :slight_smile:

Necro post! But co-incidentally I saw this today

and if anyone is wondering what that means. It’s a little bit hidden.

There is a new feature in the next release to enable or disable WiFi.

yeah maybe I over assumed on reading ability :wink: