Turn off WIFI

hi, is there a way to switch the wifi on and off on esp8266. I want to initially have the unit start and connect to network (so I can do updates OTA if required), then have a physical switch to turn the wifi off as its going to run on batteries, and connect via sim800l.

I have a binary sensor set up as a switch and tried :-

    - lambda: WiFi.mode(WIFI_OFF);
    - lambda: WiFi.mode(WIFI_STA);

the unit disconnects, but after a few seconds (with the switch still connected) it automaticcly reconnects to the wifi ???

If you remove the on_release, does it stay off the network?
Also, I don’t know if on_press has debounce. If it doesn’t, perhaps it’s seeing some on_release action during the press which you could reduce with a simple series resistor and cap to ground.

I removed the ‘on_release’, but it still reconnects after a few seconds, here’s all my code …

  name: sim800l
  platform: ESP8266
  board: d1_mini



  - ssid: "xxxxx"
    password: "xxxxxx"
  - ssid: "xxxxxx"
    password: "xxxxxxx"
#  reboot_timeout: 0s 
#  power_save_mode: high

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Sim800L Fallback Hotspot"
    password: "xxxxxx"


# Enable logging
  level: DEBUG

# Enable Home Assistant API

  - platform: gpio
      number: D4
      inverted: false  
    name: "PIR Sensor"

    device_class: motion
    id: my_motion
        - sim800l.send_sms:
            recipient: '+77xxxxxxxx'
            message: PIR activated -- send help!
  - platform: template
    name: temp_motion
    lambda: 'return id(my_motion).state;'
      - delayed_off: 1min

  - platform: gpio
    pin: D8
    name: "wifi switch"
#    device_class: 
    id: my_wifi_switch
        - lambda: WiFi.mode(WIFI_OFF);
#    on_release:
#      then:
#       - lambda: WiFi.mode(WIFI_STA);

  - service: send_sms
      recipient: string
      message: string
    - sim800l.send_sms:
        recipient: !lambda 'return recipient;'
        message: !lambda 'return message;'

- platform: template
  id: sms_sender
  name: "Sms Sender"
- platform: template
  id: sms_message
  name: "Sms Message"

  baud_rate: 9600
  tx_pin: D0
  rx_pin: D1

    - lambda: |-


api: needs a network connection, maybe it forces wifi up?

        - lambda: WiFi.mode(WIFI_OFF);

where are you getting this lambda from? Where is it documented?

I saw on the net that the ESP8266 has four modes,

  • Station mode (WIFI_STA), where the NodeMCU device joins an existing network
  • Access point (WIFI_AP) mode, where it creates its own network that others can join
  • Station + AP mode (WIFI_AP_STA), where it both creates its own network while at the same time being joined to another existing network
  • WiFi off (WIFI_OFF)

It does state that it uses ESP8266WiFi.h Library, not sure if esphome uses this library, is there a way to check and if not force the use of this library ?

It is open source, look at the code.

And as I said, what makes you think api can work without a network?

What if you want to wakeup from sleep, check the sensor data without being connected (to save battery power), and either connect afterwards if a threshold has been surpassed? That would actually make the attached sensor a ‘smart’ battery powered sensor.

@nickrout I want to use ESP Home nodes to control the shades everywhere in our house. We don´t like the idea of having ~ 20 nodes active on WiFi around-the-clock.
That´s why I use the CAN Bus option to communicate with the nodes controlling the shades. I only need WiFi for OTA and tried to switch WiFi on/off via CAN. But even if I configure reboot_timeout : 0s it seems that the node is rebooting after WiFi is turned off via CAN and enables WiFi again. Is this a bug?

You are aware of the other components which could cause a reboots like for example ‘api’ or ‘mqtt’ ?

@picohassiotts thanks for your hints.

Is there anything I could do to promote the PR you were referring?

Test it and report your findings in the PR :upside_down_face:

@picohassiotts I am not a µC developer so I have no idea how I can compile a branch with a config and load it to a device. I also couldn´t find a docu describing this. If cou could give me some directions, I´d be happy to do that.

Made this guide for another project (on w10 though)
Maybe of help?
Below my command lines used to compile:

cd c:\workshop\esphome

pip install esphome
pip install esphomeflasher
pip install --upgrade git+ https://github.com/esphome/esphome

esphome mideahvac.yaml wizard
esphome mideahvac.yaml compile


using the compiled C:\_workshop\esphome\mideahvac\.pioenvs\mideahvac\firmware.bin

Assuming you already installed python :wink:

PS:You can replace
with any ESPHome branch of your liking :wink:
as well as using your own yaml configuration :nerd_face:


thanks to the hints from @aceindy I now tested the PR and reported my findings: Allow WIFI to be disabled and enabled by ashmckenzie · Pull Request #1249 · esphome/esphome · GitHub

1 Like

Out of topic but really need to know: what kind of sim card are you using with sim800l module ?

micro sim card

I know it is micro sim card, but what carrier/plan ? (if in US)

That higly depends in your usage. I went for some 1nce card to start/play with. Mostly because of their 10 years runtime and wide coverage: https://1nce.com