Fibaro HC2 LUA Scheduler New Home

Forum all about Z-Wave Fibaro
Post Reply
robmac
Starting Member
Starting Member
Posts: 6
Joined: Sat Dec 21, 2013 6:39 pm

Fibaro HC2 LUA Scheduler New Home

Post by robmac »

This is the same version as posted on Fibaro Forum but new updates will now be posted here.

This code is a LUA based scheduler that allows you to manage all timers from one scene on a Fibaro HC".

It only runs when there are events to process not every 1 minute.

It replaces the loop/ get time/ if time is good then action then sleep. I do not know if it is more efficient than the blocks but it is more efficient than the standard LUA generated.

I have now tested with over 1000 actions and it runs fine.

WHAT YOU NEED TO CHANGE:

Insert lines in the marked section <ADD YOUR LINES HERE>

luaDaySchedule:add(<time>,<id>, <parameter> , <action>, <days> ,<catchup>,<p1>,<p2>,<p3>)

THE TIME TO RUN:

<time> sting representation of time in 24h format eg "23:21"

note: you can not use a global variable to vary the time of an event unless you force a restart of the scene

SUNRISE SUNSET + OR - MINUTES:

Schedules solar based events with + or - n minutes delay or advance.

<time> "Sunrise" or "Sunset"
<p1> positive or negative number of minutes

ID OF DEVICE or SCENE :

<id> string or numeric id of a device or LUA table of ids

WHAT ACTIONS:

switch a device on or off
set a dimmer value
send an email,
send a push notifications to all devices
send an email notification to all users
send a push notification to a device
send an email notification to an user
push a virtual device button
set a virtual device slider
set a global variable
arm or disarm a device
set RGB colour
set white level
set red level
set green level
set blue level
start program
run another scene
set target level
kill scene (1.1.1 not tested)
enable scene (1.1.1 not tested)
debug (1.1.1 not tested)
setThermostat time and temp at same time
getValue and store in global
+ as an example of own function
get remaining time and store in a global and
a function that stores n temperature sensors values in n globals


<action> one of "call"
"setGlobal"
"startScene"
"pressButton"
"setSlider"
"setValue"
"sendEmail"
"sendGlobalPushNotifications"
"sendGlobalEmailNotifications"
"sendDefinedPushNotification"
"sendDefinedEmailNotification"
"setTime"
"setArmed"
"setRGBColor"
"setColor"
"setR"
"setG"
"setB"
"setW"
"startProgram"
"setTargetLevel"
"killScenes"
"setSceneEnabled"
"debug"
"setThermostat"
"getValue"

<parameter> the value of any parameter needed for the action see samples in module for individual actions

You can define extra functions within the module and schedule when to run. See note

CATCH UP RECENT ACTIONS:

<catchup> true or false

When restarting HC2 or editing the scene, the current state of all scheduled items is parsed so that the system plays the last action that should have executed prior to the current time. This will set all variables and devices to the state scheduled with no user interaction.

By default the code parses 24hrs of events to work out current state. Mark as true or false on action add to set to catchup or not.

The catch up works for devices, scenes and variables that are always controlled by the timer. The function works out what state the device, scene and variables should be set to at the time the HC2 is restarted or scene is saved and sets it to that level/value.

If the device is regularly switched manually you need to decide if you want to use the function as you may be better to leave the device in the last state or off controlled by the parameters on the device.

It works well for variables like isNight or timeOfDay. Good for heating and hot water as it makes sure the heating is on or off after the HC2 is restarted.

SCHEDULE ONLY CERTAIN OR ALL DAYS:

<days> a LUA table of strings days of the week (in english though you could edit the module> :-D

You can schedule Sunday and Monday for example by setting {"Sunday","Monday"} in the add call.

There are three specials
{"All"} = all days,
{"Weekend"} = Saturday and Sunday,
{"Weekday"} = Monday to Friday.
these can be combined with normal days
{"Weekend","Monday"} = Saturday, Sunday and Monday,

REPEAT:

<p1> the number of times to repeat starting with the scheduled time
<p2> the number of minutes between repeats
<p3> a value to increment the value by

You can schedule multiple/repeat actions after the initial action with a single line so you can slowly increase or decrease a dimmer or slider value.

On RGB controller you can slowly change colour and brightness with multiple actions using a single line.

Repeat does not work on events scheduled using sunrise, sunset, weekday, and weekend.

MORE ON HOW TO USE:
Usage is explained by example see comment --examples in code.

to get going

Add globals
alarmTime1
alarmDays1
alarmTime2
alarmDays2

import alarm clock twice

edit device id variables in the alarm clock code

Copy restart code into a new scene and save.

Copy scheduler code into a LUA scene and save.

edit alarm clock variable and scene ids in scene code and restart scene code

use alarm timers like this


--turnOn device id 304 controlled by alarm1
--luaDaySchedule:add(getAlarm("alarmTime1"),"304", "turnOn" , "call", {getAlarm("alarmDays1")} , false )

Then add your own schedule lines <ADD YOUR LINES HERE> using the examples as templates and remove the text <ADD YOUR LINES HERE>

NOTE this version adds:

alarm clocks based on Ucciogenerale
icon and more ideas from jompa68
pause at start to allow zwave to start



If you have a problem that requires both a time and other checks using if this then else if then else if then else. Embed the if this then else if then else if then else in another scene and schedule that scene from the scheduler.


DEBUG:
You can get more/less info in debug window of HC2 but will only display so much due to window length.

Code:
local debugadd = false;
local debugcatch = false;
local debug = false; --solar
--prints when it runs to debug
local reportRun = true;


UPGRADING:
Back up your lines in a text edit program. i.e. the ones you have added <ADD YOUR LINES HERE>.

Open the new file and paste your lines over <ADD YOUR LINES HERE>.

Follow any instructions on how to edit lines given with release.

HOW IT WORKS:
The scene is set to autorun and will only run one instance.

On start all of the lines you have defined in the add section are loaded to a LUA table. Any lines that are repeated n times at an interval with a value increment are generated and stored in the same table.

The code then parses the table and constructs a list of actions to run to catch up. One event per device is then run to bring the system to the approximate state if the schedule was run unattended. Note the true false value on the add to define it the event should be caught up.

The sunrise and sunset events are calculated during the day so they adjust with the change in solar cycles. These are stored in separate tables.

The code then runs any event scheduled for now, calculates the next time to run and goes to sleep until needed again. If you have reporting on you will note a message reporting a time correction. This is a calculation to make sure schedule executes at the middle of the minute.

With run report flag set to true output is like this: (most of my events removed to illustrate the long sleeps and have a short list)


Code:
[DEBUG] 09:17:20: Catching up actions.
[DEBUG] 09:17:20: Event scheduled for 08/05/13 at 05:00
[DEBUG] 09:17:20: fibaro:setGlobal("TimeOfDay","Morning")
[DEBUG] 09:17:20: Event scheduled for 08/05/13 at 05:55
[DEBUG] 09:17:20: fibaro:setGlobal("NightTime","0")
[DEBUG] 09:17:20: Event scheduled for 08/05/13 at 06:00
[DEBUG] 09:17:20: fibaro:call(19,"turnOn")
[DEBUG] 09:17:20: Event scheduled for 08/05/13 at 08:00
[DEBUG] 09:17:20: fibaro:setGlobal("HotWaterHeat","0")
[DEBUG] 09:17:20: Catching up finished.
[DEBUG] 09:17:20: Next Run at 10:30:30
[DEBUG] 09:17:20: Correct by -10s error,
[DEBUG] 09:17:20: Going to sleep for 73mins
[DEBUG] 10:30:30: Schedule running at 10:30:30
[DEBUG] 10:30:30: fibaro:call(19,"turnOff")
[DEBUG] 10:30:30: Next Run at 12:00:30
[DEBUG] 10:30:30: Correct by 0s error,
[DEBUG] 10:30:30: Going to sleep for 90mins
[DEBUG] 12:00:30: Schedule running at 12:00:30
[DEBUG] 12:00:30: fibaro:setGlobal("TimeOfDay","Afternoon")
[DEBUG] 12:00:30: Next Run at 12:05:30
[DEBUG] 12:00:30: Correct by 0s error,
[DEBUG] 12:00:30: Going to sleep for 5mins


Known Issues : no validation on some values
Attachments
scheduler-1-3-0.zip
(48.46 KiB) Downloaded 836 times
jompa68
Starting Member
Starting Member
Posts: 5
Joined: Sun Aug 23, 2009 10:45 am
Location: Sweden
Contact:

Re: Fibaro HC2 LUA Scheduler New Home

Post by jompa68 »

Yey! Best script ever [SMILING FACE WITH OPEN MOUTH]
and thanks for your help with it, the support of others makes it possible to make it good.
Last edited by Bwired on Sun Dec 22, 2013 3:43 pm, edited 1 time in total.
Reason: cleaned up
jompa68
Starting Member
Starting Member
Posts: 5
Joined: Sun Aug 23, 2009 10:45 am
Location: Sweden
Contact:

Re: Fibaro HC2 LUA Scheduler New Home

Post by jompa68 »

robmac
Do you think my latest request will be possible?


Jonny sent this from his iPhone using Tapatalk
robmac
Starting Member
Starting Member
Posts: 6
Joined: Sat Dec 21, 2013 6:39 pm

Re: Fibaro HC2 LUA Scheduler New Home

Post by robmac »

Great idea and yes will be sorting it out and doing a bit of tidy up.

Any other requests?

In general if there are any volunteers to support.

It could do with a load of worked examples if anyone wants to share their projects using it. It would be nice to think that HC2 will introduce better scheduling but I am not holding my breath.

+ there is no reason why it is only in english.

Please feel free to translate the instructions

In the next version I will mark up areas and get all english text into table so it can be replaced with local languages.

Have a Great Christmas.
jompa68
Starting Member
Starting Member
Posts: 5
Joined: Sun Aug 23, 2009 10:45 am
Location: Sweden
Contact:

Re: Fibaro HC2 LUA Scheduler New Home

Post by jompa68 »

what about setting new icon at specific time from scheduler?

Code: Select all

fibaro:call(214, "setProperty", "currentIcon", 1064)
Something like this?

Code: Select all

setProperty = function (device, value)
fibaro:call(device, "setProperty" , "currentIcon", value )   
    if reportRun then  fibaro:debug( "fibaro:call(" .. device .. ", \"setProperty\" ,\"currentIcon\" ," .. value .. ")" ) end 
end,
BozmN
Starting Member
Starting Member
Posts: 2
Joined: Fri Dec 27, 2013 5:50 pm

Re: Fibaro HC2 LUA Scheduler New Home

Post by BozmN »

Robmac I agree with your opinion on the Fibaro forum, I have tried to register on that forum over 1 month now without success.

I use your script and it works excellent... Thanks for that!

Sent from my iPad using Tapatalk HD
BozmN
Starting Member
Starting Member
Posts: 2
Joined: Fri Dec 27, 2013 5:50 pm

Re: Fibaro HC2 LUA Scheduler New Home

Post by BozmN »

Ok I tried to enable scenes with the script..

this is what I got:

luaDaySchedule:add("Sunrise","24", true , "setSceneEnabled" {"All"} , true )
got this:
[ERROR] 17:29:19: line 1258: ')' expected near '{'


luaDaySchedule:add("Sunrise","24", true , "setSceneEnabled", {"All"} , true )
got this:
[DEBUG] 17:17:44: Event scheduled for 12/29/13 at 14:53
[ERROR] 17:17:44: line 411: attempt to concatenate global 'action' (a nil value)
Post Reply

Return to “Fibaro”