Create Rule with more day and timerange

Hi,
I have this rule condition:

I need this rule condition is satisfied on MON TUE WED THU SUN between 7 and 23, or FRI SUN between 8 and 1.

Now I use 2 rules, is it possible to achieve this with only 1 rule grouping day and time in only one item?

Thanks

Yes but not through the UI which can’t handle complicated conditions like that.

You’ll need to create a Script Condition and write the code using your rules language of choice to determine whether to run. The last line executed by the script needs to evaluate to true or false.

Hi,
is this the script you are speaking for?

Basically I well know Rule DSL but I am moving my first step into JS, but I need help on How can I return True or false, is there some example for both language? Thanks a lot

Yes, that’s how you would create a Script condition.

It’s the same in any language. You’ve created such an expression every time you use an if or switch statement.

a == b

That evaluates to true when a equals b. More of true time the excision is more complicated than that but that’s all there really is to it.

1 Like

so if I well understand, last statement of the script must be an “IF” condition that return true or false.
So in my case I can evaluate all time and day of the week and the set a variable to evaluata at the end of the script.
Is this the right way?

No, it must be a condition like you would use in a if statement.

You might also have if statements involved but what ever the last line of the script condition executed is needs to evaluate to true or false.

Examples:

  • If you want to always run the rule: true
  • If you never want to run the rule: false
  • If you want to run the rule only when a Switch Item is ON: items.getItem('MySwitch').state == 'ON'
  • If you only want to run the rule if two Item’s states are the same: items.getItem('Item1').state == items.getItem('Item2').state
  • If you only want to run the rule on weekends and when a switch Item is ON: actions.Ephemeris.isWeekend() && items.getItem('MySwitch').state == 'ON'
  • If you want to run the rule on weekends when one switch is ON and on weekdays when a different switch if ON:
if(actions.Ephemeris.isWeekend()) {
  items.getItem('MySwitch1').state == 'ON';
}
else {
  items.getItem('MySwitch2').state == 'ON';
}

In this last example there are two potential last lines of the rule depending on whether it’s a weekend or not. That’s why I’m very careful to use the phrase “last line evaluated” because any given script might have multiple potential last lines evaluated depending on the branching possible in the script.

1 Like