Add timeout on state change in a rule?

I’m running v. 3.3.0, got a state item that could be OPEN or CLOSED.
I need to warn if the state stays OPEN for more than a minute.

So far I’v created a rule:


configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Door_State
      state: OPEN
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      sink: enhancedjavasound
      text: CLOSE THE DOOOOOOOR 
    type: media.SayAction

But don’t know how to add the condition:

Start count for timeout when Door_State goes to OPEN
Switch TimeElapsed "Door opened since [%s]" { expire="1m" }
if TimeElapsed then run action

How could add this logic?
Thanks

There is no way around this except to create a Script Action. Plain UI rules can only do the simplest of “when this happens to that” type rules. As soon as you have complex conditions, timers, or more involved logic you must use a script action/condition.

However, the easiest and most comprehensive thing you can do is install the Open Reminder [3.3.0;3.4.0) rule template and instantiate and configure a rule as described there.

All you’d have to do is add a rule with your Say Action and the Open Door rule will handle the timers and all the other complex stuff.

If you want to code this yourself, I recommend getting started with Blockly. See the rules section of the Getting Started Tutorial and the Blockly reference docs for details.

Thank you @rlkoshak for the reply, I will look at this, may I ask if the npm install … can be run inside docker container?

No, npm is not shipped as part of the container. But you can install it. But your $OH_CONF folder should be in a volume outside the container anyway. So just run NPM from where ever your volume is.

I’ve submitted a PR for a new feature for jruby helper libraries to do this.

It’s not yet merged / updated to the gem (look for version 5.0.0.rc11 or newer), but once it has, you could:

  1. Change your trigger to ItemStateChangeTrigger and remove the “state: OPEN” condition, so that it triggers on any state change for the Door_State item.
configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Door_State
    type: core.ItemStateChangeTrigger
  1. Install jruby automation + configure it with the helper library. The easy way to do this is by installing the jruby automation from the Marketplace.
  2. You would change your action to run a script, select Ruby script, and have this in your script:
debounce_for(1.minute) do
  next unless event.state.open?
  Voice.say("CLOSE THE DOOOOOOOR", sink: "enhancedjavasound")
end

Voila!

Here’s the full yaml for UI based rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Door_State
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/x-ruby
      script: |-
        debounce_for(1.minute) do
          next unless event.state.open?
          Voice.say("CLOSE THE DOOOOOOOR", sink: "enhancedjavasound")
        end
    type: script.ScriptAction

Note that this is already supported in file-based rules in the current version of the helper library. The file based rule would look like this:

rule "Open door alert" do
  changed Door_State, to: OPEN, for: 1.minute
  run { Voice.say("CLOSE THE DOOOOOOOOR", sink: "enhancedjavasound") }
end

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.