ZHA zigbee device query via web sockets example

These are two very very basic Python 3 scripts to show how to get ZHA current zigbee devices and their attributes. These are just templates for folks with better python skills than mine that might help to create monitoring and diagnostic routines for zigbee networks based on the ZHA subsystem.

The first example shows how to dump the current zigbee devices and then parse the results with the linux ‘jq’ utility.

The second example shows how to collect the ZHA JSON attributes for zigbee devices into a SQLite database. This data can show how your network topology changes over time.

To use these routines, create a Home Assistant access token and insert it into the Python programs.

Hi, thanks for doing this. Do you know if there is a way to add zha attributes such as RSSI, LQI and last_seen to the actual entities that ZHA creates as entity attributes? I would like to write some automations that are triggered by these values. If not, can this be done by calling a python script?
thanks

To your question about adding these important attributes to HA entities, no I have not figured out the mapping between zigbee endpoints (and others) and HA’s entities. It’s gotta be possible, as other HA entities get this type of sensor connection data.

I have added a program, ws04.py, that does a little better job of visualizing devices and logging their state to a SQLite db. Including RSSI and LQI. I’m still learning what this data means. The ‘last seen’ time and ‘online state’ attributes seem to be important/useful bits of info. Have a look at what this program is doing and see if it might give you some ideas.

Good stuff and very interesting. I wish that I could be more help with this.
I tried running your first python script but was unsuccessful as I run HA OS in a Proxmox VM and in that environment python scripts are very restricted and don’t allow imports so the script would not run.
I also installed the pyscript addon hoping that I could run there but it crashed my entire HA system and had to do a cold boot.
I recently switched to ZHA from zigbee2mqtt and zigbee2mqtt did expose RSSI and LQI values as entity attributes so I was disappointed that ZHA does not.
I am hoping that the developers will eventually add this.
thanks for your work.

You should be able to run the web socket calls on a different machine than your server and from outside your VM as well. The web socket calls are exactly the same tech as the HA frontend uses. So anywhere you can run your HA frontend in a browser you can run the python scripts. Just put the IP address of your HA server into the python code as well as the long lived token you generated.

Yes, getting LQI and RSSI are among the reasons I started this, not as good as having them as direct attributes in a sensor, but if you can get my ws04.py running, it is recording the value in the SQLite database now. happy 2 help.

This is a CREE Connected A-19 60W equivalent USA bulb about 30 feet from the coordinator.
Here is a first pass at visualizing some of the ‘over time’ data I am capturing with the ws04.py web socket call to ZHA web sockets. This is about 12 hours of data. I was calling the ZHA web socket once every 5 seconds.

My first take away and thing to research, the RSSI, plotted on the left axis and the LQI plotted on the right axis overlay each other it looks like exactly. The LQI and RSSI can move out of correlation if noise levels change, so maybe no noise change. Or I could have an error in data or plot, BEGINNER!!! Much to learn.