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.
Boot
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
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.
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.
@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:
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.
@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)
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)
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.
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 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:
Press the button that is connected to ground and RST
Wait for the device to boot, connect to WIFI and connect to HA api (the “id(ota_update).has_state()” )
Check if I want to update the code (I use a input boolean in HA for this)
If a update, turn of the update boolean and wait for 300seconds
If no update toggle the input boolean in HA
Wait for 30 seconds so the battery voltage will be updated
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.
@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.
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.