Issue of the topic:
I am trying to read the temperature of my Aqara Smart Plug in openHAB. I already got it paired and working and can turn it on and off with a switch channel. where I publish a message to zigbee2mqtt/SmartPlug/set.
Now I want to to read for example the temperature or power the plug is publishing. So what I tried to do is create a channel and added this “zigbee2mqtt/SmartPlug/device_temperature” to MQTT State Topic and linked with the item Number, hoping it would just show me the temperature but all I get is NULL. I tried to understand what it is publishing, but unfortunately I do not find any documentation for the state property. The documentation it is not possible to read with /get or write with /set. How do I read this then?
First, what do you see in the logs? The logs are vital to understand what’s going on, particularly if there are errors.
Post your Thing configuration. If defined in the UI, click on the code tab and paste in the code. Use code fences.
```
code goes here
```
How often does zigbee2mqtt publish this message? Is it retained? If it’s not retained, OH will not get the reading until the reading is published again.
Get an MQTT client such as MQTT Explorer or MQTT.fx. This will let you see all the message traffic on your MQTT broker. That should tell you what messages are published to which topics.
Note, in MQTT there is no such thing as “read only”.
Well, you’ve set up and it’s working to publish to zigbee2mqtt/SmartPlug/set. Perhaps the current state of the temp sensor is published to zigbee2mqtt/SmartPlug/device_temperature/get.
this is the plug I use. How often does it publish? Sadly I don’t know :(, also I have no idea what it means if it is retained. I used the MQTT Explorer and this is what the plug is publishing:
You can watch that topic in MQTT Explorer to see how often it’s publishing. It will also tell you whether the message is retained or not.
And that’s a big JSON string. You’ll have to apply a JSONPATH transformation to extract the temperature reading. You can’t set a Number Item to a string like that and it has all sorts of additional information in it.
Is there a reason you are not using the Zigbee binding? It’d be a whole lot easier to get this stuff working given your overall lack of experience with MQTT. MQTT is pretty low level. You have to have a good set of knowledge to understand how it works and how to use it to be successful.
It seems like it is published every 3-8 minutes unregularly.
And is the messsage published more like this? {"level":"info","message":"MQTT publish: topic 'zigbee2mqtt/SmartPlug', payload '{\"consumer_connected\":true,\"consumption\":0.02,\"current\":0.0277,\"device_temperature\":29,\"energy\":0.02,\"illuminance\":0,\"illuminance_lux\":0,\"linkquality\":105,\"power\":7.07,\"power_outage_count\":1,\"state\":\"ON\",\"temperature\":31,\"voltage\":255}'"}
The reason I am using MQTT is because I used the zigbee binding before and had some issues, and while searching for a solution people didn’t provide the solution but said: just use the mqtt binding, it is much easier… so here we are…
@hafniumzinc I’m not sure what you mean by configuration, because there is not much to configure besides which bridge to use.
As of my understanding, all I need is the correct state topic to read. As in MQTT state topic when I configure my channel of my Thing. If it is far too complicated to read a published value with MQTT, I might look into the regular zigbee binding again.
The documentation says it is not possible to read with /get, so I don’t know how else to read this…
I’m not quite sure what you mean by configuration in openHAB, but I try my best:
I created a Thing with MQTT Broker and the only thing I could configure was the IP, so I entered 192.168.178.81. That was all I did to add this Thing. I use mosquitto as a Broker.
Now, in MQTT Explorer I enter that IP as Host with port 1883. If you need more information, such as how I configured mosquitto, I’ll gladly provide you these information.
You’re also saying that the connection is not working, but I can switch the plug on and off, after I publish a message to zigbee2mqtt/SmartPlug/set
Reliably? I mean, I’m not just guessing - go look at the log extract you posted: all MQTT Things go offline.
But it looks like you’ve managed to connect two things together via MQTT, which is good! Now just add a new String type Channel to your device Thing, with the correct topic. Link that to a String Item and confirm that this Item holds the same payload that you see in MQTT Explorer. Once that’s done, we’ll work on extracting the temperature.
Yes, it works 100% of the time I trigger the switch in openHAB.
How do I get a whole String Item? I created a String Channel of the Smart Plug and entered this:
zigbee2mqtt/SmartPlug
in MQTT State Topic, also I tried zigbee2mqtt/SmartPlug/get. Then I added an item with the property String. Is that correct so far? Because that is where it says NULL
Nvm, I think it was not published yet and that’s why it said NULL, I have the message now which says:
Oh my god, it is working as intended. Thank you so much @hafniumzinc!!
I did not install the JSONPATH Transformation Service because I didn’t even know openHAB had a Store for those kind of Services . So I installed the service and now I get correct values for my Smart Plug and my Motion Sensor.
Have an amazing day, and thanks again!