Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

LUA scheduler for HC2 v1.2.9


robmac

Question

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

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

luaDaySchedule:add(,, , , ,,,,)

THE TIME TO RUN:

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.

"Sunrise" or "Sunset"

positive or negative number of minutes

ID OF DEVICE or SCENE :

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

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"

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:

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:

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

Please login or register to see this image.

/emoticons/default_icon_biggrin.gif" alt=":-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:

the number of times to repeat starting with the scheduled time

the number of minutes between repeats

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.

Copy code into a LUA scene and save.

Then add your own schedule lines using the examples as templates and remove the text

NOTE this version adds:

-- new functions for RGB setR setG setW setB and startProgram

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 .

Open the new file and paste your lines over .

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

  • Like 1
Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Very nice post, Thank you

Please login or register to see this image.

/emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

Link to comment
Share on other sites

  • 0

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" /> i have run this four a couple of weeks now and i think it very good .

Now i have bought an Aeotec 4-in-1.

I would implement this in the code special the light sensor .

When the light sensor tells is dark out it will switch on light but it will switch the light off with time 21:15.

In the morning it will switch on light with timmer 07:00 but it will switch off with the light sensor when the sun is lightning.

I wounder how it should done this.

Best regards

Johan

Link to comment
Share on other sites

  • 0

Hi Robmac,

I tried and implement a Off functionnality for the alarm clocks by just adding "Off" in the choice of days !

It s not as nice as adding an on/off button but it's quick to do and seems to work.

Lines modified in the code:

VD alarmclock

button to change days: added

Please login or register to see this code.

line 14 and a coma at the end of line 13

added

Please login or register to see this code.

line 28 and a coma at the end of line 27

modified line 33 as follow:

Please login or register to see this code.

button to save: added

Please login or register to see this code.

line 22 and a coma at the end of line 21

Scheduler scene: updated the function luaDaySchedule.days by adding line 574

Please login or register to see this code.

and a coma at the end of line 573

I hope it helps.

Link to comment
Share on other sites

  • 0

Hi

i tried this but i' am new

made VD with VID413 Alarm Clock

made VD with VID414 Schedule Control

made scene with SID 412 Scene-1

made scene with SID 413 Scene-2

when i run this i get

[DEBUG] scheduler is intitialising. Loading whith profile group WinterNormal. --But i have set it on SummerNormal in Schedule Control VD

[DEBUG] initialise Alarm alarmTime2 in room woonkamer.

[ERROR] line98: attempt to concatenate a nil value -- this is the line -- fibaro:debug( "Initialise Alarm " .. i .. " in room " .. fibaro:getRoomNameByDeviceID(v.deviceID) .. ".")

another question

what i wanted to do is if time VID413 is as os.date("*t") then lights ID63 goes on.

Thanks

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Please login or register to see this link.

    If 4.x use the version half way through that was for BETA.

    One way or possibly another there will be a new version soon.

    Link to comment
    Share on other sites

    • 0

    How should offesets to Sunset/Sunrise be specified? I tried 

     

    luaDaySchedule:add("Sunrise-60", "151", "turnOff" , "call", {"All"},true)
     
    but that didn't work.
    In another forum I found an exampel with the same command as
    luaDaySchedule:add("Sunrise", "151", "turnOff" , "call", {"All"},true, -60)
     
    Which one is right?
     
    I also cannot get a list of ID's to work, forcing me to have a new .add for every id.
    Ie, I would expect the following to work but then the event is silently ignored.
     
    luaDaySchedule:add("Sunrise", { "151", "153", "155"}, "turnOff" , "call", {"All"},true)
     
    Great pience of addon to HC2 all in all!
    Link to comment
    Share on other sites

    • 0

    There is no code in this topic and the link for updated version is not working. Please delete this topic because is just a waste of time. 

    Link to comment
    Share on other sites

    • 0

    Please login or register to see this link.

     

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...