openHAB: reset DateTime in rules

Posted by ads' corner on Friday, 2021-01-01
Posted in [Openhab]

In one of my openHAB rules I’m using a timestamp to track when an Item went offline. This is used in my monitoring to let the user (me) know how long the Item is already offline.

Background: we have a couple ChromeCasts in our network. When they are connected to a 5GHz Wi-Fi they are quite unstable and often loose the network connection. Usually the openHAB binding reconnects the device quickly, but it happens that the device stays offline. It might have something to do with the router, but nevertheless I want to know when things go offline.

For this I defined a virtual Switch as DateTime:

DateTime	LivingRoom_OfflineSince		"Offline Since [%tY-%tm-%td%tH:%tM:%tS]"

When the ChromeCast goes offline, this Switch is updated in a rule with the current time:

LivingRoom_OfflineSince.postUpdate( new DateTimeType() )

However resetting the DateTime switch when the item comes back online proved to be a small challenge, the documentation is unclear. People write online about UnDeftype, UNDEF(unknown), UnDefType.UNDEF, UnDefType.NULL and a couple more.

In the end it’s quite easy:


Just update the Item with NULL. Don’t use sendCommand() here, you only want to update the Item, not send a new command to a virtual switch.

Categories: [Openhab]