How does OpenHAB cache the state of items

I have configured OpenHAB to read data from my central heating system via the Modbus binding, and it works well. I also want to expose the data to HomeKit, and for that purpose I need to add a few “derived” dummy switches to show, for example if the filter needs to be replaced based on the life time of the filter, below are the items relevant:

Number	prmFilterInlet_TimeThreshold  "Inlet Filter Time Threshold"				<calendar>		(gNilanVentilation)			{channel="modbus:data:Ventilation:FilterReplaceWrite:hold1326:number"}
Number	prmFilterInlet_PassDays   "Inlet Filter Replace"				<calendar>		(gNilanVentilation)			{channel="modbus:data:Ventilation:FilterReplaceRead:input1328:number"}
Switch HomeKitVentilationFilterItem "Ventilation Filter Replace"  

Basically I want to set the dummy switch HomeKitVentilationFilterItem on when the filter needs to be replaced, and I configured the following rule:

rule "Nilan Ventilation Filter Replace"
when
    Item prmFilterInlet_PassDays changed
then
	if(prmFilterInlet_PassDays == 360)  {
		sendCommand(HomeKitVentilationFilterItem, ON)
	}
	else  {
		sendCommand(HomeKitVentilationFilterItem, OFF)
	}
end

Very simple stuff and it works as expected when the system is initialised, I can see the state of HomeKitVentilationFilterItem changed from NULL to OFF. However when I run the system a few days, the value HomeKitVentilationFilterItem has changed back to NULL, below is the result when I call the REST service (which is also used by the Homebridge Plugin I think):

{"link":"http://IP_ADDRESS:80XX/rest/items/HomeKitVentilationFilterItem","state":"NULL","editable":false,"type":"Switch","name":"HomeKitVentilationFilterItem","label":"Ventilation Filter Replace","tags":[],"groupNames":[]}

I tried to find an trace in the events.log file and I can only see the dummy switch has been set to OFF everyday:

2019-06-23 02:07:48.024 [ome.event.ItemCommandEvent] - Item 'HomeKitVentilationFilterItem' received command OFF
2019-06-24 03:19:55.048 [ome.event.ItemCommandEvent] - Item 'HomeKitVentilationFilterItem' received command OFF

There is no indicate in the log that the value is reset to NULL. Given the rule is triggered very rarely (on daily basis), I’m wondering if there is some kind of cache timeout in OpenHAB, so when the value isn’t changed for X hours, it will try to pull the value again. And because the dummy switch isn’t connect to any channel, therefore it returns NULL.
Any suggestion on possible root cause or potential fix to the problem is appreciated!

Just a wild guess, but look at Persistence.

Thanks for reply!
I haven’t configure any persistence. Just did an experiment by changing the days which triggers the rule, and now the state is set to OFF and HomeBridge is happy. After 5 minutes I called the API again and it still shows OFF, so the last state is indeed cached somewhere…

Nope.
Your Item will be set to NULL at each openHAB reboot, or each time you edit your xxx.items file.

Use of persistence with restore-on-startup could preserve the state across OH reboots.
But doesn’t cover the editing case.

I’d add a rule trigger to initialize it at other times than when your device day counter rolls over. And not bother with persistence, assuming it’s not urgent and coming right in the night will do.

rule "Nilan Ventilation Filter Replace"
when
    System started or
    Item prmFilterInlet_PassDays changed
then

Guessing you might want to look at that if() condition as well
You must examine the .state of an Item.
You probably want the alert on day 361 as well.

if ( prmFilterInlet_PassDays.state >= 360)  {
1 Like

… and make sure you also check openhab.log, I would suspect some entries there as well related to these items and rule.