Wifi button with deep sleep

Hello,

I have a esp01 that is connected to a battery, it has a button connected to ground and reset.
The device is in deep sleep forever. When I push the button the device should wake up, check the state of the input boolean from Home Assistant, if it is on it should turn it off, if it is off it should turn it on. Then it should go to deep sleep for ever again.

The deep sleep is working.
I can also get information from Home Assistant about the input_boolean.
But these things I can’t get to work.

-Differentiate on what the state of the input_boolean is.
-Send a “message” to home assistant to turn on or off the input_boolean.

I don’t want to use mqtt for this, as the documentation itself states, use the HA API.

What I want to do explained with pseudo code.

  1. Boot
  2. Check state of input_boolean from HA
    3.1. If boolean is on, turn it off
    3.2 If boolean is off, turn it on
  3. Deep sleep forever

I have tried with automations and lambdas but I could not find documentation that I understood well enough to do some testing.

2 Likes

Why not skip obtaining the state all together. Send a command to toggle the input_boolean, input_boolean.toggle will turn it off if on, or on if off.

Yes that is a good idea. But I wanted to learn how to set up conditionals. And this would be a very easy way to start.

Be sure to post your results here :fist_left:

Absolutely! I asked a question first on discord but I don’t like that platform for these kind of things.

I don’t think that espohome can check home assistant entity states yet, nor is there a homeassistant service for that, but your idea is rather interesting.

Feature request time?

See homeassistant binary sensor

@michel72 Yes esphome can check entity state. As I wrote, I have a working code for that.

binary_sensor:
  #Specify to get input from HA
  - platform: homeassistant
    #Name not really needed when using internal:
    name: "Input Boolean From Home Assistant"
    #What entity to get input from in HA, this entity_id correspond to what you see in HA
    entity_id: input_boolean.keep_ventilation_on
    #Define that this sensor is not being shown in HA (Is a bit stupid to get input from HA and then send the same back)
    internal:

@OttoWinter What are you replying to?

Ok, I am starting to get somewhere.

I have managed to call HA service to toggle the input_boolean.
I have also added that service call to on_boot.
But one thing I came past was that even with priority: -10 it still does not wait long enough for the service call to go through.
If I add a delay for 15 seconds it works.
Or if I add a switch.toggle and a delay of 5 seconds it works.

Examples:

This works:

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: -10
    then:
     - delay: 15s
     - homeassistant.service:
        service: input_boolean.toggle
        data:
          entity_id: input_boolean.keep_ventilation_on

This works:

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: -10
    then:
     - switch.toggle: led1
     - delay: 5s
     - homeassistant.service:
        service: input_boolean.toggle
        data:
          entity_id: input_boolean.keep_ventilation_on

But using only 10 seconds delay does not work.

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: -10
    then:
     - delay: 10s
     - homeassistant.service:
        service: input_boolean.toggle
        data:
          entity_id: input_boolean.keep_ventilation_on

Should I be putting this code another place that on_boot when doing a service call to HA?
Cause I don’t want to add timers that may or may not work.

@michel72 But remember to integrate and configure the esphome device in HA first. Or else it will not work. (just pulled a few hairs from troubleshooting that)

What “it”?

The it I was explaining in my other reply to michel72.
How to get a entity state from HA.

Blah, I have tried so many things with on_boot or on_loop. But esphome will not wait for the api to connect unless I use a high delay first.
Using lambda with a check and loop will cause the api to never come on.

@OttoWinter Is this a feature or a bug? (Let me know if you need more details, for example code)

Yes that is intended. For what you want to accomplish see wait_until and api.connected

but for deep sleep i would recommend using mqtt for now. api is not really that well suited for this rn

Ok, yes I can understand that.
I am not that familiar to find a way to implement wait_until and api.connected.
Do you have some links to where I can read more?
I don’t know if this is something I should do in lambda or some other calls.
And if lambda, how I can call that function. (If api.connected is the whole name or not)

I will do some testing and see if I can get it robust enough to work with the api.

A small disclaimer:

Ok, I have after many hours learning and trying different things found a way to do this in esphome.

I hope there is a better way, cause this is not working very good.
But the way I understand esphome, this is not a proper use-case for esphome. (It excels in many other areas but maybe not this one)

I also have a problem getting deep sleep to work. I have created a issue in github for this: Wifi still on when using deep sleep · Issue #149 · esphome/issues · GitHub

I am also just using Home Assistant, and as @OttoWinter has said, this is not a good way to do it. It would be better to use MQTT.
But I wanted to try and see how I could get it to work.

Here is the code that I am using. I have commented as good as I can.
# is a comment in yaml, and // is a comment in lambda (c++)

First some pseudo code:

  1. Press the button that is connected to ground and RST
  2. Wait for the device to boot, connect to WIFI and connect to HA api (the “id(ota_update).has_state()” )
  3. Check if I want to update the code (I use a input boolean in HA for this)
  4. If a update, turn of the update boolean and wait for 300seconds
  5. If no update toggle the input boolean in HA
  6. Wait for 30 seconds so the battery voltage will be updated
  7. Go to deep sleep for a year

(I could not find a good way to paste a lot of code here so I add a pastebin link)
https://pastebin.com/raw/6gxY1UZC

Please let me know if there is anything I can do different. I feel a little like this with this code: https://www.reddit.com/r/funny/comments/b3jjhv/there_must_be_an_easy_way/

From my testing with this project I have found out that esphome is not that good at just doing one thing, like this wifi button is supposed to do.
There are many things going on in the background depending on many things. And it is difficult to control the execution of code to make sure things happen when you want them to.
As mentioned I have tested many different approaches so please start a discussion, I would love to discuss why and why not.

The execution time from when I push the button to the service in HA is being called is from 10seconds to 2minutes. Even if I have wifi connection after 3seconds.
And since the deep sleep is not working I need to find another solution outside of esphome.
I will post my solution when I am done with that.

Hope to get some feedback on this!

@silvrr I went with just a toggle after all.
When I did the has_state() check I could get confirmation without checking the state before and after.
And I have learned how to use conditionals and when they won’t work etc.

Not really. But someone in discord posted this one here:

esphome:
  platform: ESP8266
  board: d1_mini
  name: livingroom
  on_boot:
    then:
      - script.execute: boot_script

script:
  - id: boot_script
    then:
      - logger.log:
          format: "boot_script started"
          level: INFO
      - logger.log:
          format: "waiting for api connection"
          level: INFO
      - wait_until:
          condition:
            api.connected:
      - logger.log:
          format: "api is connected"
          level: INFO
      - homeassistant.service:
          service: light.turn_on
          data:
            entity_id: light.livingroom

Which is for a wifi button exact like yours (but using a wemos d1 mini)

1 Like

Just wanted to say thanks for this. It wasn’t until your funny comment about internal: true that i realized my buffer errors were a result of 48 internal HA sensors being set to internal: false by default.

1 Like