I’ve got OpenHab listening to MQTT data from some home made temperature sensors.
When it’s working, it all runs fine, but sometimes a sensor dies or my sensor hub locks up and OpenHab just reports the last value it received.
Is there any way to make it so my temperature items get set to Null or ‘-’ or something if they’re not updated for 5 minutes?
Hi Mark, thanks for pointing me in the right direction. I’m very new to OpenHab, so this is my first time looking at the rules.
It looks like I can build a rule around this, but I’ve got a couple of questions:
The code DateTimeItem(“trashTalking”).state is obviously for a DateTime object, is there something similar for a Number?
Does the rules engine automatically delete the object from the previous run? For example if I make a timer object and set it to 5 minutes, do I need to explicitly kill that object the next time the function is triggered? Or is it deleted when the function is re-triggered?
And you’ll see the it sets the state to UnDefType.NULL
For the Timer, you typically end up with the variable declared outside/above the Rule so it’s remembered from one execution to another.
Then, upon each Rule invocation, you need to cancel the last Timer and start a new one. As long as data keeps coming in from the device, the Rule keeps executing, and it’ll cancel the last Timer and start a new one.
If the rule doesn’t fire, then eventually the Timer will get to completion, and it then sets the value to the NULL Val, per the linked trick.
Yeah, I’ve been looking at that one, but it’ll be a bit different as I’m waiting for the value to update rather than change from 0 (at least I think that’s what the if statement is doing)
I’ve ended up with this code:
var Timer timer
rule "Reset Outside Temperature"
when
Item Temperature_Outside recieved update
then
//reset the old timer
if(timer!=null) {
timer.cancel
timer = null
}
timer = createTimer(now.plusSeconds(10)) [|
var nullValue = new NumberItem("Temperature_Outside").state
postUpdate(Temperature_Outside, nullValue)
]
end
Yup, that’s the type of variant I’d expect. Just be careful with firing the Timer every 10 seconds. You’d need a high velocity of real updates in order to have this not continuously reset the value to NULL.
This thread is kind of old. I wonder if there is an easier way using the current version of openhab.
I have approx. 40 items that need to be set to NULL if not updated after 2.5 minutes. That would be a lot of error-prone boilerplate code when I use Timers.
Something like this in the item definition would be nice:
Number TestItem {autoNull=“2.5m”}
I searched the docs, forums… really no other way??