This object is in archive! 

Tutorial: How to find out if it's national holidays

Juraj Bezdek shared this idea 7 years ago
Under Consideration

I have some rules, which behavior should change if I am going to work or not. Weekends are easy, but consider that I stay home sick (I use virutal switch that needs to be set in advance) or there are national holidays. I have rules scheduled such as "turn on the coffee machine" or "wake me with lights". It's uncomfortable when you want sleep and these rules are triggered too early.

I want decide which rules should be triggered during days that I am going to work and which during weekends, holidays and the days that I am staying at home.


So my goal - I needed to let zipato know, that there is national holiday, so that it wont wake me up during Christmas etc.


Thanks to David Pitchard, who bring me to idea to use Google Aps script to do this.


The idea behind this is simple:


1. create virtual sensor on zipato named "isHoliday" (and control it's state from some external app)


2. create google workbook with two sheets.

First will have only regular holidays, such as christmas, which is always on the 24.12. every year.


Second will have irregular holidays, such as easter.

These holidays differs every year so you can't create simple rule, like

"if (month ==3 and day == 24 ) then". But you can write them down for

many years in front.


3. create google app script, that will be triggered

every night and call zipato isHoliday virtual sensor, with state calculated by

simple logic:


- IF ( currentDate is in regular holidays.setYear(currentDate.getYear)) then


>>return TRUE;


- ELSE IF (currentDate is in Irregular holidays) then


>>return true;


- ELSE


>>return false;


4. Use state of isHoliday virtual sensor to check whether I am going to work this morning :)


I am attaching simple tutorial with pictures and also the script it's self. Enjoy:)


Also for slovak folks... and excel with regular and irregular holiday up to 2022.

Replies (4)

photo
1

You're welcome!

That's an interesting approach. I actually do this with rules in Zipabox itself. I put in the standard ones and the variable ones, and just make a note to update the variable ones every year.

photo
1

Thas was my first attempt. It was painful to create such long rule... since we have much more holidays than it's common, it was lot's of clicks :)

But I didn't wanted to remember to change the rule every year... this is much more conveniant and also testable, since I could easily debug it in google IDE. Now I can be almost sure that it will work... working with dateTime in zipato more like trial&fail process

photo
1

Yeah and long rules are a problem. Initially I tried to make one large rule for public holidays and holidays (vacations), but the Zipabox is unable to store rules that are more than a couple of screens long.

You may also notice that the more nested conditions you have, the more quickly you hit this invisible limit.

When a rule gets large or complex, you have to be really careful. Refresh the page often to check that your changes have actually been saved!

photo
photo
1

Great work Juraj, it is good to have clever people on board.

photo
1

My running script is failing. The error is"


TypeError: Cannot call method "setHours" of undefined. (line 2, file "zipato")Line 2 is follows: date = new Date(date.setHours(0,0,0,0));

Basically copy and paste from tutorial. What might be wrong?

photo
1

It looks like you are trying to run the first function in the list (I suppose that it's default behaviour of google app scripts IDE)...


Just change the function you are trying to run/debug to setHolidaySensor in the toolbar (next to run button). That is the function you should run.

photo
1

Ok. So basically it is only required to run the setHolidaySensor.


That works fine without errors, however then there is another strange thing. I have added todays date to both regular and irregular (even if in only one of them), however when running the script, it sends 0 even that todays date is part of the spreadsheets. is this normal? Because I would expect that if I add todays date and run the script after, it should return 1 but it returns 0.

photo
1

Hmm that is strange indeed.


Could you please debug the script and look if it runs inside the "for" loop? In the debuger you should see all the values that were loaded into reugular/irregular array. Check if and how are they loaded.

photo
1

No good. I dont know how to debug (no skills). Here is the script in txt file. I have inserted todays date as regular holiday to test it.

photo
1

Duplicate post

photo
1

:) OK... so lets start with that...


1. add function like this to your skript file (at the end for example)

  1. function test()
  2. {
  3. Logger.log(isHollyday(new Date()));
  4. }


2. Choose test function in the function list. Then click on the "bug" icon on the toolbar.

In "View" menu there are some options, first two are the right ones. I don't know how google named them in english, but in slovak it's "Prepis vykonania" (1), and "Denníky" (2).


Please check both of them. They are filled with details about your last debug session.

If you don't understand them, feel free to post it here/ or on my email. Don't forget to delete the google spreadsheed link. (if its not here, the error must happend before or during opening it).

photo
Leave a Comment
 
Attach a file