Jruby has a specific feature that makes this easier.
rule 'Wandleuchte Kellerdiele' do
changed s_kg_diele_wandleuchte, to: ON, for: 180.seconds
only_if s_automatisierung_innenbeleuchtung
run { s_kg_diele_wandleuchte.off }
end
Specifically, the for
parameter will cause the rule to run only when the item has changed to the given state (in this case, ON
) for the given duration (180 seconds).
If the item turned off (changed state different to the parameter), the rule will not run and any internal timer is cancelled and cleared. When it changed again to ON, it will start a new timer from that moment.
I believe it does exactly what you want without having to write the boilerplate code. This scenario is quite common that the feature is built in to the scripting library.