Getting last updated of Arduino temp and humidity sensor

Hi there, first of all, thanks for OpenHab, it’s a great piece of software, if a little daunting for the beginner!

Some background, I’ve managed to install and configure Openhab, it’s currently displaying weather in my area from Openweathermap, and also displaying the temp and humidity of an Arduino device with a DHT11 sensor on it (this is via an Arduino gateway, then into a Raspberry Pi via MQTT)

Since the temp/humidity node is battery powered, I wanted to know when the last update was (it’s set to 60 second updates). In simple terms, how do I achieve this? I’m persisting the data on each update of the node, so I have that setup and working.

I’m not completely sure on how to do it entirely, but my method would be to capture the time on the temp node (through maybe a clock module), take the time from the module as your node is creating the message to send, then when it sends the message, with the time inside of the message, get your controller to pull the time out of the message and then either print it to the console or even set yourself a item on the webapp called something like “Time of temperature reading on Node1”. That is my two cents, and i’m sure that someone else would come up with a better/sleeker/more efficient method.

Here is how I do it:

From my Items file:
Number itm_temp1_mqtt “Temperature [%.2f]” (Indoor, Temp) {mqtt="<[mymosquitto:home/rfm_gw/nb/node10/dev48:state:default]"}
Number itm_hum1_mqtt “Humidity [%.2f]” (Indoor) {mqtt="<[mymosquitto:home/rfm_gw/nb/node10/dev49:state:default]"}
DateTime sensor1_LastUpdate “Sensor-1 Last Update [%1$ta %1$tR]” (Update)

From my rules file:
rule “sensor1_lastupdate”
when
Item itm_temp1_mqtt received update or
Item itm_light1_mqtt received update
then
postUpdate(sensor1_LastUpdate, new DateTimeType())
end

From my sitemap file:
Frame label=“Sensor-1”
{
Text item=itm_temp1_mqtt icon=“temperature”
Text item=itm_hum1_mqtt icon=“temperature”
Text item=sensor1_LastUpdate icon=“clock-on”
}

Since you are persisting the data, you can also use the PersistenceExtensions.lastUpdate function to get the last update time.

Thanks everyone, I wanted to go with the persistence which steve1 mentioned, can someone tell me where I need to reference this i.e. items, sitemaps?

Thanks

Thanks, I tried this too and nothing is happening! Copied everything, only thing I changed was the Item names in the rules file. Does the rules file get picked up automatically?

(in response to kmac2001 code)

Changes to your rules file should get picked up if you have a line in your openhab.cfg file like:
folder:rules=10,rules

Thanks, it appears they are, but I’m getting this error

2015-11-15 16:01:47.236 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule  'sensor1_lastupdate' java.lang.NullPointerException: null

Try putting the following as the first line in your rules file:

import org.openhab.core.library.types.*

That did the trick…thanks, much appreciated - next question, can I combine 2 items in a single sitemap items? I simply want to display the temp and humidity together with the last updated in brackets, on the same line.

Here is how I combine wind direction and speed on one line. You should be able to do something like this with your temp and humidiy.

Item file:
Number Wind_Speed_Mph “Windspeed [%.0f mph]” {weather=“locationId=home, type=wind, property=speed, unit=mph”}
String Wind_Direction “Wind direction [%s]” {weather=“locationId=home, type=wind, property=direction”}
String Wind_Out “Wind [%s]”

rules file:
rule “wind_speed”
when
Item Wind_Direction changed or
Item Wind_Speed_Mph changed or
then
var Number speed = Wind_Speed_Mph.state
var String dir = Wind_Direction.state
postUpdate(Wind_Out, dir + " at " + speed + " Mph")
end

sitemap file:
Text item=Wind_Out

This produces a line like:
Wind NW at 10 MPH

2 Likes

Thanks! Much appreciated, there is a quite a learning curve, but I think it’ll be worth it. Last (well maybe not), how do I, instead of passing the date that it was last updated, but instead say last updated, for example, 1 minute ago?