[SOLVED] Multiple if conditions for triggering rules with time based

Yep, that will work.

I am also trying to do a rule that switches on when 1) from a certain time onwards eg 7am, and 2) a wifi presence is detected. My phone wifi presence is determined by this item - OnePlus5 under Unifi binding. So it gives On or OFF.

This is my current rule that only fulfills 1)

rule "Flexion power to ON"
when	
	Time cron "0 0 7 ? *MON-SAT *"
then	
	mqttActions.publishMQTT("cmnd/sonoff-flexion/POWER", "On")
	sendTelegram("bot1", "Flexion Microscope is switched ON")
	logInfo("Info","Flexion power ON Rule fired")
end

How do I add condition 2) to the current rule such that it will still trigger the switch(if both conditions met) even if after 7am but not after 5pm?

I’m not sure to understand correctly your request, but if you want to trigger in your time slot when your phone is detected, you should try this rule

rule ""
when
    Time cron "0 0 7 ? *MON-SAT *"
then
    if(phoneitem.state == ON ) {
    Actions_here
}

Structure your rule so that it works from any trigger.
Then add the triggers that you want.
Psuedocode -

when
     turns 0700   or
     Wifi presence changed
then
    if (time between 0700 and 1700) AND (Wifi presence true)
    then   do Stuff

Thank you @yippicai and @rossko57 for your replies.
I am sorry if I was not clear. But I want to be able to trigger the switch BOTH the conditions of 1) time slot between 7am to 5pm, AND 2) presence is ON. Not either or.

Are either of your suggestions what I intend to be?

Yep, read it and see what it does. Mentally work through it with various conditions. Remember that if X then do stuff has the implication if not X then don’t do anything

Would this amended rule be correct?

rule "Flexion power to ON"
when	
	Time cron "0 0 7-17 ? *MON-SAT *" or
	Item OnePlus5 changed from Off to On
then
	if (OnePlus5 == ON) AND time between "0700 and 1700"	
	mqttActions.publishMQTT("cmnd/sonoff-flexion/POWER", "On")
	sendTelegram("bot1", "Flexion Microscope is switched ON")
	logInfo("Info","Flexion power ON Rule fired")
end

Well, as a work in progress :smiley:

Time cron “0 0 7-17 ? *MON-SAT *” or

You don’t need to fire the rule every hour to meet your requirement. It will fire every time presence changes, and if presence doesn’t change then you have no work to do.
You only need to fire once at the beginning of your time window, 0700, to assess whether presence is on or off at that time.

if (OnePlus5 == ON) AND time between “0700 and 1700”

Okay, my suggestion was in psuedo code, just made up language to show you the logic of how your rule should work.
For a real rule, you will need to use the proper syntax as shown in the documentation and thousands of examples in this forum.
The structure of an if(), for example:

if ( conditions) {
    // more code
}

Note the curly brackets { } defining the if-block.

if ( (condition one) && (condition two) ) { .....

Note the && symbol to denote logical AND in rules.

if (OnePlus5.state == ON) ...

Note the use of the .state method to get the state of an Item - as opposed to its label, name, etc.

time between "0700 and 1700"

Nah. :smiley:
One way is to get the current hour from the system now variable and do the comparison.
Part of the solution

Here’s an example looking for an hour of day range, like you want.

1 Like

Thank you for your insights.

My rule looks like this now, but I dont see it working still

rule "Flexion power to ON"
when	
	Time cron "0 0 7 ? *MON-SAT *" or
	Item OnePlus5 changed to ON
then
	if (OnePlus5.state == ON && now.getHourofDay() >=7 && now.getHourofDay() < 17) {	
	mqttActions.publishMQTT("cmnd/sonoff-flexion/POWER", "On")
	sendTelegram("bot1", "Flexion Microscope is switched ON")
	logInfo("Info","Flexion power ON Rule fired")
	}
end

Doesn’t look far wrong to me.
I would move the logInfo to before the if() line, then it will tell the truth about the rule firing.
You can always put another logInfo inside the if-block to see if you pass the conditions.
What does openhab.log tell you about it?

I got this error

2019-02-22 11:20:30.589 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Flexion power to ON': 'getHourofDay' is not a member of 'org.joda.time.DateTime'; line 76, column 33, length 16

Okay, that should cause you to look closely at the use of that in your rule. Why would it fail for you, but work for other people? Compare carefully with other examples of usage, e.g. earlier on in this thread.

.getHourOfDay

Rules are (very) case sensitive.
We all make mistakes like this and they are very easy to overlook, but not that difficult to find using the error reporting.

Thank you Sir. Indeed the capital “O” of .getHourOfDay was the culprit.

If you are not yet using VSCode as a rule editor, I would recommend it. It catches typos like that. It is another thing to set up and something else to learn to use, but will pay off in the long run.

Actually I did install and setup VScode. But I haven’t figured out how to enable write priviledges to the conf files. My OH setup is on a synology docker. So I can’t save anything I change with VScode

I am getting this error with my rule which triggers a fan when temperature goes to 37 or higher

2019-03-11 21:10:43.251 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Rackfan power to ON': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_doubleArrow(T,org.eclipse.xtext.xbase.lib.Procedures$Procedure1) on instance: null

This is the rule

val mqttActions = getActions("mqtt", "mqtt:broker:mosquitto")

rule "Rackfan power to ON"
when
	Time cron "0 0 10 ? * MON-SUN *" or
	Item Sonoffps3_Temperature received update
then
	if ((Sonoffps3_Temperature.state => 37) && now.getHourOfDay() >=10 && now.getHourOfDay() <=18) {
    mqttActions.publishMQTT("cmnd/sonoff-rackfan/POWER", "ON")
	sendTelegram("bot1", "Rackfan is switched ON as temperature is above 37°C" )
	logInfo("Info","Rackfan power ON Rule fired")	    
    }
	
    else if ((Sonoffps3_Temperature.state < 38) && now.getHourOfDay() >=10 && now.getHourOfDay() <=18) {
    mqttActions.publishMQTT("cmnd/sonoff-rackfan/POWER", "OFF")
	logInfo("Info","Rackfan power OFF Rule fired")	
    }
    
end	

Compare => with >=, only one can be correct.

Hi,
I was trying to understand that thread to create a rule for my needs, unfortunately it throws this error when saving already:

2022-05-19 21:31:41.494 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'automation.rules' has errors, therefore ignoring it: [136,63]: no viable alternative at input '&&'

This is the rule. A dehumidifier should only work when a certain humidity is reached AND when there is enough PV power (my power meter shows negative values when it provides power to the grid, and positive when drawing from it. Hence the value)

when
	Time cron "0 0/5 * * * ?"
then
	if ((FeuchteNodemcu2.state as DecimalType).floatValue >= 61) && ((DSWirkleistung.state as DecimalType).floatValue <= -1000) {
		ShellyPlug2Output.sendCommand (ON)
		}
	else if ((FeuchteNodemcu2.state as DecimalType).floatValue <= 55) {
		ShellyPlug2Output.sendCommand (OFF)
		}
end

Any ideas what I am doing wong?

Have a look at the ( ) brackets if your if() statement - they exclude the && at the moment.
You can add spaces for clarity.

Thanks rossko, at least the error is gone. Need to see if the script works as intended when the sun is gone today :wink: