Conditional Switch Turn on - OH3


Openhab 3 on RPI4 latest milestone.
I am trying to have my entrance Hall light turn ON for 2 minutes if

  1. it is dark outside
  2. The entrance hall light is off
  3. the front door opens (indicated by a alarm zone trigger)

my best guess is


3 - zone 17 goes from closed to open


Nightime Switch is on


Turn light on for 2 minutes

I am guessing that the best / more efficient way is to get the astro binding, use it to trigger a virtual switch on and off based on it’s event triggers (dawn and dusk) for my condition. (this has issues on reboot i guess but i am ok with that)

What i dont know how to do is turn on light for 2 minutes within the oh3 rules set.

Can anyone point me in an OH3 level direction?

Expire metadata.

1 Like

I believe you can use something like the following to turn the light off after two minutes.

createTimer(now.plusSeconds(120)) [ | sendCommand(HallLight,OFF) ]

I use three rules for the daytime/nighttime switch

// Set Daytime switch to ON at sunrise and OFF at sunset
rule SetDaytimeToDay
when Channel "astro:sun:local:rise#event" triggered START 
then Daytime.postUpdate(ON)

rule SetDaytimeToNight
when Channel "astro:sun:local:set#event" triggered END 
then Daytime.postUpdate(OFF)

rule SystemStartUp
when System started  
    var vSunrise = (Sunrise.state as DateTimeType).getZonedDateTime()
	var vSunset  = (Sunset.state as DateTimeType).getZonedDateTime()
	switch now {
	  case now.isAfter(vSunrise) && now.isBefore(vSunset): Daytime.postUpdate(ON)
	  default: Daytime.postUpdate(OFF)
1 Like

Create a rule that is triggered by the front door opening and turns on the hall light.

If the local sun phase name is NOT equal to DAYLIGHT (that will cover you from sunrise to sunset)

Create an expire entry for the hall light that turns off after two minutes.


@ubeaut , thanks for the alternative full answer - much appreciated. I still need the nighttime switch to trigger other things also (and for seeing the time it triggers so will keep that also).

Can you explain - Create an expire entry for the hall light that turns off after two minutes.? is it a rule specific entry? or will it always affect the hallway light (not desired) ? where do I place it/which metadata entry? @11194 thanks for mentioning also!

@ubeaut what if i want to adjust the Daylight time? can i offset it? also do i need to add the channel to reference it? (i assume yes)

@JimH thanks for the timer level info also - where would i put that? in a script called by the rule? isnt the expire metadata better?

If you want to offset the time see here:

The metadata expire is on the item. It is not a rule. Look at the picture I sent earlier. Click on the item and you will see metadata.

If you want to use the hall light for more than 2 minutes then you would have to create a virtual switch and put the metadata on that and then have 2 more rules. One to turn on the hall light when the virtual switch was on and one to turn the hall light off when the virtual switch goes off.

1 Like

@ubeaut Thanks! maybe coding a timer into the rule or a script would avoid the need to have the virtual item? - i ask because i was to also do something similar with the electric hot water heater - although expiry would be smarter here indeed.

Yes you can put a timer in the rule if you wanted to but if you are not a coder and just want to pint and click rules then you can do it with virtual switch or the physical switch.

Choice is yours to make…You don’t have to do it a certain way of you don’t want to.

I use expire the most and only have one timer in a rule. Both ways work well.

1 Like

You would put the timer command in the rule just after it sends the command to turn the light on.

I don’t know if the timer or the GUI based expire is better, someone with knowledge of Openhab internals would need to speak to that. I just prefer rules files vs the GUI.

1 Like

I’ve found virtual item very convenient for light switching based on motion sensors. When motion sensor detects a motion in controlled room, a rule is checking if it`s not daytime and main room light is not on already with rule condition. Then rule refreshes lightlevel sensor to check if there is some other light source so main light is not needed. After that rule triggers on main light and a proxy timer item with expire metadata set to 2 minutes.

  var light = WallmountFamilyRoom_Lightlevel.state as Number
if(light < 1) {

Every motion event after that checks if timer item is still on, and sends

postUpdate(FamilyRoomLightTimer, ON)

to reset expire timer and to restart 2 minutes countdown.
And a very simple UI rule turns off main light when timer item expires.
This is working like a charm for kitchen and other zones where you do move all the time you spend there - we are not using conditional switches or voice commands anymore.

1 Like

I have Phillips Hue sensors for light readings:

The rule is if motion is detected and the lux is less than 4 and the Lamp in the lounge room is off then turn on the small lamp which has the expire set on it.

I have the check fro the lounge light is off because it is dimmable and can get below 4 lux.

1 Like

I should mention that I tried expire for my hotwater heater, wanted an auto-off after 35mins but it seems expire is limited to small time amounts - i read that it resets the expire timer after any device update so 2 mins is prob ok but 35 gets reset time and time again probably due to traffic updates.

so timer in a script is more applicable i think!

(more info from here - Expire timer does not always work)

Just use a proxy item for expire timer if somehow you update your main item during expire countdown. And a simple rule - when timer item switches off - switch off main item. And btw, i have 2 hours expire timer on watering valve working every day without issues, so expire timer itself is not an issue.

can you show me how you set it without a proxy item pls? maybe i did something wrong

Just a switch item, not linked to any thing/channel, with expire metadata on him.

what action do you choose to use? update state or send command?

send command OFF

1 Like

Quick question, does the sunphasename variable relate to a channel you have linked? i.e. do you create a channel (which one) to be able to rever to daylight?

I use a variable just for quick transform from “234 lumens” to “234” so i could compare this variable with needed number. And daylight check is made in UI rule condition - there is no need of variable, i just check that TimeOfDay item != DAY. This item is made with the use of Rich Koshak’s design pattern TimeOfDay

Look at his:

It uses virtual switch because the real item gets updated and resets the timer.