Automatic Light Off Rule


(Ken Mickeletto) #1

Hi All,

Very new to OpenHAB but very cool, and extremely complex.

Does anyone have a rule or can post some good starters for creating a rule that does the following?

If the light switch is turned on (at switch) start a 10 minute timer and then turn it off.
If the light switch is double tapped, start a 120 minute timer and then turn it off.

Kids leave garage lights on all of the time and it is a real sore spot in our house. I’m hoping this will help, plus it would be a great starting point for me to get my head wrapped around the rules.

I am using the Insteon PLM and Insteon 2477D dimmer switch if that is of any matter.

Thanks so much!


Rules Not working after
Rules Not working after
(Udo Hartmann) #2

If depends on how to trigger the double tap. If your switch does only send an ON when pressed (and maybe an OFF when released) it would work like this:
.items:

Switch MySwitch "My Switch" {...} //binding for switch only
Switch MyLamp "My Lamp" {...} //binding for lamp only

.rule:

var Timer MyTimer1 = null
var Timer MyTimer2 = null

rule "autoswitch lamp"
when
    Item MySwitch received command ON
then
    MyLamp.sendCommand(ON)
    if (MyTimer1 == null) {
        MyTimer1 = createTimer(now.plusMillis(500), [| //double tap has to be within 0.5 seconds
            MyTimer2.cancel
            MyTimer2 = createTimer(now.plusMinutes(10), [|
                MyLamp.sendCommand(OFF)
                MyTimer2 = null
                ]
            )
            ] // corrected the code here
        )
    }
    else
        MyTimer1.cancel
        MyTimer1 = null
        MyTimer2.cancel
        MyTimer2 = createTimer(now.plusHours(2), [|
            MyLamp.sendCommand(OFF)
            MyTimer2 = null
            ]
        )
end

You could even set a level, so don’t sendCommand(ON) but e.g. sendCommand(75).

At home I have an example which is even more complex, you can trigger one-tap, double-tap and tripple-tap, but for the one-tap, you can switch in serial style through 4 scenes (first tap: light 1 is on, second tap: light 2 is on, third tap: both lights are on, fourth tap: all lights out.)


(Ken Mickeletto) #3

Thanks Udo,

I applied the rule but it doesn’t seem to want to engage for me. Here is
my copy of the rule in case I have messed it up somehow. No matter if I
double click the switch or single click, it always just stays on forever.
var Timer MyTimer1 = null
var Timer MyTimer2 = null

rule "livingroomlighttest"
when
Item livingroomlight received command ON
then
livingroomlight.sendCommand(ON)
if (MyTimer1 == null) {
MyTimer1 = createTimer(now.plusMillis(500), [| //double tap has to
be within 0.5 seconds
MyTimer2.cancel
MyTimer2 = createTimer(now.plusMinutes(120), [|
livingroomlight.sendCommand(OFF)
MyTimer2 = null
]
)
)
}
else
MyTimer1.cancel
MyTimer1 = null
MyTimer2.cancel
MyTimer2 = createTimer(now.plusMinutes(10), [|
livingroomlight.sendCommand(OFF)
MyTimer2 = null
]
)
end

Also, here is a copy of the debug logs where the switch was turned on.
19:49:20.699 [INFO ] [.o.b.i.i.device.MessageHandler:361 ] -
LightOnSwitchHandler: device 30.AD.66 was switched on FAST.
19:49:20.700 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:FastOnOff publishing: ON
19:49:20.700 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:00.00.01|messageFlags:0xCF=ALL_LINK_BROADCAST:3:3|command1:0x12|command2:0x00|
19:49:20.700 [DEBUG] [b.i.i.device.MessageDispatcher:70 ] - all link
message is no duplicate: true/true
19:49:20.701 [DEBUG] [b.i.i.device.MessageDispatcher:72 ] -
30.AD.66:ManualChange->DefaultMsgHandler cmd1:0x12 group
1/-1:IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:00.00.01|messageFlags:0xCF=ALL_LINK_BROADCAST:3:3|command1:0x12|command2:0x00|
19:49:20.701 [DEBUG] [.o.b.i.i.device.MessageHandler:302 ] -
DefaultMsgHandler drop unimpl message 0x12:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:00.00.01|messageFlags:0xCF=ALL_LINK_BROADCAST:3:3|command1:0x12|command2:0x00|
19:49:20.701 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:00.00.01|messageFlags:0xCF=ALL_LINK_BROADCAST:3:3|command1:0x12|command2:0x00|
19:49:20.701 [DEBUG] [b.i.i.device.MessageDispatcher:70 ] - all link
message is no duplicate: true/true
19:49:20.702 [DEBUG] [b.i.i.device.MessageDispatcher:72 ] -
30.AD.66:GenericDimmer->LightOnDimmerHandler cmd1:0x12 group
1/1:IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:00.00.01|messageFlags:0xCF=ALL_LINK_BROADCAST:3:3|command1:0x12|command2:0x00|
19:49:20.702 [INFO ] [.o.b.i.i.device.MessageHandler:328 ] -
LightOnDimmerHandler: device 30.AD.66 was turned on FAST. Sending poll
request to get actual level
19:49:20.702 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:GenericDimmer publishing: 100
19:49:20.703 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:GenericLastTime publishing: 2016-07-26T19:49:20
19:49:20.950 [DEBUG] [.o.b.i.InsteonPLMActiveBinding:581 ] - got msg:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:34.79.1B|messageFlags:0x40=ALL_LINK_CLEANUP:0:0|command1:0x12|command2:0x01|
19:49:20.951 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:34.79.1B|messageFlags:0x40=ALL_LINK_CLEANUP:0:0|command1:0x12|command2:0x01|
19:49:20.951 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:34.79.1B|messageFlags:0x40=ALL_LINK_CLEANUP:0:0|command1:0x12|command2:0x01|
19:49:20.951 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:34.79.1B|messageFlags:0x40=ALL_LINK_CLEANUP:0:0|command1:0x12|command2:0x01|
19:49:20.951 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:GenericLastTime publishing: 2016-07-26T19:49:20
19:49:21.100 [DEBUG] [.o.b.i.InsteonPLMActiveBinding:581 ] - got msg:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:12.01.01|messageFlags:0xCB=ALL_LINK_BROADCAST:3:2|command1:0x06|command2:0x00|
19:49:21.101 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:12.01.01|messageFlags:0xCB=ALL_LINK_BROADCAST:3:2|command1:0x06|command2:0x00|
19:49:21.101 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:12.01.01|messageFlags:0xCB=ALL_LINK_BROADCAST:3:2|command1:0x06|command2:0x00|
19:49:21.101 [DEBUG] [b.i.i.device.MessageDispatcher:68 ] - all link
message:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:12.01.01|messageFlags:0xCB=ALL_LINK_BROADCAST:3:2|command1:0x06|command2:0x00|
19:49:21.102 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:GenericLastTime publishing: 2016-07-26T19:49:21
19:49:21.703 [DEBUG] [o.o.b.i.i.device.InsteonDevice:379 ] - qe taken off
direct: GenericDimmer(1:3:8)
OUT:Cmd:0x62|toAddress:30.AD.66|messageFlags:0x0F=DIRECT:3:3|command1:0x19|command2:0x00|
19:49:21.703 [DEBUG] [o.o.b.i.i.device.InsteonDevice:399 ] - next request
queue processed in 2000 msec, quiettime = 2000
19:49:21.703 [DEBUG] [o.o.b.i.internal.driver.Port :382 ] - writing
(500):
OUT:Cmd:0x62|toAddress:30.AD.66|messageFlags:0x0F=DIRECT:3:3|command1:0x19|command2:0x00|
19:49:22.015 [DEBUG] [.o.b.i.InsteonPLMActiveBinding:581 ] - got msg:
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:34.79.1B|messageFlags:0x2F=ACK_OF_DIRECT:3:3|command1:0x02|command2:0xFF|
19:49:22.015 [DEBUG] [b.i.i.device.MessageDispatcher:134 ] -
30.AD.66:GenericDimmer DIRECT_ACK: q:QUERY_PENDING cmd: 80
19:49:22.016 [DEBUG] [b.i.i.device.MessageDispatcher:139 ] - changing key
to 0x19 for msg
IN:Cmd:0x50|fromAddress:30.AD.66|toAddress:34.79.1B|messageFlags:0x2F=ACK_OF_DIRECT:3:3|command1:0x02|command2:0xFF|
19:49:22.016 [INFO ] [.o.b.i.i.device.MessageHandler:537 ] -
DimmerRequestReplyHandler: set device 30.AD.66 to level 100
19:49:22.016 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:GenericDimmer publishing: 100
19:49:22.016 [DEBUG] [b.i.i.device.MessageDispatcher:159 ] - defdisp:
30.AD.66:GenericDimmer set status to: QUERY_ANSWERED
19:49:22.017 [DEBUG] [o.o.b.i.i.device.DeviceFeature:264 ] -
30.AD.66:GenericLastTime publishing: 2016-07-26T19:49:22
19:49:23.703 [DEBUG] [i.i.device.RequestQueueManager:132 ] - device queue
for 30.AD.66 is empty!


(Udo Hartmann) #4

Please, if quoting code or logs, use code fences (use three backticks in a row before and after code ```)

There is a typo in my posting, and it is in yours, too :slight_smile:

var Timer MyTimer1 = null
var Timer MyTimer2 = null

rule "livingroomlighttest"
when
    Item livingroomlight received command ON
then
    livingroomlight.sendCommand(ON)
    if (MyTimer1 == null) {
        MyTimer1 = createTimer(now.plusMillis(500), [| //double tap has to be within 0.5 seconds
            MyTimer2.cancel
            MyTimer2 = createTimer(now.plusMinutes(120), [|
                livingroomlight.sendCommand(OFF)
                MyTimer2 = null
                ]
            )
            ] //this bracket was missing in the original posting
        )
    }
    else
        MyTimer1.cancel
        MyTimer1 = null
        MyTimer2.cancel
        MyTimer2 = createTimer(now.plusMinutes(10), [|
            livingroomlight.sendCommand(OFF)
            MyTimer2 = null
            ]
        )
end