changedSince not finding last change but reporting second to last change (OH 2.5.10)

Hey guys,

I am having some issues with the changedSince() function and could use your help. I have read other threads where people have also had issues with this function but haven’t seen a resolution to it.

In short, when I use the changedSince() function, it doesn’t pull the most recent change but instead pulls the second to most recent change.

Let me describe my setup. I have mapdb configured as my default persistence but I also have influxdb and Graphana installed and connected. This is how I check if influxdb is actually storing the item states.

my rule for testing:

rule “rule name”
Item Tester received update
var time = 6
if(JMHomekit.changedSince(now.minusHours(time), “influxdb”)) {
logInfo(“home.rules”, “true” + time) }
else {logInfo(“home.rules”, “false” + time)}

the item changed 5.5 hours ago and then again 10 minutes ago.
Running time = 6 yields: true6
Running time = 5 yields: false5

If I update the item again and run the same experiment by picking a point before the last change but after the second to last one, ill get the same results. I am having this issue with 2 separate items.

I should also note that the changes are all showing up on Graphana and when I run lastUpdate on an item, it provides the time of the last change.

I also can confirm that the items are in fact changing as opposed to just updating. This can also be seen on Graphana as well as in the below logs as these are simple ON/OFF switch items.

The 2 items in question are switched by Homekit automatically when myself or my wife gets home.

Lastly, I pulled the logs and I can confirm all changes to an item are present though I did see a few times where it changed from NULL to ON/OFF. Not sure what thats about.

2021-01-21 09:17:37.633 [vent.ItemStateChangedEvent] - JMHomekit changed from ON to OFF
2021-01-21 09:45:39.403 [vent.ItemStateChangedEvent] - JMHomekit changed from NULL to OFF
2021-01-21 10:02:44.455 [vent.ItemStateChangedEvent] - JMHomekit changed from NULL to OFF
2021-01-21 10:45:45.004 [vent.ItemStateChangedEvent] - JMHomekit changed from NULL to OFF
2021-01-21 12:35:08.721 [ome.event.ItemCommandEvent] - Item ‘JMHomekit’ received command ON
2021-01-21 12:35:08.722 [vent.ItemStateChangedEvent] - JMHomekit changed from OFF to ON
2021-01-21 13:15:42.758 [vent.ItemStateChangedEvent] - JMHomekit changed from NULL to ON
2021-01-21 13:35:06.924 [ome.event.ItemCommandEvent] - Item ‘JMHomekit’ received command OFF
2021-01-21 13:35:06.926 [vent.ItemStateChangedEvent] - JMHomekit changed from ON to OFF
2021-01-21 14:09:36.878 [vent.ItemStateChangedEvent] - JMHomekit changed from NULL to OFF
2021-01-21 14:43:33.157 [vent.ItemStateChangedEvent] - JMHomekit changed from NULL to OFF
2021-01-21 18:42:13.703 [ome.event.ItemCommandEvent] - Item ‘JMHomekit’ received command ON
2021-01-21 18:42:13.704 [vent.ItemStateChangedEvent] - JMHomekit changed from OFF to ON

I could get around this like others have by creating a couple dummy items to hold the DateTime of the last change but wanted to post it here to see if I was doing anything wrong and or if this is a bug, then hopefully it’ll get squashed.

That is suspicious. Items generally only get to NULL state when initialized i.e. system boot time or if you edit them. Any editing or rebooting going on?

A binding detecting a problem may set an Item to UNDEF, which is something different. Of course the homekit binding could possibly be being “naughty” here and set NULL.
Rules can set NULL too, though it’s ill-advised. I assume you’d know if you had any such.
I think both these cases would produce an events.log though - so it looks like unexpected initialization.

The persistence service does not record NULL/UNDEF states at all, just ignores them, so this may come into play here for your initial problem.

I have been rebooting my OH instance lately as ive been editing rules but I dont think I did it at those times. Could very well be the cause though. Even still I dont think that explains why its not picking up the times where it does change to OFF or ON.

To your other point, the first thing I did was to check all of my rules to see if there were any that set the value of the 2 items in question. Didn’t find any.

Probably not, with influxdb. (with rrd4j, say, periods of NULL would result in gaps in the dataset which are not handled well by persistence methods)
It’s just cause for concern if unexplained. It’s not some random condition.

There were a bunch of complaints in OH2 that changedSince did not work as expected with influxdb,I do not think they were resolved. The users just switched to another database like mySQL, mostly, and no follow up.

Ya I should have clarified that I am running OH 2.5.10. I want to update to OH3 but need some bindings to be updated first.

In the mean time I eliminated all changedSince functions and replaced them with an item that tracks changes. see below for anyone else running into this issue:

Item file:

DateTime JMLastUpdate //new dummy item to track changes in JMHomekit

New rule:

rule "JM Presence Last Update"
Item JMHomekit changed
logInfo("home.rules", "JMLastUpdate updated.")


rule "Rule name"
Item JMHomekit changed or
var DateTime jm = new DateTime((JMLastUpdate.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
var onedayago = now.minusHours(24)
    createTimer(now.plusSeconds(1), [| //to give the above rule time to update the JMLastUpdate item state
if(onedayago < jm) { 
    //do stuff