Hi!
I’m trying to implement a display with infos regarding the api state. It should display something like online/offline. For this i’m using a lambda. But I’m unable to get it to work as I can not find the correct command. This is what I have:
HOW is that a solution if you don’t reference api.connected in your configuration?
All you’re doing here is getting the value of your binary sensor “statussensor” which has nothing to do with api.connected or the api state. This is a false solution.
I have duplicated it. Because it seems to report along the lines what you would expect, it fools you into thinking it works.
I figured out a real solution, outside of lambda, outside of display:
- if:
condition:
api.connected:
then:
- lambda: 'id(displaytext2).publish_state("api on");' # to my oled display
else:
- lambda: 'id(displaytext2).publish_state("api off");'
REMEMBER that logger: log output counts as api client as well as your Home Assistant instance, so kill log output when testing for negative, and disconnect your connection externally to your hassio or that will count as a client too. I just unplug ethernet and/or rename wifi temporarily to prevent logger.
Display, for reference:
Okay, the real solution I just picked from esphome source code is: global_api_server->is_connected()
Important notice: it returns false if there are no connected API clients. Possible clients are Home Assistant and esphome logs (can be more). So if you watch esphome’s logs from your local machine and your Home Assistant gets disconnected, you still see that global_api_server->is_connected() == true (and api.connected condition is true).