[OH3] Rule: switch off after sometime?

Dear colleagues :slight_smile:

I’m newbie and recently I install openHAB on the platform:

  • Hardware:Intel® Celeron® CPU N3150 @ 1.60GHz
  • OS: Windows 10
  • Java: Zulu11.45.27-ca-jdk11.0.10-win_x64
  • openHAB 3.1.0.M1

I have some smart equipment and now I’v trying to create the rule for power outlet TP-Link HS110.

The description rule is when power outlet (heated towel rail) switches on, after some time period (for example 30 minutes) it switches off. Maybe I don’t understand the logic of time conditions but I tried different variants and no results. I looked through the documentation, some posts in community and found nothing as well. Below you can see info about rule.

Design mode

Current code (without time condition)

triggers:
  - id: "3"
    configuration:
      itemName: HeatedTowelRail_Switch
      state: ON
      previousState: OFF
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: HeatedTowelRail_Switch
      command: OFF
    type: core.ItemCommandAction

Could you please advise mу how can I manage with it?

BR, Aleksei.

You could use the Expire metadata property for this, and get rid of a rule completely, I think.

For your Item:

  • Click Add Metadata
  • Click Expiration Timer

Fill in the details and voila!

Hi Aleksei,

you don’t even need a rule for this. There is a feature called expiration timer that you can add to your item.
Go to settings - items and find your switch item. On the item page, click add Metadata and select Expire. There you can give a time and a command that is sent after the time expired (in your case: OFF).

Edit: I was too slow!

Definitely use what T and Bob suggest and use the Expiration metadata to do this.

But I want to answer your original question too.

Rules are triggered by events. The “When” part of the rule is where you define the events that will cause the Rule to run. Events are usually a time, system event, or Item event (e.g. an Item changes state).

If you also want to take into consideration the state of an Item (e.g. MyTempSensor > 75 so the rule only runs if the temp us above 75 degrees F) you’d put that in the “But only if” section of the rule.

Finally, what you want to have happen when the rule runs goes in the Then section of the rule. In your case the what you want to have happen is that after a certain amount of time the Switch is turned OFF. So that has to be implemented in that Then section of the rule.

Now that you know where you need to implement this, the short answer is you can’t do this without writing some code in a Script Action. What you will need to do is add a Script Action under the Then section of the Rule. In that code you’ll send the command to the Switch. Then you have a couple of options, a good one and an easy one.

The easy option is to just add a Thread.sleep to make the rule pause for the given amount of time before sending the OFF command later. If using Rules DSL that would look like

Thread::sleep(30*60*1000); // sleep for 30 minutes
HeatedTowelRail_Switch.sendCommand(OFF)

In OH 2 this would be a really bad idea. In OH 3, at least for now, this is a less bad idea but still not a good idea. 30 minutes is a really long time to tie up a rule. If that Switch Turns OFF on and then ON again, another trigger for this rule will be queued up and it’ll have to wait the the 30 minutes before it gets to run again and then that one will have to wait 30 minutes.

But that will work, but I’m sure you can tell I don’t recommend that approach.

The good option is to create a Timer. This is a way to schedule a little bit of code to run at a later time. In that case, in Rules DSL the Script Action would look something like:

createTimer(now.plusMinutes(30), [ | HeatedTowelRail_Switch.sendCommand(OFF) ]

T & BobMiles, thanks a lot ! Especially I’m gratefull to Rich for detailed description of creating Timer :+1:. It’s really useful. Honestly speaking, this is a part of the rule because I’m going to buy humidity sensor and use it as event for this rule.

BR, Aleksei.