Quick Question on Time of day

I have a rule to inform me if one of my servers go offline when they shouldn’t that I only want to check during the hours of 8am to 11pm. The servers will shutdown over night deliberately when doing cloud backups or maintenance

I use the following as a variable to ensure the rule only trriggers during that time. It seems to not work occasionally, can anyone tell me if anything is wrong with the syntax, the IDE is happy.

var boolean ValidTime = (now.isAfter(now.withTimeAtStartOfDay.plusHours(8)) &&
now.isBefore(now.withTimeAtStartOfDay.plusHours(23)))

the test is

if (ValidTime == true)
{

}

Thanks in advance

Where do you set the var?

I’m setting it there in the declaration ?
As a global variable for the rules file.

You have to set the var within the rule… the definition is always static, even if you use dynamic functions.

Oh I see, so as I have three rules one for each server, I need to set it in each one, but its ok to use the same var in all three?

thanks

Assuming your rules may be run at any time (e.g. triggered by a server going offline) you would want to calculate the time at the moment the rule runs, no?

Nope, I’m merely wanting to know IF they go off during their normal working hours.
The rule actually sends a broadcast, after a 5 minute delay, when they go offline, (supposedly only between 8-11)

They are on automatic restart at 7am so I expect them off anytime after 11pm and I expect them on at 8am. If you’re interested the rule is below.

rule "Server1_Failover"
when 
	Item Server1 changed
then
	if (ValidTime == true)
	{
		if (Server1.state == OFF)
		{
			logInfo ("Presence", "Unexpected offline Status for Server 1, setting broadcast for 5 minutes")
			presenceTimer1 = createTimer(now.plusMinutes(5), [|
				sendBroadcastNotification("The Server no.1 [xxx.xxx.xxx.xxx] has gone offline for longer than 5 minutes")])
			server1_bad_state = true
		}
		else if ((Server1.state == ON) && server1_bad_state == true)
		{
			logInfo ("Presence", "Server 1 status is back online")
			server1_bad_state = false
			if (presenceTimer1 != null)
			{
				presenceTimer1.cancel
				logInfo ("Presence", "Broadcast has been cancelled")
				presenceTimer1 = null
			}
		}
	}
	else if (server1_bad_state == true)
		server1_bad_state = false 
end

Well, the principle remains. If you want to calculate using the time when the rule runs, you must do the calculation within the rule. Doing the calculation once when the rule file is loaded will not do what you want.

I understand that now, so the natural come-back is when is it ok to define a global variable in a rule file.

In many of the Demo Examples be it in the Wiki or provided by the project contributors the following are defined globally

var Timer timer = null
.......
var Number counter = 0
var Number lastCheck = 0
.........
 // Constants
    var Number K = 0.017453
    
// Change this reflecting your destination
    var Number latitude = xx.xxxxxx
    var Number longitude = yy.yyyyyy
.........

 // Constants
    var Number K = 0.017453
    
    // Change this reflecting your destination
    var Number latitude = xx.xxxxxx
    var Number longitude = yy.yyyyyy

These are assigning values, just for clarification how does that differ from assigning a value from a calculation?

thanks

The difference is not in the calculation in general, but rather in the fact that your particular calculation is dependent on time, thus it gives a different result depending on when it runs.

As explained above, with doing this as a global var, then your calculcation will be performed (once) when the rules file is loaded which is not the time you are interested in.

So, I think you have two options:

  1. Make this a local var inside each rule and do the calculation each time a rule fires, or

  2. Change your ValidTime var to an OpenHAB item, and create two cron based rules to switch the state of this item to ON or OFF at the boundaries of your time interval.

1 Like

Brilliant, that clarifies it nicely for me and hopefully others.