Rule with a time delay?

Hello,

is it possible to create a rule with a delay and a second if check in it?

What i wanted to create is a rule that turn on a alarm switch when nobody is at home.
Rule should be clear:

rule "Alarmanlage automatisch"
when
	Item person_1 changed or
	Item person_2 changed
then
	if ((person_1.state == OFF) && (person_2.state == OFF)) {  // i know it could checked by an presence group
	proxy_alarm.sendCommand(ON) }
	else
		proxy_alarm.sendCommand(OFF)
end

The problem i got is my presence detection. I am using network binding. But from time to time there is a really short offline to my iphones presence and a second later it switches back to online.
And yes, i am using arping and tested a lot with timeout and retries. Also chmod u+s is done to arping.
I am happy with network binding and it works really satisfactory to me, that really short offline switch is negligible for me (it’s really only 1 or 2 times a day) , but its enough to active the alarmsystem and everytime just in this short moment i am able to pass a motion detector :frowning:

So, is there an option to create a rule with a short timer (30 seconds or so) with a check behind if both items really are offline?

Or do i have to create 2 rules???

rule "1"
when
	Item person_1 changed or
	Item person_2 changed
then
	createTimer(now.plusSeconds(30)) [ check_state.sendCommand(ON) ]
end


rule "2"
when Item check_state received command ON
then   if ((person_1.state == OFF) && (person_2.state == OFF)) { 
    	  proxy_alarm.sendCommand(ON) }
    	else
    		proxy_alarm.sendCommand(OFF)
end

make a timer that checks after set time if the devices are still offline.

This post should help

Edited my post while you were answering :wink:

Is this possible?

when
	Item person_1 changed or
	Item person_2 changed
then
        createTimer(now.plusSeconds(30)) [
                    if ((person_1.state == OFF) && (person_2.state == OFF)) { 
    	            proxy_alarm.sendCommand(ON) }
    	            else
    		               proxy_alarm.sendCommand(OFF) ]
end

that should work i think

Got it…

Rule posted before was not the best idea, because the 30 seconds timer had also impact on the deactivating alarm.
Thanks anyway

Now i use this rule and everything works fine.

rule "Alarmanlage automatisch"

when
		Item person_1 changed or Item person_2 changed
then	if ((person_1.state == ON) || (person_2.state == ON)) {
				proxy_alarm.sendCommand(OFF) }
		if ((person_1.state == OFF) && (person_2.state == OFF)) {
			createTimer(now.plusSeconds(30)) [
                    if ((person_1.state == OFF) && (person_2.state == OFF)) { 
    	            proxy_alarm.sendCommand(ON) }] }
end
4 Likes

Hi guys,

Not sure what is wrong with this one but let me know if there is anything that I’d have to add or update to it to make it work,

Rule
when
Item gPhone changed from ON to OFF
then
createTimer(now.plusSeconds(30))
[
Mode.sendCommand(ECO) ]
end

In the above, the Mode is the Item name of my thermostat and I am sending it to go ECO which works fine in the UI but not sure If is working correctly.
Let me know if there is anything wrong with the code or what would be the update required on it.
The rule works fine in the PaperUI but obviously, I can’t add the Time Delay from there. Any help will be appreciated.

ECO isn’t a recognized openHAB command. Maybe Mode is a Sring type Item, and “ECO” is wanted?

I have tried with off as well. Anyways none of my rules are working. I think something isn’t right but though one quick question, do we need to have items defined in items file if we want to use them in rules from command line. All of my items were created from the PaperUI, this might be a problem as well.

Okay. The best we can offer on that is - something is wrong.
Look in openhab.log and see what it tells you, maybe share that here if isn’t clear.

No, not at all. Whether your Items are created by PaperUI or xxx.items files makes no difference at all, they are all just Items when openHAB is running.

You do have to be careful about not having Items with same name from different sources though - only one would work.

Something wasn’t right and after the restart the remaining rules are working fine and I made some progress on the above as well but I think I may need some help in making it perfect.

The Use Case is that I’d like to check if my Phone is off then wait for 10 minutes and recheck if the Phone is still OFF then send a command to Turn the thermostat to ECO mode.

Right now, the wait is working fine but the inner condition isn’t working and this is probably something to do with syntax I think

The following works absolutely fine.
rule “Nest”
when
Item gPhone changed from ON to OFF
then
createTimer(now.plusSeconds(600), [
sendCommand(Thermostat_Mode,‘ECO’) ])
end

What I need is this and it is not working for some reason

rule “Nest”
when
Item gPhone changed from ON to OFF
then
createTimer(now.plusSeconds(600), [
if gPhone.state == OFF
sendCommand(Thermostat_Mode,‘ECO’) ])
end

Any ideas what could be wrong with this one?

Yes, if statements require brackets in rules language, see many examples.
Did you look in openhab.log to see what it had to say about it?

I even tried this and it didn’t work

when
Item gPhone changed from ON to OFF
then

createTimer(now.plusSeconds(20), [
if (gPhone.state == OFF) {
sendCommand(Thermostat_Mode, ‘ECO’)}
])
end

I have not looked into the logs as of now but what could be wrong with this code now?

Nothing is the logs, the rule didn’t execute, I am checking the logs using the following command

tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log

The last rule shown does not have a name. Every rule must have a name.

rule "Nest with timer"

What is less obvious is that every rule must have a unique name. You can’t have two rules called “Nest”, only one will work.

In the default file it has a name and it is unique, I have commented out the other one.

It actually worked, It got commented by mistake. Thanks for all the help guys.

Actually I have just realized that not a single rule is working.

Dows it happen when you don’t have Items defined manually and you’re referencing them in rules?

All of my items are created via PaperUI and if this is true then do I’ve to delete and recreate the Items manually?

Any help on this will be appreciated.