Hi everyone, I’m trying to understand how to handle the availability of hardware-connected entities in ESPHome.
I’m using a MCP23017 port expander connected via I2C, and I have its outputs configured as gpio switches in ESPHome. These switches are then used in the esphome sprinkler controller.
The issue is that even if the MCP23017 is disconnected or malfunctioning, all its entities (the switches) still appear in Home Assistant as fully available: I can turn them on and off without any warning or error.
The same happens with the sprinkler component: all valves linked to those outputs remain perfectly usable in Home Assistant
I couldn’t find anything in the ESPHome documentation about managing entity availability based on hardware presence or communication errors.
Is there any way to natively manage hardware-based availability inside ESPHome? Or maybe a way to make entities unavailable if a dependent hardware device fails or is missing?
I’m looking for a solution, ideally handled directly by ESPHome.
Are you suggesting that the MCP23017 is disconnected from the microcontroller running ESPHome, or that ESPHome is disconnected from whatever computer you are running Home Assistant on ?
I expect it is a Wi-fi issue, which is not uncommon.
My own experience is that I can press the button, etc in Home Assistant all I want, but after a while get a message stating the connection is unavailable.
If you want a connection that is more tolerant of disconnections, I suggest you look into MQTT.
“The issue is that even if the MCP23017 is disconnected or malfunctioning, all its entities (the switches) still appear in Home Assistant as fully available: I can turn them on and off without any warning or error”
It is not a Wi-Fi issue. I simulated a problem with the MCP by disconnecting it from the ESP — I would expect the switches to be unavailable in HA, but they remain available.
The same happens if I turn on the ESP without the MCP. The ESP doesn’t detect the MCP, but the entities are still available.
I tried it, but update_interval is not valid for binary_sensor template, it pools the state every loop, so every 16 ms.
I removed the update_interval just to check the code and I get an error while compiling:
/config/esphome/4ch-pro.yaml:127:47: error: 'virtual bool esphome::mcp23017::MCP23017::read_reg(uint8_t, uint8_t*)' is protected within this context
127 | return id(mcp23017_hub).read_reg(0x12, &tmp);
| ^
In file included from src/esphome.h:56,
from src/main.cpp:3:
src/esphome/components/mcp23017/mcp23017.h:19:8: note: declared protected here
19 | bool read_reg(uint8_t reg, uint8_t *value) override;
| ^~~~~~~~
*** [.pioenvs/4ch-pro/src/main.cpp.o] Error 1
========================== [FAILED] Took 8.86 seconds ==========================
I cannot read that value.
To me, it seems this situation should be handled within esphome: if a connected sensor/device is no longer available, the entities that rely on that sensor/device should be marked as unavailable.
I’m sure it’s doable but might need more tinkering that HA approach.
You could try: id(mcp_switch_1).invalidate_state();
on that interval lambda.
Not sure if it works for switch though and to get the correct state after MCP is in play again needs to be studied (if this real life situation even exist).
It seems we have a winner I will try to disconnect it as soon as I can. I use it in an irrigation system and it is summer time now, so not the best moment now.
I don’t really know The binary_sensor to check if it is connected or not is a great idea and it works for my purpouse (I can make an automation to notify if it will disconnect), so I don’t really mind.