[SOLVED] Problem with a rule depending on getHourOfDay

I tried to create a rule that only would fire on a certain timeframe each day, but i guess i have not yet
completely understood working with variables in rules…
This is my rule:

var Timer myTimer_motion_SZ = null
var Number hour = now.getHourOfDay

rule "motion_SZ_ON"

when
    Item Motion_SZ changed from OFF to ON 
then
	if ((hour <= 21) && (hour >= 18)) {
		logInfo("motion_SZ.rule","Rule motion_SZ executed, the hour is: " +hour.toString)
		sendCommand("LED_SZ_4", "ON")
		myTimer_motion_SZ = createTimer(now.plusSeconds(20)) [|
			sendCommand ("LED_SZ_4" , "OFF") ]
	}
	else
	logInfo ("motion_SZ.rule", "Rule motion_SZ NOT execute due to time of day, the hour is: " +hour.toString)
end

i expected the variable ‘hour’ to be updated whenever the rule itself fires, but from the logs i am writing i think it only gets updated the moment i create this rule (or copy it into the folder, or change something in the rule)

can anyone help me with this?
is there a way to “refresh” the variable when the rule starts?
I’m a bit lost at the moment

The assignment to your hour variable happens only whenever the rules file is reparsed so that’s the value you get. Move that line into the rule body.

just beneath the initial “rule …” statement?
or is there a defined place in the body for variables?

No, beneath the “when”, that’s where the body begins.

thanks for the fast help!!
Thought i had read somewhere that var and val have to be declared only at the very beginning of a .rules-file
so i didn’t even try that

Well both is possible. Same as with most programming languages: a definition inside the body makes it local to the rule so you cannot access it from inside other rules. If that’s what you want to do you can define it globally as you did and add a update statement (i.e. hour = now.getHourOfDay) at the beginning of your rule.