We have moved at community.getvera.com

Author Topic: CODE: Public Holiday virtual switch  (Read 50963 times)

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
CODE: Public Holiday virtual switch
« on: April 22, 2011, 07:25:25 am »
Here's another virtual device for your MiOS-meddling pleasure...  It presents itself as a binary switch, which is
  • ON if today is a public holiday, and
  • OFF it today is not.

For "public holiday" you can substitute anything which can be represented as all-day events in an iCalendar (*.ics) accessible over HTTP.  Some ideas for using this device:

  • Use it as an inhibitor for the excellent WakeupRamp plugin so you can sleep in on holidays. (This is why I wrote the plugin.)
  • Publish your vacation dates to an iCalendar file, and set scenes to start when you go away, and scenes to stop when you come back from your trip.
  • Feed it with Jewish holidays from http://www.hebcal.com/ and use it for a sunset scene Luup Event condition.

To install:
1. Use SSH (Unix scp or WinSCP or another tool) to copy ical.lua to /usr/lib/lua.
2. Add D_HoildayVirtualSwitch1.xml, D_HolidayVirtualSwitch1.json and I_HolidayVirtualSwitch1.xml to MiOS Developers > Luup Files.
3. Create a new Luup device with UpnpDevFilename D_HolidayVirtualSwitch1.xml.  Set the Description to whatever you want to name the device.
4. Save, wait, then reload, then wait.  (It seems to take two Luup restarts for the variables to appear.)
5. Click the spanner/wrench and go to the Advanced tab.  Scroll to the bottom and in the calendar field, put the URL of the iCal file (e.g. http://www.vic.gov.au/ical/holidays.html).
6. Optional: change the offset field to 1 if you want the device to represent tomorrow's holidays (-1 for yesterday, 7 for next week, etc.).
7. Close the dialog and Save.
« Last Edit: January 01, 2012, 06:28:36 am by futzle »

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
Re: CODE: Public Holiday virtual switch
« Reply #1 on: April 22, 2011, 07:30:34 am »
Some screen shots:
  • The device on a day that isn't a holiday.
  • The device on a day that is a holiday.
  • The device control dialog.

Offline huogas

  • Sr. Member
  • ****
  • Posts: 301
  • Karma: +0/-0
Re: CODE: Public Holiday virtual switch
« Reply #2 on: April 22, 2011, 08:47:23 am »
Wow! What a good idea!

Thanks Futzle
Vera2 UI4(1.1.1245/3.2), Kwikset x1, HM-DW001 x1, ZRP100 x1, HA-04WD x1, ZDP100 x6, HSM100 x2, VRI06-1LX x8, ZW5301 x1, HM-FS001 x2, ZRW103W x1, GE 45606 x2, HA05C x1, TED5000 x1

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: CODE: Public Holiday virtual switch
« Reply #3 on: April 22, 2011, 09:15:43 am »
Nice plugin. Thanks for sharing your code.

Feature requests: :)
  • Use a local ics file
  • Display a [complete] list of future holidays on the 'Control' tab (could be implemented via HTML injection via UPnP variable ...)
  • Display a warning message if there are no future holidays in the ics file.

Edit:
MCV should integrate the holiday switch into the 'Timers' tab.
« Last Edit: April 22, 2011, 11:59:53 am by Ap15e »

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: CODE: Public Holiday virtual switch
« Reply #4 on: April 22, 2011, 04:21:00 pm »
Wouldn't it be a little bit less resource consuming to use a DOW timer in the startup code instead of an interval timer in periodic?

Code: [Select]
luup.call_timer("periodic", 1, "60", "", "")

Code: [Select]
luup.call_timer("periodic", 2, "00:00:01", "1,2,3,4,5,6,7", "")

... and please feel free to borrow some load balancing code from DAD. :)
« Last Edit: April 22, 2011, 04:24:44 pm by Ap15e »

Offline huogas

  • Sr. Member
  • ****
  • Posts: 301
  • Karma: +0/-0
Re: CODE: Public Holiday virtual switch
« Reply #5 on: April 22, 2011, 06:33:08 pm »
Wouldn't it be a little bit less resource consuming to use a DOW timer in the startup code instead of an interval timer in periodic?

For my information, what is the difference between the two ways of doing it ?
Vera2 UI4(1.1.1245/3.2), Kwikset x1, HM-DW001 x1, ZRP100 x1, HA-04WD x1, ZDP100 x6, HSM100 x2, VRI06-1LX x8, ZW5301 x1, HM-FS001 x2, ZRW103W x1, GE 45606 x2, HA05C x1, TED5000 x1

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: CODE: Public Holiday virtual switch
« Reply #6 on: April 22, 2011, 08:47:00 pm »
@futzle  a newbie question, your first install step "Add ical.lua to MiOS Developers > Extra Lua Files (or scp it to /usr/lib/lua)." do we need to enter the full directory path) e.g C:\Desktop\VERA stuff\HolidayVirtual\ical.lua? ???

Tried it both ways aka filename only and with the full directory path with no success, I get a generic device but my advance tab doesn't have a calendar field even after a number of Lua reloads.. more clues please
« Last Edit: April 22, 2011, 09:40:14 pm by Michael_N_Blackwell »
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
Re: CODE: Public Holiday virtual switch
« Reply #7 on: April 23, 2011, 12:37:45 am »
Wouldn't it be a little bit less resource consuming to use a DOW timer in the startup code instead of an interval timer in periodic?

I thought about that at the time... but I went with the code you see for a couple of not-very-compelling reasons.  Because of the known daylight saving bug that you found with timers, and because of my long history of programming with time zones, I'm leery of anything that works in local time.  Also, I considered enhancing the plugin later to handle iCal events shorter than a day and having a start and end time.  Perhaps the real reason is that I was lazy and wanted to code this up as quick as possible.

Thanks for the feature requests.  I'll try to find time to do them.  Also, patches welcome. :)

@futzle  a newbie question, [...] do we need to enter the full directory path)???

To be honest, I used scp to copy the file, so I can't speak for how well the Extra Lua Files page works.  The end result is that the Vera has to have a file /usr/lib/lua/ical.lua, and you can use whatever mechanism you like to get it there.  If you're a Windows person who doesn't like command lines, something like WinSCP is probably the way to do it.

The way these "upload file" dialogs work in web pages is that you should see the full path to your local file in the File field, but when it uploads to the Vera, the path gets stripped off and the Vera stores the file in whatever directory it likes, presumably the correct one.  There is a known bug with Internet Explorer which messes with the path, so it's generally recommended to use a different browser to do these uploads.

Chances are very good that the problem is with uploading the four files, so look for some evidence that the files were uploaded properly: you should be able to see the three I_ and D_ files in the Luup Files list, for instance.  Also, make sure that you didn't misspell the D_*.xml filename wrong when you created the device. Copy and paste the filename just to make sure.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: CODE: Public Holiday virtual switch
« Reply #8 on: April 23, 2011, 06:26:09 am »
PHVS thinks today (Saturday) is a holiday (Good Friday) in Germany ...

The reason is obvious:

From http://www.schulferien.org/iCal/Feiertage/icals/Feiertage_Nordrhein_Westfalen_2011.ics:

Code: [Select]
BEGIN:VEVENT
CREATED:20110114T125119Z
LAST-MODIFIED:20110114T125119Z
DTSTAMP:20110114T125119Z
SUMMARY:Karfreitag
DTSTART;VALUE=DATE:20110422
DTEND;VALUE=DATE:20110423
URL:http://www.schulferien.org/Feiertage
DESCRIPTION:Karfreitag ist gesetzlicher Feiertag in Nordrhein-Westfalen. Alle Termine auf www.schulferien.org/Feiertage
TRANSP:TRANSPARENT
UID:Karfreitag-Nordrhein_Westfalen-201104221197827424@schulferien.org
END:VEVENT

Is the ics data just plain wrong?

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: CODE: Public Holiday virtual switch
« Reply #9 on: April 23, 2011, 03:10:47 pm »
Ok @futzle, got working... please fix your post, in step 3 you missed spelled "3. Create a new Luup device with UpnpDevFilename D_HoildayVirtualSwitch1.xml.  Set the Description to whatever you want to name the device".

I kept copying your misspelled text into the new device with no success!  :-[ One good thing is you introduced me to WinSCP which is great tool, it's the first time I seen my actual VERA directory structures and files  :-* I was looking for a tool to review my logs... thanxs
« Last Edit: April 23, 2011, 04:59:30 pm by Michael_N_Blackwell »
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline huogas

  • Sr. Member
  • ****
  • Posts: 301
  • Karma: +0/-0
Re: CODE: Public Holiday virtual switch
« Reply #10 on: April 23, 2011, 04:11:12 pm »
I'll quote myself...
Wouldn't it be a little bit less resource consuming to use a DOW timer in the startup code instead of an interval timer in periodic?

For my information, what is the difference between the two ways of doing it ?
Vera2 UI4(1.1.1245/3.2), Kwikset x1, HM-DW001 x1, ZRP100 x1, HA-04WD x1, ZDP100 x6, HSM100 x2, VRI06-1LX x8, ZW5301 x1, HM-FS001 x2, ZRW103W x1, GE 45606 x2, HA05C x1, TED5000 x1

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
Re: CODE: Public Holiday virtual switch
« Reply #11 on: April 23, 2011, 07:42:18 pm »
Is the ics data just plain wrong?

No, the data for your state is right, unlike the data for mine.  The end date is supposed to be exclusive, so if the dates are the same (as for the Victoria file I was working off) you have a zero-day event.  The events in the NRW is a correct one-day event.

I have modified the file I_HolidayVirtualSwitch1.xml in the first post of this topic.  Please let me know if it fixes the bug.

It's not surprising that there are tools out there that get the ics format wrong.  I think that I am going to have to code a lot of workarounds into this plugin to handle them all.

got working... please fix your post

Fixed.  Thanks for finding that, and sorry for the wild goose chase.

For my information, what is the difference between the two ways of doing it ?

One sets a timer to call the periodic() function at 1 minute past midnight on the next day.  The other sets a timer to call the periodic() function 60 seconds from now.  The arguments to call_timer() are explained here: http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_call_timer

Offline m0jon

  • Full Member
  • ***
  • Posts: 113
  • Karma: +0/-0
  • @m0jon
Re: CODE: Public Holiday virtual switch
« Reply #12 on: April 24, 2011, 04:25:52 am »
I get "Lua Engine Failed to Start" after the first refresh?

m0jon

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: CODE: Public Holiday virtual switch
« Reply #13 on: April 24, 2011, 06:56:49 am »
Quote
I get "Lua Engine Failed to Start" after the first refresh?

Please use scp to transfer ical.lua to your Vera. 'MiOS developers' -> 'Extra Lua files' -> 'Add extra LUA file' doesn't seem to work ...

@futzle

Please modify step 1 of your installation instructions.
« Last Edit: April 24, 2011, 07:04:35 am by Ap15e »

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: CODE: Public Holiday virtual switch
« Reply #14 on: April 24, 2011, 07:03:20 am »
Quote
Please let me know if it fixes the bug.

Thanks for the fix, it does work now.