Hello all,
I am generally new to HomeAssistant, so excuse me for any basic info missing from my side.
I have been running HomeAssistantin docker for some months mainly with MQTT entities. For a project that requires a battery-powered device I selected low-power ESPNow protocol for running the device (I know ZigBee would be already a better and easier solution to implement , but it was not possible at the moment).
Since ESPNow is not supported from HomeAssistant, I created a Bridge that receives the messages from the ESPNow device and transfers it to HomeAssistant through serial port. That required to create the sensor with proper configuration + changing the “configuration.yaml” for HomeAssistant.
That worked well so far but I wanted to go to the next level by adding some kind of “auto-discovery” funcionality. Each ESPNow sensor, on the first boot sends a detection message with all required info (platform, entity_id, device class, unit etc) and then keeps on just updating the state. I tried 3 ways to do that but none of them worked:
- Running a template for the serial input. But I realized that templates do not accept the dynamic values on entiy_id + other.
- I tried python_scripts integration in HomeAssistant activated each time the serial port was updated that parsed the message and created an entity using has. That way entity is temporary without unique_id and will be lost after reboot.
- I tried python scripts outside home assistant that would modify the “configuration.yaml” file accordingly, adding the entities sent from detection messages. Home assistant did not allow me perform such action (I also could not find a solution to skip that)
Running a script that sends MQTTmessages to my broker is also not an option since I use cloud broker and I don’t want to depend on the internet connection for these specific sensors.
Do you have any ideas of how could that work? Or if it is even possible?
Update:
So I came up with some solution that works for:
- Home Assistant Docker
- Sensor and Binary Sensor
So it is more suitable in case you need to use ESPnow protocol with some descent amount of Battery Powered sensors, that will follow same structure and logic. Otherwise complexity of the solution is not worth it.
-
Create an ESPNow senor that sends a specific data format:
On boot it sends a detect message:
“Detect: Sensor, entity_id, unique_id, state, icon, device class, unit of measurement”
Then it sends the same message just starting with the word “Data”:
“Data: Sensor, entity_id, unique_id, state, icon, device class, unit of measurement”
-
Create an ESPNow to Serial Bridge and connected to Serial port of your HomeAssistant host. It should follow the same structure of the message received. The Bridge processes the message and forwards the message to the Serial Port. In case message starts:
- with “Detect”, it forwards the complete message
- with “Data”, it forwards Entity Id and State
-
Create a Sensor of the Serial integration that is receiving the String message. This Sensor triggers a shell command run_python_script.sh when it updates
-
Create the run_python_script.sh file that targets your python script and executes it. (This way you avoid running the python script through HomeAssistant that have many restrictions)
-
Create a long term token for the python script that will be needed since the external python script needs to send and receive some information to/from HomeAssistant
-
Create python script that does quite a few things:
- Calls HomeAssistant and receives current state of the Bridge (to get access to the message that triggered the detection)
- Splits the message and creates the entity data.
- Checks if the unique_id is already in the “templates.yaml” of HomeAssistant
- If it is not included, then it accesses the “templates.yaml” and add the configuration lines of the detected sensor.
- The configuration is such that from now on the sesnor is direclty connected to the Serial Sensor and does not need to trigger any other scripts to update. In case Serial sensor gets a Data message with the entity id of the current sensor, it updates the stae.
- Sends a notification to HomeAssistant that a new sensor was detected and calls user to Restart in order for the changes to take effect.
Ready to use!
In conclusion, a lot of effort. So I would say, if not under the prerequisites I mentioned in the beginning, no need to try it. Go with ZigBee…