Rules - time switch for a lightbulp

Hi everyone,

I’m trying to implement a time switch:

triggers:
  - id: "1"
    configuration:
      itemName: EingangsturKontaktBitronVideo90201021A_ContactPortal1
      state: OPEN
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: BuroWurfelLEDVANCECLA60RGBWZ3_Color
      command: ON
    type: core.ItemCommandAction
  - inputs: {}
    id: "3"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: BuroWurfelLEDVANCECLA60RGBWZ3_Color { expire="1m,command=OFF" }
    type: script.ScriptAction

Unfortunately it doesn’t work. Can you tell me why?

I’ve no idea what you expected that to do, but if you want expire to act on an Item then you must configure it on the Item, not within a rule.

Note that will then have permanent effect, the Item will always turn OFF a while after it was turned ON, by anyone.
It’s usually best to exlpain what you are trying to achieve.

Hi @rossko57, thank you for your reply. I want the lamp in the hallway to go on for a minute when the front door is opened. This rule should be supplemented by the condition that this should only happen when the sun has set.

In the first step, I wanted to try the first part of the rule: if the door is open, then switch on the lamp for a minute.

The unaddressed part here will make a difference to how to do this.
What would you like to happen if the door is closed before a minute? (No change, perhaps)
What would you like to happen if the door is still open after a minute? (Light stays on, perhaps)
What would you like to happen if someone turns the light on by hand? (Light stays on without timing, perhaps)

Don’t be afraid to split your logic up,maybe use more than one rule if its easier.
“When the door is opened, I would like to turn the light on, but only if it is dark.”
“When the door is closed, and the light is ON, I would like to turn it OFF in a minute’s time.”

Okay, I haven’t thought so much about it yet. I wanted to learn from this simple example how to start a timer. But I think:

  • door closes before a minute: no change
  • door still open after a minute: lights stay on
  • light switched manually: no timer

I don’t know where to enter anything to trigger the timer. With that I would be a long way ahead and very happy. :slight_smile:

Of course now we have to think of something else … :wink:
Stay on forever,or only untilthe door eventually closes?

Solving these decisions by rule structure was what I was hinting at.
Door changes to open -
do this (turn on light if it is dark)
Door changes to closed -
do that (start off-timer, if needed)

That’ll be something to do in the script section, exactly what depends on the script language that you choose.

This might help for DSL

Start at Actions | openHAB. If you have trouble come back with what you’ve tried and we can help. Eventually you’ll need to deal with all the conditions rossko57 mentions but we can start with a simple Timer.

Unfortunately though to deal with all of the conditions it’s going to mean that Rules DSL won’t work here unless you move to .rules files because you’ll have to save the Timer from one run of the rule to the next in a “global” variable which are not available in UI rules. So it might be worth looking into how to write rules in .ruled files (start at Rules | openHAB) or look into using JavaScript as the language in UI defined rules. Unfortunately, at this point there isn’t as much documentation nor examples for JavaScript on the forum yet.

Here is an example of creating a Timer in JavaScript UI Script Action.

// Pull in the logger
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Expamples");
logger.info("About to test createTimer");
// Pull in ScriptExecution where createTimer lives
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");

// Define the function that gets called when the timer goes off
var runme = function(){ logger.info("Timer expired!"); }

// Pull in ZonedDateTime to set when the Timer should go off
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

// Get now
var now = ZonedDateTime.now();

// Create the Timer
var timer = SE.createTimer(now.plusSeconds(1), runme);

So far, so good but each time that code runs the timer gets recreated and the old one becomes orphaned. So the code needs to be modified a bit.

// Pull in the logger
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Expamples");
logger.info("About to test createTimer");
// Pull in ScriptExecution where createTimer lives
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");

// Define the function that gets called when the timer goes off
var runme = function(){ 
    logger.info("Timer expired!"); 
    this.timer = null;
}

// Pull in ZonedDateTime to set when the Timer should go off
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

// Get now
var now = ZonedDateTime.now();

// Initialize the Timer variable if necessary
this.timer = (this.timer === undefined) ? null : this.timer;

// Reschedule the timer if it is already set
if(this.timer !== null) {
    this.timer.reschedule(now.plusSeconds(1));
}
// Create a new timer if one is not already set
else {
    var timer = SE.createTimer(now.plusSeconds(1), runme);
}

A huge THX to both of you :slight_smile: Was I ment was something like this …

triggers:
  - id: "1"
    configuration:
      itemName: EingangsturKontaktBitronVideo90201021A_ContactPortal1
      state: OPEN
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: FlurDeckeLEDVANCECLA60RGBWZ3_Color
      command: ON
    type: core.ItemCommandAction
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript
      script: java.lang.Thread.sleep(15000);
    type: script.ScriptAction
  - inputs: {}
    id: "4"
    configuration:
      itemName: FlurDeckeLEDVANCECLA60RGBWZ3_Color
      command: OFF
    type: core.ItemCommandAction

To my surprise, the lamp is switched on for 15 seconds, then flickers and then lights up another 15. So in total 30 - why?

How often does your Item get updates? That’s not the same as a change. Many device technologies periodically update, even if there is no change.

Sleep is a bit rubbish for this use, anything could happen while it is running.

If a new trigger comes along while the rule is sleeping, it will get queued up. Then run the rule again, when it has finished what it was doing before.

Can I see the frequency of updates in OH3?

updates-to-same-value don’t get logged by default.

You can make a little logging rule to make your own logs for each update.

In this case, who cares. Don’t you want your rule to run when the door state changes?
This is a hint about choosing from the different available rule triggers carefully.

You’re not going to trust us that sleep is a rubbish way to go about a timer job than? :wink:

Alternativley to creating a rule, you can see Item updates in the Developer Sidebar. Hit ALT-SHIFT-D and start the event stream and you will see all the events occurring on your system, including update events. You can filter the events to only see those related to your motion sensor Item. ![image|152x500]

That a stream filtered on *Humidity. The ItemStateEvents are updates. ItemStateChangedEvents are changes. There is also a Group event on there. Not shown are commands. This stream also includes Thing Events and Rule events so it can be quite handy and it’s more complete than watching the logs.

Edit: Hmmmm, wonder what happened to my image. Posting the image again.

1 Like

I believe you, no question. In this case, I wanted a simple, quick fix. It doesn’t have to be perfect because I’m just beginning and have a lot to learn. My code can get better over time …:wink:

THX. I filtered the stream

grafik

but other things keep coming in

Did I something wrong?

Pay attention to the the “wildcards accepted”. Try “OnOff”. It does a simple string match against the stream I think. But also, I don’t know if it filters on the type or if it just filters on the Item name and event type (e.g. ItemStateEvent will return all the update events). Also, I think the searches are OR, not AND, meaning if you have two filters it will show the event if either match, not only if both match.

Thank you for your help. No matter what I enter, everything will be streamed. I will stop for today and wish you and everyone else a nice evening, or whatever time of day it may be with you.

Hi, I found a way to filter the logs. I called up the address http://192.168.178.43:9001 and then entered the filter: http://192.168.178.43:9001/?filter=Flur

The output was as follows:

2021-03-18 11:02:49.469 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' received command ON

2021-03-18 11:02:49.471 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' predicted to become ON

2021-03-18 11:02:49.474 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' changed from 0,0,0 to 0,0,100

2021-03-18 11:03:19.470 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' received command OFF

2021-03-18 11:03:19.475 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' received command ON

2021-03-18 11:03:19.477 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' predicted to become OFF

2021-03-18 11:03:19.481 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' predicted to become ON

2021-03-18 11:03:19.487 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' changed from 0,0,100 to 0,0,0

2021-03-18 11:03:19.488 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' changed from 0,0,0 to 0,0,100

2021-03-18 11:03:19.597 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' changed from 0,0,100 to 0,0,0

2021-03-18 11:03:49.470 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' received command OFF

2021-03-18 11:03:49.473 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'FlurDeckeLEDVANCECLA60RGBWZ3_Color' predicted to become OFF

I’ve lost what your purpose was here.

Your rule is/was triggered by update to door Item.

That’s right, a door contact triggers the lighting in the hallway to be switched on. These lines come from the lightbulb in the hallway.

Can you tell from the log entries why the lightbulb shines twice as long as it should?

Only so far as “yes, because something keeps turning it ON”

We can guess that it’s your rule doing that.
So the next question should be “why does my rule run multiple times?” and logically that should lead to looking at what triggers the rule.