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


Recommended Posts

  • Topic Author
  • 1 hour ago, jompa68 said:

    Get some error and it is perhaps my fault because i setup triggers wrong...

     

    Please login or register to see this code.

    When i did go to sleep last night an error did occur in debug and this morning the 4th Rule.eval did not run.

     

    Please login or register to see this code.

    UPDATE

    Found why the 4th Rule.eval did not run. Should be this, perhaps the error from last night is gone also now.

     

    Please login or register to see this code.

     

    Yes, the previous rule would give that kind of error. I will see if I can implement more helpful error messages.

    Link to comment
    Share on other sites

  • Topic Author
  • "Cross posting" dimmer use case. Dimming up and down a light when holding in a key on the fibaro keyfob

     

    Link to comment
    Share on other sites

    I really like the possibility to test all rules with ZeroBraneStudio before place them in a live scene :D :-D

    Link to comment
    Share on other sites

  • Topic Author
  • 4 hours ago, jompa68 said:

    I really like the possibility to test all rules with ZeroBraneStudio before place them in a live scene :D :-D

    Thanks, I just pushed a new version with some bug fixes and enhancements.... introduced a bug in the last version that prevented some rule to trigger when running off-line.

    Yes, it is *very* convenient to test rules off-line. I especially like to be able to post "fake" events to see how my rules reacts.

    Ex. a simple turn on light when sensor is breached

    Please login or register to see this code.

     

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • .If I have many sensors I want to test the logic for I make an helper function that automatically makes the sensor safe after a specified time

    Please login or register to see this code.

    Here I can breach s1 and it will automatically become safe after 30s as specified when created with makeSensor. It will behave like a real motion sensors and extend the 30s window if breached again before. 

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • A reason that I developed the script language was that I want some kind of library of global functions. 

    See thread

    Please login or register to see this link.

    ...

     

    The EventScript language I put together is almost a complete language (almost a Lua in Lua) but developing more advanced scripts in it still requires insights into programming in general and can be quite complex. The idea has been more that because scripts are Lua strings I can store them outside the EventRunner scene, lika a Fibaro global or another scene and read them into the EventRunner at startup - something we are not allowed to do with Lua code.

    More general, a library with known working script templates can be developed for common automation tasks and easily be instantiated by people that are not so proficient in coding.

    Example below are two templates, one that turns on/off a lamp using a sensor, and another template that dims a light up and down when a key is pressed on a keyfob.

    This is close to real sharable code, on a home automation tasks level... or like scenes in scenes...

     

    Please login or register to see this code.

    This is still a proof of concept and need some more error checks and storing script templates elsewhere...

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • Here is an attempt to describe syntax and commands available in the EventScript language. Is not complete but will be updated over time.

    There are 2 previous posts on how to write

    Please login or register to see this link.

    and

    Please login or register to see this link.

    with EventScript.

     

    Grammar

    Please login or register to see this code.

     

    Rules

    Script rules comes in four flavours, and always contain a '=>'

    Please login or register to see this code.

    Rules are registered and invoked when matching conditions are met.

    Expression given to Rule.eval without an '=>' is evaluated immediatly.

    Please login or register to see this code.

    @time -- always part of a rule. Run action at time of day

    Please login or register to see this code.

    @{t1,...tn} -- always part of a rule. Run action at times of day

    Please login or register to see this code.

    @{catch,t1,...} -- if the first argument in the time parameter list is the constant 'catch' the framework tries to catchup actions that have already passed when the framework starts up

    Please login or register to see this code.

    @@intervall -- always part of a rule. repeat action at specified interval

    Please login or register to see this code.

    #event -- event triggering rule.

    '#foo' is a short for {type='foo'}

    '#foo{bar=9}' is a short for {type='foo', bar=9}

    Please login or register to see this code.

    Matching with constraints are allowed in event values and start with '$..'. Variables bound in matching expression available as local vars in rest of rule.

    Please login or register to see this code.

    'env' -- script variable that is bound the the environment the script is executing in. Specifically env.event is the event (sourceTrigger) that triggered the rule.

    Please login or register to see this code.

    Mathematical and logical operators

    '!','&','|' - logical operators

    Please login or register to see this code.

    '+','-','*','/','%' - matematical operators. '%' is the 'reminder'/'modulus' operators

    Please login or register to see this code.

    '>','<','>=','<=','~=','==' - comparision operators. works with numbers and strings

    Please login or register to see this code.

    '+=','-=','*=' - increment and assign. works with variables only

    Please login or register to see this code.

    'rnd([<low>,]<high>)' -- random number between <low> and <high> including, <low> defaults to 1

    Please login or register to see this code.

    'round(<num>)' -- round a number. round(<num>) == math.floor(<num>+0.5)

    Please login or register to see this code.

    'sum' -- return sum of table with numbers

    Please login or register to see this code.

    'min(<e1>,...<en>)' or 'min(<table>)' -- min of arguments or table of arguments

    Please login or register to see this code.

    'max(<e1>,...<en>)' or 'max(<table>)' -- max of arguments or table of arguments

    Please login or register to see this code.

    Assignment

    Please login or register to see this code.

     

    Device properties

    'ID:isOn' - :isOn returns true if device is on, i.e. 'fibaro:getValue(ID,'value')>0'.

    {ID1,...,IDn}:isOn is true if any of the devices are on. The rationale is that if any lamp is on in a room, the room is considered lit.

    Please login or register to see this code.

    'ID:isAllOn' - If there is a need to test if all IDs are on, e.g. {ID1,...,IDn}:isAllOn

    Please login or register to see this code.

    'ID:isOff' - :isOff returns true if device is off, i.e. 'fibaro:getValue(ID,'value')==0'.

    {ID1,...,IDn}:isOff is true if all the devices are of. The rationale is that if any lamp is on in a room, the room is considered lit.

    Please login or register to see this code.

    'ID:isAnyOff'- If there is a need to test if any ID is off, e.g. {ID1,...,IDn}:isAnyOff

    Please login or register to see this code.

    'ID::value'- ID:value i.e. 'fibaro:getValue(ID,'value')'

    '{ID1,...IDn}:value returns a table of values for the IDs listed

    Please login or register to see this code.

    'ID:last' - :last returns seconds since the device was last changed.

    Only supported by device that have last modified/lastbreached properties.
    'ID:scene' - :scene returns sceneActivation value (button clicked value) of the device

    Please login or register to see this code.

    'ID:central' - CentralSceneEvent - returns the 'data' portion of the event

    Please login or register to see this code.

    'ID:access' - AccessControlEvent- returns the 'data' portion of the event

    Please login or register to see this code.

    'ID:safe' - :safe returns true if the device is safe, i.e. 'fibaro:getValue(ID,'value')==0'

    Please login or register to see this code.

    'ID:breached' - :breached returns true if the device is breached, i.e. 'fibaro:getValue(ID,'value')>0'

    Please login or register to see this code.

    'ID:lux' - :lux returns the lux value for a light sensor, i.e. 'fibaro:getValue(ID,'value')'

    Please login or register to see this code.

    'ID:temp' - :temp returns temperature for a temp sensor, i.e. 'fibaro:getValue(ID,'value')'

    Please login or register to see this code.

    'ID:manual' - :manual returns seconds since last manually changed, or -1 if changed by the script.

    Please login or register to see this code.

    'ID:bat' - :battery returns battery property. i.e.'fibaro:getValue(ID,'batteryLevel')
    'ID:armed' - :armed returns if device is armed. i.e. 'fibaro:getValue(ID,'armed')
    'ID:R',':G',':B: - gets RGB values from a RGB device.

    'ID:name' - name of device ID

    'ID:roomName' - name of room for device ID

    If a device doesn't support a property there will be an runtime error.

     

    Some ':' properties acts as commands.
    'ID:on' - :on turn on device ID. i.e. 'fibaro:call(ID,'turnOn')'

    Please login or register to see this code.

    'ID:off' - :off turn off device ID. i.e. 'fibaro:call(ID,'turnOff')'
    'ID:value=<expr>' - calls 'setValue' on device ID. i.e. 'fibaro:call(ID,'setValue',<expr>)

    Please login or register to see this code.

    'ID:toggle' - :toggle turn on device if off, otherwise turn off. 

    Please login or register to see this code.

    'ID:armed=<0/1>' - arms/disarms a device 

    Please login or register to see this code.

    'ID:start' - :start starts scene with ID. i.e. 'fibaro:startScene(ID,)'

    Please login or register to see this code.

    'ID:start=args' - :start starts scene with ID and args. i.e. 'fibaro:startScene(ID,args)'. However, if args is an event '{type=...}' then it translates to 'args._from=__fibaroSceneId; fibaro:startScene(ID,{json.encode(args)})'

    Please login or register to see this code.

    'ID:stop' - :start stops scene with ID. i.e. 'fibaro:killScene(ID)'
    'ID:msg="text"' -- :msg="text" sends "text" to phone device with id ID. i.e. 'fibaro:call(ID,'push',"text")'

    Please login or register to see this code.

    'ID:btn=BtnID' - :btn=BtnID, press button with id BtnID on virtual device ID, i.e. 'fibaro:call(ID,'pressButton',BtnID)'

    Please login or register to see this code.

    'ID:R=<expr>','ID:G=<expr>','ID:B=<expr>' - sets RGB values for a RGB device.
    'ID:color={<expr>,<expr>,<expr>}' - sets RGB values for a RGB device.


    Date and time

    'HH:MM[:SS]' - time constant translating to seconds 60*(MM+60*HH)+SS

    Please login or register to see this code.

    '+/HH:MM[:SS]' - same as os.time()+HH:MM:SS

    Please login or register to see this code.

    't/HH:MM[:SS]' - epoc today, e.g. epoc(last midnight)+HH:MM:SS

    Please login or register to see this code.

    'n/HH:MM[:SS]' - epoc today if current time before or tomorrow if current time has passed

    Please login or register to see this code.

    'ostime()' - returns epoc, seconds since january 1st, 1970

    Please login or register to see this code.

    'osdate([frmt]) - Works as Lua os.date. 

    Please login or register to see this code.

    'midnight' - constant returns epoc seconds to last midnight
    'sunset' - constant returns seconds since midnight to sunset

    Please login or register to see this code.

    'sunrise' - constant returns seconds since midnight to sunrise

    Please login or register to see this code.

    'now' - constant that return seconds since midnight

    Please login or register to see this code.

    'wnum' - constant that returns the current week number (important in Sweden)

    Please login or register to see this code.

    '<expr>..<expr>' -- test if time is between interval, including.

    Please login or register to see this code.

    'day(<str>)' - returns true if day number expressin is true

    Please login or register to see this code.

    'month(<str>)' -

    Please login or register to see this code.

    'wday(<str>)' -

    Please login or register to see this code.

    'date(<str>)' - TBD

     

    Tables

    Please login or register to see this code.

    'sort(<table>)' -- sort a table

    Please login or register to see this code.

    'size(<table>)' -- return size of table

    Please login or register to see this code.

     

    Logging and format functions

    'osdate([fmt]) -- see date and time section
    'fmt(str[,...])' -- string format like Lua string.format

    Please login or register to see this code.

    'log(str[,...])' -- loggs string to console and also return the string. arguments like string.format

    Please login or register to see this code.

    'tjson(<expr>)' calls json.encode on expression 

    Please login or register to see this code.

    'fjson(<expr>)' calls json.decode on expression 

    Please login or register to see this code.

    'trace(<bool>)' -- starts tracing the byte code, only for the fearless
     

    Event functions

    'post(<event>[,<time>])'
    'cancel(<post ref>)'

     

    Delays and rule state 

    'once(<expr>)' onceis a special function that keeps state so that it only returns true if it's argument is true but was false before. This is a convenient way to stop an event from triggering multiple times.

    Please login or register to see this code.

    This rule triggers whenever the sensor triggers and then checks if it is between 6-8. If that is true the test 06:00..08:00needs to turn false before it will be able to turn true again, effectively only greeting people once between 6-8 when the sensor is breached.


    'trueFor(<time>,<expr>)'- forallows for checking if a condition is true for a specified duration.

    Please login or register to see this code.

    The forexpression needs in this case to be true for 10min before it will return true. First time the expression is true a timer is started that checks in 10min if the expression is still true and then returns true. If the expression turns false during that period, the timer is canceled.

     

    'again([<num>])' - Sometimes when the trueFor expression becomes true, one wants to reset the timer to get a new trigger the time again. Typical case is if a door is open for 10min send a notification, and continue to send an alarm for every 10min until the door is closed. So, there is a need to 'tell' the forcommand to re-enable the timer. ¨repeat' does that.

    Please login or register to see this code.

    This will log a message every 10min as long as the door is open. again returns the number of repetition it has currently done, and it can take an argument how many repeats it should do.

    Please login or register to see this code.

    In this case it will only repeat 3 times, and the for-timer is not reset. However, the next time the for expression becomes true it starts over again.


    'wait(<time>)' - waits specified time before continuing executing expression.  'fibaro:sleep' is not allowed as it stops anything going on in the scene while sleeping. 

    Please login or register to see this code.

    If device 55 is turned on it waits 10min, then checks if 55 hasn't changed state in the last 10min and if so turns off the light. waitis also useful for writing expressions to test rules.

    Please login or register to see this code.

    This turns on the lamp at 10:00, off again at 10:05, and then on again at 10:07.

     

     

    VD functions
    'label(<ID>,<name>)'
    'slider(<ID>,<name>)'

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab maybe you have in your post some typo:

    1. Only one } 

    Please login or register to see this code.

    2. I think there should be room2.lamp:on

    Please login or register to see this code.

    Link to comment
    Share on other sites

  • Topic Author
  • 9 minutes ago, petrkl12 said:

    @jgab maybe you have in your post some typo:

    1. Only one } 

    Please login or register to see this code.

    2. I think there should be room2.lamp:on

    Please login or register to see this code.

    Thanks! there were a milli000000n typos... 

    Edited by jgab
    Link to comment
    Share on other sites

    On 10/17/2018 at 9:38 AM, jgab said:

    I have a scene continuously calling Apple iCloud Find friends and posting events when family members come and go (I will post that scene later)

    Interested in this scene. 

    Link to comment
    Share on other sites

    @jgab after latest update i get this error

     

    Please login or register to see this image.

    /monthly_2018_10/20181022_09-41-25.png.b3ea1545bdac0523087e141dcbd44799.png" alt="20181022_09-41-25.png.b3ea1545bdac0523087e141dcbd44799.png" />

    Link to comment
    Share on other sites

  • Topic Author
  • 22 hours ago, jompa68 said:

    @jgab after latest update i get this error

     

    Please login or register to see this link.

    There is a new version pushed with some better error messages. If running offline the line number of where the offending script rule is defined should be shown.

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • So, in the Github repository an 'iOSLocator' scene has been uploaded. The idea is to think about scenes as services, like a service oriented architecture. Scenes can send and receive events from each other (Ah, shows my age, guess it should be microservices these days).

     

    Anyway, the iOSLocator scene is based on the EventRunnerLite framework and polls Apple's Find friends service for family members position, and if they changed location it will send events to whatever EvenRunner scene we have told it to send events to. Because it's based on the EventRunner framework the scene can be tested offline like in ZeroBrane to verify that it works.

     

    iOSLocator depends on data in the 'HomeTable' to specify people, their Apple accounts, and places of interests. In the beginning of the 'iOSLocator' file there is an example of the structure required.

    Please login or register to see this code.

    The first 'place' in the HomeTable struct should be the 'home' or base, as it is used to calculate how far away people are. Then all other places of interest. Use, e.g. google maps to get lat/long for places that are important for you. The 'list' parameter is the radius from the lat/long that defines the place.

    Note, I changed the coordinates for my home so you can't come after me if your HC2 crashes :-) 

    Then in the users list we need to list the people's iCloud credentials so we can log in.

    In the 'scenes' part of the structure, we list what scenes this service should send events to. It knows that it's name is iOSLocator so it will send events to the scene iOSClient (that has id 9 in this case).

    Every minute it will ask iCloud for the next persons location, which means that if you have 4 persons in the family it takes 4 minutes to update all members. If the iCloud server is polled too often it seems it will start to block requests.

    If a person moves outside a place an event will be sent that the person is at place "away".

    So, if we have configured the 'iOSLocator' and start it up we can put event handlers in our iOSClient scene (or whatever scene you want to receive the events to). Nothing special has to be configured in the client as the events arrive just like any other event.

    Please login or register to see this code.

    and we can start to add logic to send notifications or detect if everyone is away or if someone has come home....

    Please login or register to see this code.

    If there is some complicated calculations to do it is easier define a Lua event handler, like in the example above. EventScript rules should be used for writing short concise rules and Lua event handlers for stuff that need to trigger on events . In fact, if there is more advanced calculations like in this example it works as well to break it out into a normal Lua function as they can be called from EventScripts.

    Please login or register to see this code.

    Here checkLocation is just an ordinary Lua function helping us to keep track if someone is home or all away.

     

    A real presence algorithm would also check sensors and other possible means of detecting people inside the house. This can also be used to send notifications when children come home or arrive at school.

    Edited by jgab
    forgot homeFlag... Nicer 'who'... corrected homeFlag
    Link to comment
    Share on other sites

    So if i change this to false it should use my variable instead? 
    Did try to comment out HomeTable part from scene but then scene stops

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, jompa68 said:

    So if i change this to false it should use my variable instead? 
    Did try to comment out HomeTable part from scene but then scene stops

    Please login or register to see this code.

     

    Yes, that was the idea. if _test is false it should call fibaro:getGlobalValue(_deviceTable), and _deviceTable should be set to the name of the fibre global.

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, jompa68 said:

    Please login or register to see this code.

    So it doesn't manage to read in the HomeTable global. 

    Have you set _deviceTable at line 20 to the name of the fibaro global containing the HomeTable?

    I've pushed a new version of iOSLocator that checks the value a bit better and quits if it doesn't get a HomeTable config data.

     

    If you set _REMOTE to true and add the HC2 credentials you can run the iOSLocator scene in ZeroBrane and it will do fibaro:getGlobalValue to the HC2 and you can debug what value it gets.

    Set a breakpoint in iOSLocator on line 108 (iUsers = {}), and step through to see what happens.

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • On 10/13/2018 at 11:00 PM, petrkl12 said:

    Hi,

     

    how to start scene with parametres in your script e.g. 

    fibaro:startScene(jT.scene.HueCollorEffect,{"1", "192.168.1.245", jT.hue.HueUserZahrada, "colorloop", 254, 2}) 

     

    I have found in your wiki:

    :start. Start scene. Ex. 66:start = fibaro:startScene(66)

    but I don't know where I should place parametres {a,b,c}

     

    Thanks

     

    My script - but it doesn't work:

      Rule.eval("everyHourRun = {00:00,01:00,02:00,03:00,04:00,05:00,06:00,07:00,08:00,09:00,10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00})")
      Rule.eval("@everyHourRun & label(Zahrada.Osvetlenizahrady,'sldBrightness')~=0 => log('Run Garden Light Effect');fibaro:startScene(scene.HueCollorEffect,{'1', '192.168.1.245', hue.HueUserZahrada, 'colorloop', 254, 2})")

     

    There is another way to achieve things being scheduled at specific intervals.

    Please login or register to see this code.

    I just changed the interval command '@@' to be drift free. That means that if it starts at the second 15 past the minute it will continue on on that second for every minute and not start to drift. That means that one can use the date() command that is almost a crontab format (it compiles to a code that test the condition very effective). 

    Anyway, date('0 10-16/2') means that the test will return true on the '0' minute every second hour from 10 to 16.

    To make something on the hour the string is simply date('0 *') - * stands for any hour, the same as date('0 0-23'). Much simpler than having to list all hours with the daily '@' command.

    Crontab is very flexible, date('15,45 7-19/3 mon-fri dec,jan') means 15min and 45min past every third hour between 7 and 17 on Monday to Friday in January and December.

    The '@@00:01' will run the test every minute (exactly like crontab does) and if the date() test is true it will run the action. This is very flexible.

    The day(),wday(),month() functions are implemented with date(). Have a look at any crontab documentation on the net and you will get the gist of it.

    Edited by jgab
    Link to comment
    Share on other sites

    7 hours ago, jgab said:

    Set a breakpoint in iOSLocator on line 108 (iUsers = {}), and step through to see what happens.

    All sorted now. In my HomeTable i define scene not scenes that are setup with iOSLocator.
    Added scenes and moved iOSLocator parts to that and all working as we want when we set

    Please login or register to see this code.

    Thanks again.

    Now time for 1000m swimming before go to work, have a nice morning.

    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
    Reply to this topic...

    ×   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...