Rule to detect relay state but ignore flashing period

I have interesting case which I can’t resolve on the HW relay side (sonoff) and was thinking of a rule in OH2 to address it, but it’s not trivial IMHO.
Situation description: I wanted to be able to detect state of the entry gate based on its controller (not Contact) as it’s seems the most elegant and non-intrusive solution. My gate controller has output for warning lamp which I nicely integrated with Sonoff (Tasmota) GPIO. The warning lamp behavior which I can’t change is as follows:

  • closed gate gives OFF
  • opening gate gives ON
  • open gated gives ON
  • closing gate gives blinking between ON/OFF with interval of about 0.5s (then closing itself takes let’s say ~20s)
    Now, I want to detect when the gate is opened, it’s not trivial as during closing it flips between ON and OFF about 40 times, therefore rule won’t be reliable and definitely I can’t think of any notification there.
    I was thinking of having 2 items - relay_state and gate_state. Then some kind of rule which would be checking time between updates of a relay_state item and if less than let’s say 1s interval not to update the gate_state item.
    But I find it problematic not only because I think time formats and mapping between Joda and DateTime I still don’t understand well and use it intuitively rather than with good understanding :smiley: but also because last state from closing to closed happens in this short interval and I won’t register it.
    Graphically it looks as follows:


any ideas?

I’d approach it as a variation of “motion timer”, using your two Items.

When relay state changes to ON, set gate OPEN.
Also when relay changes to ON, cancel any off-timer.

When relay changes to OFF, start off-timer of a few “flashes” duration, nothing else.
If off-timer runs to completion, set gate CLOSED.

During the flashing phase, the timer keeps getting cancelled, and does not complete until flashing stops.

@rossko57 thanks for the idea, this may work well for very first starting and final closing positions but if I want to send a notification (or trigger some action) based on OPEN/CLOSED state of the gate, this won’t work as this will change many times during flashing period and therefore trigger my actions multiple times.

No it won’t. Notify on gate change.

More explanation -
Every time the relay “flashes” ON, gate is set to OPEN. Often it will already be open, and cannot get more open. There’s no change.

Every time the relay “flashes” ON, it cancels any running timer.
The only way the gate can get set CLOSED if if the timer is allowed to run to its end. It never can, while the relay is flashing.
Only after all the flashing stops, and the relay remains OFF, will the timer be allowed to finish. Only then will the gate be change to CLOSED.

Right! I missed this nuance that CLOSED won’t be set in the meantime. Let me try it and provide feedback. Thanks

You can refine the basic idea. In the relay-ON then set gate-OPEN code, you would probably not pointlessly update the gate item if it is already OPEN.

You might need to think about what happens if the gate closes on a passer-by and jams. How does it behave, reverses open maybe? You might need to add into the timer code to check if the relay is still OFF at completion time, before setting CLOSED. EDIT - thinking on that, no you don’t need that, if relay changed to ON it kills the timer anyway.

jams and passes-by are simple. In my case it’s configured to reverse the direction (open again) and in that case ON / OPEN will be set.If I can address ‘flashes’ when closing then I think I’m done

@rossko57 thanks for the hint, it works as expected!