The concept
In fall 2023 there are very affordable Tuya zigbee temperature sensors on the market. A temperature and humidity sensor can be found on Aliexpress for about 5€ and a zigbee gateway for about 15€. This article describes one approach to connect a bunch of such cheap zigbee sensors and a zigbee gateway to OpenHAB 3 or 4.
The procedure is as follows
- Install mosquitto
- Install json transformations add-on
- Install Javascript Scripting add-on
- Install divideby10.js to transforms
- Connect sensors to the zigbee gateway with the phone app
- Discover local sensor addresses to enable local access to sensors
- Read sensor values from sensors with a script locally using tuya-cli
- Publish sensor values on local mqtt using Mosquitto
- Import sensor values to OH using a mqtt generic thing to assigned individual channels and items
Install necessary add-ons
Install mosquitto like described here. Mosquitto is the MQTT broker that delivers the temperature measurements to you.
Settings => Other add-ons
Install Jsonpath transformations:
Jsonpath transformations are necessary to be able parse the measurement value from the mqtt output.
Settings => Other add-ons
Install Javascript Scripting to enable running the divideby10.js script
Further, to be able to show the measurement correctly, the value read from the sensor needs to be divided by ten. To do this, create a file with the name divideby10.js in /etc/openhab/transforms with the following content
( function(i) { return i / 10 } ) (input)
Remember to change ownership of the file to openhab:openhab with
sudo chown openhab:openhab divideby10.js
Discover your tuya device local keys
Without the sensor local keys you can not read the sensor data to OpenHAB. Discover your devices’ local keys, ids and addresses as described here: https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md
Make a rule script to read and publish sensors on mqtt regularly.
In the example below:
- First line reads the sensors
- Second line formats the output of tuya-cli for mqtt
- Third line publishes the result on your mqtt network
var yourSensorName=actions.Exec.executeCommandLine(time.Duration.ofSeconds(20), "tuya-cli", "get", "--ip", "your.zigbee.gateway.ip.number", "--id", "yourZigbeeGatewayId", "--key", "yourZigbeeGatewayLocalKey", "--cid", "yourSensorCID", "--protocol-version", "3.3");
yourSensorName=yourSensorName.replace(" '","\"").replace(": ",":").replace(" '","\"").replace("'","\"").replace("'","\"").replace(": ",":").replace(" '","\"").replace("': ","\":").replace(" }","}");
actions.Things.getActions("mqtt", "mqtt:broker:******").publishMQTT("tuya/yourSensorName", yourSensorName, false);
- yourSensorName is a variable name for your sensor e.g. masterBedroomSensor
- your.zigbee.gateway.ip.number is the IP of your zigbee gateway on your local network. Check the gateway mac adress from the Smart Life app, and look for that mac adress on your router DHCP client list. The corresponding local IP adress is what you need. e.g. 192.168.0.100
- yourZigbeeGatewayId, yourZigbeeGatewayLocalKey and yourSensorCID are discovered in the previous section
Set the script to trigger e.g. every hour.
Create a thing where you collect all your measurements
Things => plus in blue ring => MQTT binding => Generic MQTT thing
Name the thing e.g. “yourMeasurementThing”
Create a channel to your sensor
Select Channel tab under your newly created Thing => Add channel
Name the Channel e.g. “yourMeasurementChannel”
Add your device into the MQTT State Topic as below. Replace “takka” with yourSensorName
Configure the transformations as below
Create an item for the sensor
yourMeasurementThing => channels => yourMeasurementChannel => Add link to item => Create New Item
Configure the new item as below
Repeate the create channel and create item phase for each of your sensors.
Conclusion
You should now have a temperature item that is automatically updated once every hour with proper units and formatting. You can add new sensors by adding channels and corresponding items under yourMeasurementThing. I have 8 sensors running currently all around the house.
I initially tried the Smarthome/J tuya binding to read the sensors, but it does unfortunately not in fall 2023 support devices behind a zigbee gateway and therefore I resorted to this solution. The procedure described above works, but is unncessary complicated. It would make sense to update the item value directly from the output of tuya-cli and skip the mqtt publisihing totally. I did not find out how to parse the measurement value directly from the tuya-cli output, so I resorted to this cludge with mqtt. This solution will be obsolete once the Smarthome/J Tuya binding starts supporting devices behind a zigbee gateway.
I’d be happy to receive ideas on how to simplify the method.