Set a item to Null if not updated?

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?

Getting a null value to put back into the item is shown in this post:

But you’d need to wrapper it in a rule, with an internally cancelled/started Timer, that triggers 5 minutes after the last received update.

There are examples of that in the openHAB wiki

1 Like

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:

  1. The code DateTimeItem(“trashTalking”).state is obviously for a DateTime object, is there something similar for a Number?
  2. 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?

In the example, substitute NumberItem for DatetimeItem and it works there also.

These all descend from the internal GenericItem:

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.

The Timer code will be a variation of this example from the Wiki:

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"
	Item Temperature_Outside recieved update
	//reset the old timer
	if(timer!=null) {
            timer = null

timer = createTimer(now.plusSeconds(10)) [|
            var nullValue = new NumberItem("Temperature_Outside").state
	postUpdate(Temperature_Outside, nullValue)

I’ll give it a go a little later.

Cheers :smile:

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.

Ahh, yes. That was just copied from the example, I need to set that to 300.

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??

The Expire binding does exactly that.

1 Like

Thank you! That saved me a lot of typing!