How to return a value from a script?

I have script to check if now() is holiday it looks like this:

if(currDate.getDayOfWeek() == DateTimeConstants.SUNDAY && currDate.getDayOfWeek() == DateTimeConstants.SATURDAY && ) return true 
if( currDate.getMonthOfYear()= 1 && currDate.getDayOfMonth() == 1 ) return true   // Nowy Rok 
    return false

as you see it returns true or false. From the rule I can call it using

callScript("isHoliday")

but what is proper way to to get return value, shoul I use ?

isHoliday = callScript("isHoliday")

Michal Szymanski

I don’t know if this will work as expected. At least I’m using an item to store holiday state and set the state in the script.

By the way, your first line will always return false, as there is no chance that the current day of week is Saturday and Sunday at the same time :slight_smile: && is AND, || is OR :wink:

I vaguely remember reading a long time ago that scripts do not return values.
They are more thought to be little reusable blocks of script code, not actual functions.
The right way to do it then would be to use items, as Udo already said.

If you need functions, you can do those by using lambdas. There are examples of that to be found in the forum, I am sure.

Yes I know it is error of course :slight_smile:
I can set a state of item in the script using sendCommand but one question is it ok that I will have something like this:

callScript(“isHoliday”) // set isHoliday inside a script
if (isHoliday.state==true) {
…

I know that changing a state can take some time and is performed asynchronously .

Typically you would set isHoliday at Midnight and at System start. There is no need to do this calculation just in time :slight_smile:

1 Like

Can I use “return” in lambda expression? Code that calculate holiday days has many “return” and what I’ve seen in lambda expression examples there was no return - only in the last line I’ve seen value that is returned.

Michal Szymanski

Well, you are right :slight_smile:

The return statements can happen anywhere in the code block contained in the lambda afaik.

But I’ve seen the remark that using “return” in the rule is not recommended (maybe only for the rule not for lambda).

Michał Szymański