Rule: dimm lights after Xmin of no motion with expire binding

  • Platform information:
    • Hardware: RPi 4
    • OS: openhabian
    • openHAB version: openHAB 2.5.3-1
  • Issue of the topic: I am using a tradfri motion sensor connected to openhab via Phoscon/Congbee binding to dimm two dimmable lights depending on the hour of the day. I want the light to be dimmed to 0 (turned off) after 5min of no motion detected. I tried with expire binding but failed. Turning on the lights with a different intensity works pretty good for every specified timeframe. But it simply does not turn off. As I am relatively new to the art of writing rules by hand, I most probably forgot something or missed a step during the setup of the rule. May I kindly ask the help of the community?
  • Please post configurations (if applicable):
    • Items configuration related to the issue:
      This is the ‘flur.items’ file I created which includes the expire binding code:

Group:Dimmer Licht_im_Flur_5minOFF “Licht im Flur nach 5min off” { expire=“5m,command=Off” } // dimm Light to 0 after Xmin

  • Rules code related to the issue:
    This is the ‘flur.rules’ I created for this. Turning on the lights works. But does not expire as defined in ‘flur.items’

rule “Flurlicht nach Bewegung und Zeit”
when
Item BewegungsmelderFlurIKEAOfSweden_Presence changed from OFF
then
var Number hour = now.getHourOfDay
if ((hour >= 18) && (hour < 19 )) {
sendCommand(Licht_im_Flur_5minOFF, 30)
}
else if ((hour >= 19) && (hour < 20)) {
sendCommand(Licht_im_Flur_5minOFF, 50)
}
else if ((hour >= 20) && (hour < 22)) {
sendCommand(Licht_im_Flur_5minOFF, 80)
}
else if ((hour >= 22) && (hour < 0)) {
sendCommand(Licht_im_Flur_5minOFF, 40)
}
else if ((hour >= 0) && (hour < 6)) {
sendCommand(Licht_im_Flur_5minOFF, 20)
}
else if ((hour >= 6) && (hour < 8)) {
sendCommand(Licht_im_Flur_5minOFF, 60)
} // your logic here
end

Any hints, what I am doing wrong?

Mulitple errors I think:

Why this is a Group? I don’t think this will work that way.

Also

Should be

command=OFF

Also I can’t understand these commands. What is the “50” command here?

Thank you for your fast reply!

Why this is a Group? I don’t think this will work that way.

It is a group, because it holds the two dimmable lights. I read in the docs it is possible to define a dimmer group and so far the manual manipulation of the lights with sliders works. But I am open for suggestions. What approach would you chose to command the two lights?

command=OFF

I changed the ‘command=OFF’. Thanks.

What is the “50” command here?

The 50 is the Number in Percent for the dimmable lights. It works. For avery timeframe there is a different dimming of the lights.

Attempting to link expire binding to a Group type Item used to throw an error at xxx.items file load time.
Binding configuration of type 'expire' of item 'XXX' could not be parsed correctly.
Don’t you get something like that?

While the expire binding could never be fully functional with Group Items - you could never update a Group state for example - there would be sensible use cases for using expire in “command” mode wih a Group.
I think it simply does not work though, due to that validation error.

Thank you for your reply.

Actually I do not get any errors in log which contain ‘expire’.

Thanks for that clarification. So the best way to do what I want to achieve might be to talk to the items directly without a group?

Curious. I just copy-paste your Group config into my working OH 2.5.0
(after fixing quotemarks) -
openhab.log

2020-04-26 20:22:31.056 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'doors.items'
2020-04-26 20:22:31.069 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'expire' of item 'Licht_im_Flur_5minOFF' could not be parsed correctly.

Yes, it’s a pretty standard use for createTimer.

Thanks for the hint. I stumbled across the Design Patter. Motion Sensor Timer already and thought I understood. This obviously was wrong. So I might need some more clarifications:

The pattern uses two items (Switch Occupancy1 and MotionDetector1) both expire after 5 min. Why? Can I use the Dimmable Light item directly to be switched if any motion is detected? Like this?

flur.items:

Dimmer Licht_im_Flur_amBad5minOFF “Licht im Flur am Bad nach 5min off” { expire=“5m, 0” } // dimm Light to 0 after Xmin

Dimmer Licht_im_Flur_anKueche5minOFF “Licht im Flur an Küche nach 5min off” { expire=“5m, 0” } // dimm Light to 0 after Xmin

flur.rules:

rule “Flurlicht nach Bewegung und Zeit”
when
Item BewegungsmelderFlurIKEAOfSweden_Presence changed from OFF
then
var Number hour = now.getHourOfDay
if ((hour >= 11) && (hour < 19 )) {
sendCommand(Licht_im_Flur_amBad5minOFF, 30)
Thread::sleep(100)
sendCommand(Licht_im_Flur_anKueche5minOFF, 30)
}
else if ((hour >= 19) && (hour < 20)) {
sendCommand(Licht_im_Flur_amBad5minOFF, 50)
Thread::sleep(100)
sendCommand(Licht_im_Flur_anKueche5minOFF, 50)
}
else if ((hour >= 20) && (hour < 22)) {
sendCommand(Licht_im_Flur_amBad5minOFF, 80)
Thread::sleep(100)
sendCommand(Licht_im_Flur_anKueche5minOFF, 80)
}
else if ((hour >= 22) && (hour < 0)) {
sendCommand(Licht_im_Flur_amBad5minOFF, 40)
Thread::sleep(100)
sendCommand(Licht_im_Flur_anKueche5minOFF, 40)
}
else if ((hour >= 0) && (hour < 6)) {
sendCommand(Licht_im_Flur_amBad5minOFF, 20)
Thread::sleep(100)
sendCommand(Licht_im_Flur_anKueche5minOFF, 20)
}
else if ((hour >= 6) && (hour < 8)) {
sendCommand(Licht_im_Flur_amBad5minOFF, 60)
Thread::sleep(100)
sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
} // your logic here
end

This is my actual setup as I changed according to your hint regarding the problem with expire and groups.

Edit: I changed all command=0 to 0. Now the light turns on at specified dimming level, but does not turn off after the 5 min defined in flur.items.

Because Rick has given you two examples of using the expire binding as a timer.
The design pattern contains multiple different solutions.
“Simple Example: Expire Binding Version” is unrelated to “Simple Example: Timers Version” and they use quite different techniques.

Your original problem revolved around the combination of choosing to use expire and using it with a Group, where it does not play nice.
You might now choose instead to use expire with “ordinary” Items or use createTimer methods with a Group, it’s your choice.

I don’t see anything immediately wrong with your current use of expire.
There is a flaw in your rule -
else if ((hour >= 22) && (hour < 0)) {
those can never be true at the same time

Expire is a binding. You do have to install it. Maybe that’s why you don’t get an error message about use with Groups.

Thanks. I will go for the expire with ordinary items.

Thanks. I changed to ‘else if ((hour >= 22) && (hour < 24))’

Indeed a good point. But I had it installed in the first place. It just does not show up under configuration>Bindings but in the Addons section of PaperUI it is marked as installed. Nevertheless. Just to be sure, I reinstalled it.

I’ve just realised that is not what you want.
If you default the expire action, you get a state update. That’s not a command.
You should see this action in your events.log
If you want a command, you must tell it.
{ expire="5m, command=0"}

That’s what I had used in a try and error test yesterday. But it lead to the lights not even being turned on any longer (when also used in the flur.rules file). As I modified it back to just 0 it worked again for turning on the light. For testing reasons I now modified it back to ‘comman=0’ in the flur.items file.

After reinstalling the expire binding cat events.log | grep expire gives me:

2020-04-24 10:36:01.706 [thome.event.ExtensionEvent] - Extension ‘binding-expire1’ has been installed.
2020-04-27 11:42:56.548 [thome.event.ExtensionEvent] - Extension ‘binding-expire1’ has been uninstalled.
2020-04-27 11:57:08.170 [thome.event.ExtensionEvent] - Extension ‘binding-expire1’ has been installed.

I also detached the dimmer items I previously had created for the things (dimmalbe bulbs) from the things so they are only connected to the dimmer item with { expire=“5m, command=0”}.

Now it seems to work.

The binding is reinstalling every 5 minutes?
Sounds like the “addons” problem

Try this diagnostic Item in your items file

Switch testExpireBinding "testing" {expire="5m,command=rhubarb"}

When your xxx.items file loads, you should get this error in your openhab.log
If you do not, the binding is not active

2020-04-27 11:33:42.202 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'doors.items'
2020-04-27 11:33:42.214 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'expire' of item 'testExpireBinding' could not be parsed correctly.
org.eclipse.smarthome.model.item.BindingConfigParseException: The string 'rhubarb' does not represent a valid command for item testExpireBinding
1 Like

It works with the setup below in this post.

No. The first installation was, as I guessed, on 24th right before I started taking on the automation with the motion sensor. After you mentioned there might be a problem with the installation I uninstalled it (27th 11:42) and reinstalled it (27th 11:57).

It works now with this setup:

flur.items:

Dimmer Licht_im_Flur_amBad5minOFF “Licht im Flur am Bad nach 5min off” { expire=“1m, command=0”} // dimm Light to 0 after Xmin

Dimmer Licht_im_Flur_anKueche5minOFF “Licht im Flur an Küche nach 5min off” { expire=“1m, command=0” } // dimm Light to 0 after Xmin

flur.rules:

rule “Flurlicht nach Bewegung und Zeit”
when
Item BewegungsmelderFlurIKEAOfSweden_Presence changed from OFF
then
var Number hour = now.getHourOfDay
if ((hour >= 11) && (hour < 19 )) {
sendCommand(Licht_im_Flur_anKueche5minOFF, 30)
Thread::sleep(50)
sendCommand(Licht_im_Flur_amBad5minOFF, 30)
}
else if ((hour >= 19) && (hour < 20)) {
sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
Thread::sleep(50)
sendCommand(Licht_im_Flur_amBad5minOFF, 60)
}
else if ((hour >= 20) && (hour < 22)) {
sendCommand(Licht_im_Flur_anKueche5minOFF, 90)
Thread::sleep(50)
sendCommand(Licht_im_Flur_amBad5minOFF, 90)
}
else if ((hour >= 22) && (hour < 24)) {
sendCommand(Licht_im_Flur_anKueche5minOFF, 40)
Thread::sleep(50)
sendCommand(Licht_im_Flur_amBad5minOFF, 40)
}
else if ((hour >= 0) && (hour < 6)) {
sendCommand(Licht_im_Flur_anKueche5minOFF, 20)
Thread::sleep(50)
sendCommand(Licht_im_Flur_amBad5minOFF, 20)
}
else if ((hour >= 6) && (hour < 8)) {
sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
Thread::sleep(50)
sendCommand(Licht_im_Flur_amBad5minOFF, 60)
} // your logic here
end

Thank you for your support!