How to use variables in "when" block of rules?

Tags: #<Tag:0x00007f617c7b6bc0> #<Tag:0x00007f617c7b6940> #<Tag:0x00007f617c7b6828>

I want to use variable in “when” block of rules.
My code:

val DayTime_day_start_hours = 7
val DayTime_day_end_hours = 23
val str1 = "0 " + DayTime_day_start_minutes + " " + DayTime_day_start_hours + " ? * * *"

rule "When system started" 
when
    System started
then
  logInfo("RRRLog", "Rule 'When system started'")

  if(now.withTimeAtStartOfDay.plusHours(DayTime_day_start_hours).isBefore(now) && now.withTimeAtStartOfDay.plusHours(DayTime_day_end_hours).isAfter(now)){
    DayTime_day.postUpdate(ON)
  }
end

and this rule

rule "DayTime_bed OFF DayTime_day ON"
when
    Time cron "0 " + DayTime_day_start_minutes + " " + DayTime_day_start_hours + " ? * * *"
then
    DayTime_bed.postUpdate(OFF)
    DayTime_day.postUpdate(ON)
end

or this rule

rule "DayTime_bed OFF DayTime_day ON"
when
    Time cron str1
then
    DayTime_bed.postUpdate(OFF)
    DayTime_day.postUpdate(ON)
end

I get an error:

[WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'def.rules' has errors, therefore ignoring it: [91,20]: missing 'then' at '+' [92,1]: no viable alternative at input 'then'

or

Configuration model 'def.rules' has errors, therefore ignoring it: [91,15]: mismatched input 'str1' expecting RULE_STRING

Unfortunately you can’t

You will get this flexibility if you switch to NGRE

What is “NGRE”?

To get started with Python see [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x).

Keep in mind that even in NGRE, the when triggers are created when the script is loaded. You can’t later change the value of the variable and have it change the trigger without reloading the Rule. There are ways to do that too but it doesn’t happen by default.