Adding read only data to openHAB

  • Platform information:
    • Hardware: Raspberry Pi 3B
    • OS: openHABian 3.2.0
    • openHAB version: 3.2.0
  • 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?

Sincerely,
a clueless beginner

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.

Can you tell us which device you have from this page?

@beazt the above is crucial - make sure you can verify that you’re getting data from your device, and verify the topic, using the above advice.

1 Like

What do I see in the logs:

openhabian@openhabian:/var/log/openhab $ cat events.log
2022-04-06 18:03:45.888 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SmartPlug_onoff' changed from NULL to ON
2022-04-06 18:03:48.965 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:broker:1b765ceec7' changed from UNINITIALIZED to INITIALIZING
2022-04-06 18:03:49.089 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:broker:1b765ceec7' changed from INITIALIZING to OFFLINE
2022-04-06 18:03:51.636 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:MotionSensor' changed from UNINITIALIZED to INITIALIZING
2022-04-06 18:03:51.700 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:MotionSensor' changed from INITIALIZING to OFFLINE (BRIDGE_OFFLINE)
2022-04-06 18:03:51.895 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:SmartPlug' changed from UNINITIALIZED to INITIALIZING
2022-04-06 18:03:51.943 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:broker:1b765ceec7' changed from OFFLINE to ONLINE
2022-04-06 18:03:51.959 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:MotionSensor' changed from OFFLINE (BRIDGE_OFFLINE) to ONLINE
2022-04-06 18:03:52.033 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:SmartPlug' changed from INITIALIZING to ONLINE
2022-04-06 18:11:27.121 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from UNINITIALIZED to INITIALIZING
2022-04-06 18:11:27.145 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from INITIALIZING to OFFLINE (BRIDGE_OFFLINE)
2022-04-06 18:11:49.125 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from OFFLINE (BRIDGE_OFFLINE) to ONLINE
2022-04-06 18:12:15.103 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from ONLINE to REMOVING
2022-04-06 18:12:15.129 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from REMOVING to REMOVED
2022-04-06 18:12:15.221 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from REMOVED to UNINITIALIZED
2022-04-06 18:12:15.263 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:test' changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)
2022-04-06 18:12:29.632 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:test' changed from UNINITIALIZED to INITIALIZING
2022-04-06 18:12:29.662 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:test' changed from INITIALIZING to ONLINE
2022-04-06 18:12:53.268 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:test' changed from ONLINE to REMOVING
2022-04-06 18:12:53.274 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:test' changed from REMOVING to REMOVED
2022-04-06 18:12:53.280 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:test' changed from REMOVED to UNINITIALIZED
2022-04-06 18:12:53.313 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'mqtt:topic:1b765ceec7:test' changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)
openhabian@openhabian:/var/log/openhab $ cat openhab.log
2022-04-06 18:03:00.123 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Time zone set to 'Europe/Berlin'.
2022-04-06 18:03:00.222 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Locale set to 'de_DE'.
2022-04-06 18:03:36.665 [INFO ] [.core.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007
2022-04-06 18:03:41.664 [INFO ] [e.automation.internal.RuleEngineImpl] - Rule engine started.
2022-04-06 18:03:45.424 [INFO ] [org.openhab.ui.internal.UIService   ] - Started UI on port 8080
2022-04-06 18:03:48.437 [INFO ] [ab.ui.habpanel.internal.HABPanelTile] - Started HABPanel at /habpanel
2022-04-06 18:03:51.159 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to '192.168.178.81' with clientid 30951657-8431-464e-8e8a-c7993a6b3ade
2022-04-06 18:04:17.005 [WARN ] [.io.rest.auth.internal.TokenResource] - Couldn't find a user with a session matching the provided refresh_token
2022-04-06 18:04:17.010 [WARN ] [.io.rest.auth.internal.TokenResource] - Token issuing failed: invalid_grant

I am not sure if these are the right logs, I can’t get anything out of these.

My configuration:

UID: mqtt:topic:1b765ceec7:SmartPlug
label: SmartPlug
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:1b765ceec7
channels:
  - id: smartPlug
    channelTypeUID: mqtt:switch
    label: onoff
    description: ""
    configuration:
      commandTopic: zigbee2mqtt/SmartPlug/set
      off: OFF
      on: ON
  - id: temp
    channelTypeUID: mqtt:number
    label: temp
    description: ""
    configuration:
      unit: °C
      min: -30
     stateTopic: zigbee2mqtt/SmartPlug/device_temperature/get
     max: 100

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:

{"consumer_connected":true,"consumption":0.02,"current":0,"device_temperature":29,"energy":0.02,"illuminance":0,"illuminance_lux":0,"linkquality":102,"power":0,"power_outage_count":1,"state":"ON","temperature":29,"voltage":248}

so basically it is publishing to device_temperature and energy.
zigbee2mqtt/SmartPlug/device_temperature/get did not help to get my value.

Thanks for the quick reply

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 doesn’t look like your openHAB Broker/Bridge Thing is keeping a connection to your MQTT broker in the first place. This needs investigating first.

Perhaps share you configuration of your Broker Thing?

It says it right there in the documentation:

Device temperature (polled every 30 min).

Though I guess this is how often it is polled. I suppose the (same) data could be published more often. As Rich says, monitor what MQTT Explorer says…

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…

Forget all this - your connection between openHAB and your MQTT broker isn’t working. One thing at a time.

Please show us your configuration in openHAB, and also how you configured MQTT Explorer so we can compare.

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:

device_temperature":32,"energy":0.03,"illuminance":0,"illuminance_lux":0,"linkquality":105,"power":0,"power_outage_count":1,"state":"ON","temperature":30,"voltage":255}

I don’t know, we can’t see any of this! Please share your configurations using the Code tab and screenshots.

Is that the whole message? Looks like the beginning of it is missing.

Either way, go back to your Channel and choose to configure it. Ensure that Advanced options are available. In Incoming Value Transformation add

JSONPATH:$.device_temperature

Make sure you have the JSONPATH Transformation Service installed.

1 Like

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 :smiley: . 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!

I would now probably create an identical Channel in your Thing, but of Number type, and link it to a Number Item, as your incoming value is a number.

Yes, that is what I did and both Strings and Number types are working.