Esphome, magnetic switch, deep sleep and friends

Hi all,
I want to build a door sensor:

  • wake from GPIO status changing (either 0 → 1 or 1 → 0)
  • publish status of GPIO on via MQTT
  • deep sleep

Hardware:

  • ESP32 Lolin32
  • magnetic switch (NC mode), pin 14 and GND
  • LiPo battery

esphome code:

esphome:
  name: lol32-bascula-sx
  friendly_name: lol32-bascula-sx

esp32:
  board: lolin32_lite
  framework:
    type: arduino

logger:
  level: DEBUG

api:
  encryption:
    key: "xxx"

ota:
- platform: esphome
  password: !secret esphome_secret

web_server:
  port: 80

wifi:
  ssid: "xxx"
  password: "yyy"
  manual_ip:
    static_ip: 192.168.201.x
    gateway: 192.168.201.x
    subnet: 255.255.255.x

mdns:
  disabled: true

captive_portal:

binary_sensor:
  - platform: gpio
    name: BasculaSX
    device_class: garage_door
    pin:
      number: GPIO14
      allow_other_uses: true
      inverted: True
      mode: INPUT_PULLUP
    on_state:
      then:
        - lambda: |-
            ESP_LOGD("sleepy", " Bascula SX Published!:");
            id(bassx_count) += 1;
            if(id(bassx_count) > 1 ){
              id(deep_sleep_1).begin_sleep();
            }

globals:
  - id: ota_mode
    type: bool
    initial_value: 'false'
  - id: bassx_count
    type: int
    restore_value: no
    initial_value: '0'

mqtt:
  id: mqtt_client
  broker: 192.168.202.x
  port: 1883
  username: xxx
  password: xxx
  birth_message:
  will_message:
  on_message:
    - topic: lol32-bascula-sx/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
        - logger.log: "OTA mode enabled - deep sleep mode disabled"
    - topic: lol32-bascula-sx/ota_mode
      payload: 'OFF'
      then:
        - deep_sleep.enter: deep_sleep_1
        - logger.log: "deep sleep mode enabled"

deep_sleep:
  id: deep_sleep_1
  wakeup_pin:
    number: GPIO14
    allow_other_uses: true
  run_duration: 10sec

When switch is open it seems working,
but when I close it, esphome seems loop it - continuously wake up - without staying in deep sleep.
What am I missing here?
Thanks for any help.
-f

Quite confusing…
-Not described on your wish list, but your code is sending esp to sleep any time gpio14 changes state.
-Is your sensor open (no continuity) when door is open or opposite?
-your deep_sleep component only wakes up on high signal. And It’s pulled up by binary sensor.

Indeed I need some help from you guys :wink:

The idea is: deep sleep all the time,
wake up on switch open, send state to mqtt then sleep,
wake up on switch close, send state to mqtt then sleep.
If on_state automation is not right, which you should I use?

I’m using this magnetic switch:

in normally closed config.
Specifiyng inverted: True in esphome config, when I open the contact the state is set to Open, when I close the contact is set to Closed in HA.

Without deep sleep, it seems to be working fine.
How to change the config in order to trigger both conditions?
Thanks for your help!
-f

It’s not a question if on_state is wright. It’s a question what the automation does. Yours is sending Esp to sleep. Not waking it up.
So you might want to remove it.

You didn’t really answer to my question about door opening vs switch.
It’s difficult to describe logic of the setup because there are 3 different open definitions.
-Switch electric circuit can open (NO or NC)
-Switch magnet can open (which can open or close the electric circuit)
-Door can open (which can open or close the magnetic field)

Adding here that the circuit can be wired either to 3.3V or to gnd, and that it can be logically inverted on esphome…

Anyway, add this line to your deep_sleep component:
wakeup_pin_mode: INVERT_WAKEUP

I have never used it, but according to documentation it should be able to invert the wake-up logic according to actual pin state.

Removed!

It works! Thank you so much!
-f

Nice! I wasn’t sure about INVERT_WAKEUP.
You are welcome.

1 Like