[SOLVED] Mqtt Temp and humidity setup

Hi Richard,

On a reboot, my devices are staying in their previous state (at least as I’m still in testing mode, and all my switched are off at the time of reboot). That said after the reboot the icons are all showing on but the switches in the ui are all off.

Unfortunately after installing the mapdb and adding the persistence then performing a remote reboot, the system did not come back up so I now have to wait until the other half will power down and up again. So it is only the icon states that I wanted to retain or refresh.
I’m not sure about the QOS state ado I will have to go read up on that aspect before I know how to answer that one.

Vincent’ s advice to use the expire binding is very useful and an easy solution to signal you the sensor somehow is ofline.
Keep in mind though that if your temperature or humidity are also in e.g. an ‘average’ group, that ‘0’ value will influence the average. Maybe not a big thing but if you have other processes depending on it, it may become a big thing.
Obviously you need to solve why your device is offline, but as it is hanging on an ESP8266, you could have an MQTT message sent when your sensor cant be read.
Also, when it would be the entire ESP8266 that goes offline for whatever reason, that can be checked too with the network binding using lines like (this is for my phone, but works on all sorts of connected devices):

Switch Telefoon_OnLine "Online [%s]" <gsm> (Security){channel="network:pingdevice:Redmi:online"}
Number Telefoon_Latency "RedmiLatency [%s msec]" <time> (Machines){channel="network:pingdevice:Redmi:latency"}
DateTime Telefoon_LastSeen "Last Seen [%1$td/%1$tm %1$tH:%1$tM]" <time> (Security){channel="network:pingdevice:Redmi:lastseen"}

Obviously the first line tells you when a device is offline or not, but the other two lines could give a clue to why or how long

1 Like

Just checked. You CAN ping tasmota devices!!

indeed :slight_smile:
I ping my telephone, raspis and esp’ s just to see if all is OK and additionally I use my phone ping to determine f I am at home.

I use a MIN temperature group to warn me for freezing… so I cant use the expire to set to zero

For the tasmota devices in particular, the broker will issue a LWT message (Last Will and Testament) when they disconnect.
I use that to monitor if they are online:

Example:

String LargeBedroom_RadiatorValve_OnlineStatus { mqtt="<[mybroker:tele/LargeBedroom_RadiatorValve/LWT:state:default]" }

Screenshot:
image

1 Like

Very useful indeed

What is this for? I have a similar doubt, I need to extract temperature value from a string data that consists of other information, Can I use the above code to do that.

My data that I subcribe from mqtt looks like the screenshot attached below
image

Could you copy and post that json, please and I can tell you how to get the data you want out of it

1 Like

Okay so i tried doing it but it wasn’t happening.
Below is the json data i subscribe, The data i need is object adc

{
  "applicationID": "2",
  "applicationName": "",
  "deviceName": "0",
  "devEUI": "",
  "rxInfo": [
    {
      "mac": "",
      "rssi": -119,
      "loRaSNR": 4.5,
      "name": ",
      "latitude": ,
      "longitude": ,
      "altitude": 40
    }
  ],
  "txInfo": {
    "frequency": 868500000,
    "dataRate": {
      "modulation": "LORA",
      "bandwidth": 125,
      "spreadFactor": 12
    },
    "adr": true,
    "codeRate": "4/5"
  },
  "fCnt": 139,
  "fPort": 11,
  "data": ",
  "object": {
    "adc": "1071"
  }
}

The items file code is as follows

Number home_temp "Living Room [%.1f °C]" <temperature> ["CurrentTemperature"] {mqtt="<[mosquitto:application/2/node/0000000000000013/rx:statestate:JSONPATH($.object.adc)]"}

It didn’t work with this code so i tried creating a rules file, I don’t have much idea on it .

rule "Parse Room Temperature"
when
        Item home_temp received update
then
        val tem = transform("JSONPATH", "$.Object.adc", home_temp)
       

       
end

Am I doing something wrong…?

Number home_temp "Living Room [%.1f °C]" <temperature> ["CurrentTemperature"] { mqtt="<[mosquitto:application/2/node/0000000000000013/rx:state:JSONPATH($.object.adc)]" }

That should work, provided your mqtt binding is configured properly and running

But it’s not my mqtt binding and json transform bindings are installed…

New item:

String MQTTTest { mqtt="<[mosquitto:application/2/node/0000000000000013/rx:state:default" }

Rule:

rule "Parse Room Temperature"
when
    Item home_temp received update
then
    val tem = transform("JSONPATH", "$.Object.adc", home_temp)
    logInfo("TEST", tem.toString)
end

What do you see in the log?

The variable tem is just used in rules… Is that wrong??

Logs look like this

2018-08-09 15:51:58.000 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'outlet.rules'
2018-08-09 15:51:58.012 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'outlet.rules' is either empty or cannot be parsed correctly!
2018-08-09 15:51:58.084 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'outlet.rules'
2018-08-09 15:53:16.886 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'oulet.items'
2018-08-09 15:53:16.894 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'oulet.items'
2018-08-09 15:53:41.199 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'outlet.sitemap'
2018-08-09 15:53:41.201 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'outlet.sitemap' is either empty or cannot be parsed correctly!
2018-08-09 15:53:41.244 [ERROR] [.core.internal.folder.FolderObserver] - Error handling update of file 'C:\openHAB2\conf\sitemaps\outlet.sitemap': null.
java.lang.NullPointerException
	at org.eclipse.smarthome.io.rest.sitemap.internal.PageChangeListener.getAllItems(PageChangeListener.java:125)[117:org.eclipse.smarthome.io.rest.sitemap:0.9.0.b5]
	at org.eclipse.smarthome.io.rest.sitemap.internal.PageChangeListener.updateItemsAndWidgets(PageChangeListener.java:75)[117:org.eclipse.smarthome.io.rest.sitemap:0.9.0.b5]
	at org.eclipse.smarthome.io.rest.sitemap.internal.PageChangeListener.sitemapContentChanged(PageChangeListener.java:217)[117:org.eclipse.smarthome.io.rest.sitemap:0.9.0.b5]
	at org.eclipse.smarthome.io.rest.sitemap.SitemapSubscriptionService.modelChanged(SitemapSubscriptionService.java:277)[117:org.eclipse.smarthome.io.rest.sitemap:0.9.0.b5]
	at org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl.notifyListeners(ModelRepositoryImpl.java:286)[122:org.eclipse.smarthome.model.core:0.9.0.b5]
	at org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl.addOrRefreshModel(ModelRepositoryImpl.java:136)[122:org.eclipse.smarthome.model.core:0.9.0.b5]
	at org.eclipse.smarthome.model.core.internal.folder.FolderObserver.checkFile(FolderObserver.java:234)[122:org.eclipse.smarthome.model.core:0.9.0.b5]
	at org.eclipse.smarthome.model.core.internal.folder.FolderObserver.processWatchEvent(FolderObserver.java:297)[122:org.eclipse.smarthome.model.core:0.9.0.b5]
	at org.eclipse.smarthome.core.service.WatchQueueReader.run(WatchQueueReader.java:206)[98:org.eclipse.smarthome.core:0.9.0.b5]
	at java.lang.Thread.run(Unknown Source)[:1.8.0_171]
2018-08-09 15:53:41.258 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'outlet.sitemap'

My rules file have the following code

rule "Parse Room Temperature"
when
        Item home_temp received update
then
        val tem = transform("JSONPATH", "$.Object.adc", home_temp.toString)
		home_temp.postUpdate(tem)
       
end
rule "Parse Room Temperature1"
when
    Item home_temp received update
then
    val tem = transform("JSONPATH", "$.Object.adc", home_temp)
    logInfo("TEST", tem.toString)
end
rule "secure"
when 
  Item Office_temp received update
 then
  
      try{
	  val temper=(Office_temp.state as Number)
	  if(temper>=1){
	  sendNotification("xxxx@gmail.com", "security: someone has entered")
	  }
	  
	  }
	  catch(Exception e){
        logError("ID", "Error calculating next Node ID: " + e.toString)
    }

No need for the sitemap. Just monitor the log file

1 Like

I did, not sure what the error means though, it says rules file is either empty or cannot be parsed correctly.

OK post the rule file, please

The rules file above is missing an “end” at the end of last rule.

The rules file doesn’t give errors now but neither does it show the logs of mqtt subscription. Although I can see the data in mqtt lens. I even get info in logs as starting mqtt broker connection mosquitto. Although there is an info that

2018-08-10 14:11:36.860 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'b.items'
2018-08-10 14:11:37.143 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'mysql.persist'
2018-08-10 14:11:39.438 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'b.rules', using it anyway:
The value of the local variable tem is not used


My rules file currently look like this


rule "Parse Room Temperature"
when
        Item home_temp received update
then
        val tem = transform("JSONPATH", "$.Object.adc", home_temp)    
end

rule "Parse Room Temperature_1"
when
    Item home_temp received update
then
    val tem = transform("JSONPATH", "$.Object.adc", home_temp)
    logInfo("TEST", tem.toString)
end```

so i edited it like this

```php
when
        Item home_temp received update
then
        val tem = transform("JSONPATH", "$.Object.adc", home_temp)    
        home_temp.postUpdate(tem)
end

now i don’t get that info in logs but it’s still now displaying the value in openhab or logs.
Any help on what could be the issue…

Can you post the current definition of the item home_temp, please?