openhab / openhab-jruby

A Helper Library for Writing openHAB Automations in Ruby
https://www.openhab.org/addons/automation/jrubyscripting/
Eclipse Public License 2.0
5 stars 5 forks source link

Add TimedCommand parameter `only_when_ensured` #290

Closed jimtng closed 2 months ago

jimtng commented 2 months ago

It's useful to know whether a timed command is currently active.

Example:

rule "Motion Detected" do
  received_command Motion_Sensor, command: ON
  run do
    # Do not activate timer if the light was already turned on manually
    next if FrontPorchLight.on? && !FrontPorchLight.timed_command?

    # The timer extends when more motion is detected
    FrontPorchLight.ensure.on for: 30.minutes
  end
end

Caveat: only works in the same file, but the whole timed command map lives in the file scope anyway.

ccutrer commented 2 months ago

Yes, I like this better. Can you update the PR title?

jimtng commented 2 months ago

PR title updated, commits tidied up