[SOLVED] Rule: Check if string "value" has contained certain value last 5 seconds

So, I have a Time cron rule triggered every 5 seconds. Then want to check if a device string value has contained a certain value within the last 5 seconds. Is this possible?

My door lock has a string variable CHANGED_VIA, where output is AUTO, CODE or THUMB. Further, my door lock is set to AUTO lock when door is closed.

Want to use the time cron rule, followed by checking condition if CHANGED_VIA is/has been “CODE” for the last 5 seconds, and then send command.

Have tried using ‘when CHANGED_VIA changed to CODE’, however, as the lock is set to AUTO lock the state change is not necessarily recognized as the CHANGED_VIA variable goes immediately back to previous value (communicated via cloud API).

Therefore want to see if the state change is recognized if “listening”/checking variable over 5 seconds intervals.

That’s probably the wrong approach.

If you trigger a rule from an Item changing to XX, it doesn’t matter if the device changes it back. Your rule knows it was XX to begin with.
Do you need to wait for it change back or something, before taking action?

The door lock is communicating with a dedicated bridge/gateway, which then is implemented in OpenHab via a dedicated binding.

What I see, is that for regular state change (with sufficient time between open / locked), the state change is recognized and logged by OpenHab. However, with open / autolock, with short time between, the OpenHab binding / log does not recognize / log the state change (even though recognized by the doorlock gateway).

So, as an alternative, I thought of checking if string value has been (contained) a certain value looking within the latest time interval (e.g 5 seconds), just to see if it could be possible to retrieve/scan values otherwise.

Well if openHAB cannot see this change that happens too quickly, then it cannot see the change.
I don’t really follow what you mean about taking action to see what cannot be seen.
Are you just trying to poll your mystery gateway? Does it allow reading of historical events?

Yes, that is what I’m checking if could be possible at all, as the doorlock gateway itself actually keeps track of historical lock/unlock events.

As the item in OpenHab is connected to the required channel of the gateway, I thought it could be possible to use rule to check for historical values, but maybe I’m completely wrong?

I’ve no idea if openHAB rules can interrogate your mystery gateway via an unknown binding.

A rule cannot check openHABs historical record of events that openHAB apparently cannot see.

I totally see your point, and understand the challenge.

Regardless, we have sidetracked from the actual question which was whether or not it is possible to check if a string “value” has been/contained a certain “string value” for a given/historical time interval.

In part that is because this is an XY Problem.

Anyway, you can set/reschedule a Timer when the Item changes state. When the Timer goes off you know the Item has been in the same state for that amount of time. Search for “debounce” and you will find lots of examples.

You can use Persistence and use MyItem.lastUpdate(true) to get the time the Item was last in a state different from the current state. If you use MyItem.lastUpdate() you will get when the Item changed to it’s current state.

You can use the Expire binding to set an Item back to some default state when it remains in a given state for a defined amount of time.

But none of this actually solves your problem. If OH never sees the change in the first place, it will not have any information about it. It doesn’t go back to the device to ask it about past state. If OH never saw the event, there is no way to recover that missed event from within OH.

Setup persistence and then use changedSince or updatedSince. If true, this will confirm that your Item has a particular state and has not changed for 5 seconds…

# Jython
from core.actions import PersistenceExtensions
from org.joda.time import DateTime

if items["Changed_Via"] == StringType("CODE") and not PersistenceExtensions.changedSince(itemRegistry.getItem("Changed_Via"), DateTime.now().minusSeconds(5)):
    # do stuff
// Rules DSL
if (Changed_VIA.state == "CODE" && !Changed_VIA.changedSince(now.minusSeconds(5))) {
    // do stuff

Note that these suggestions all assume this target string “value” is available as an Item.

Appreciate the input and suggestions.

As the binding is set up for polling, I will have a look at ‘myItem.sendCommand(RefreshType.REFRESH’ to see if I can force more frequent updates from the gateway (e.g using a Time cron rule, every 5 seconds).

If this works, OH should be able to recognise most of the state changes, and I could use standard state change rules.