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

Guest kallecux

 

Hello!

 

I read with great interest some posts and the wiki on GitHub, but I do not find the perfect entry yet.

I think the system is very good - I think I can compress some scenes considerably!

 

I have the following beginner questions:

 

1. How can I turn on a lamp for a certain amount of time when move ist detected?

2. Why is there a difference between Rule and Rule.eval?

 

Thank you in advance!

Karl Heinz

Link to comment
Share on other sites

8 minutes ago, kallecux said:

 

Hello!

 

I read with great interest some posts and the wiki on GitHub, but I do not find the perfect entry yet.

I think the system is very good - I think I can compress some scenes considerably!

 

I have the following beginner questions:

 

1. How can I turn on a lamp for a certain amount of time when move ist detected?

2. Why is there a difference between Rule and Rule.eval?

 

Thank you in advance!

Karl Heinz

Hello Karl Heinz,

here is an example how you can do it.

Please login or register to see this code.

Remember to add 111 and 222 to header in scene.

Your second question is simple, rule is short name for rule.eval. You set this value in the beginning of function main and then you can user rule instead of rule.eval

Please login or register to see this code.

 

Link to comment
Share on other sites

  • Topic Author
  • 3 hours ago, kallecux said:

     

    Hello!

     

    I read with great interest some posts and the wiki on GitHub, but I do not find the perfect entry yet.

    I think the system is very good - I think I can compress some scenes considerably!

     

    I have the following beginner questions:

     

    1. How can I turn on a lamp for a certain amount of time when move ist detected?

    2. Why is there a difference between Rule and Rule.eval?

     

    Thank you in advance!

    Karl Heinz

     

    Hi @kallecux

    The EventRunner framework provides a lot of options and most tasks can be solved many ways.

    You are right that one can often combine many scenes into one ER (EventRunner) scene. I have one main scene and a few helper scenes.

     

    So, there are 2 things going on. One is to write scenes with Lua that adhere to the event model provided by the EventRunner framework.

    The other approach is to write "rules" in something we call EventScript - and that is what people mostly discuss in this forum thread.
     EventScript is a "programming" language made up by me to make it easy to support many typical home automation tasks.

    EventScripts are strings given to the function Rule.eval(<string>) that compile the string to an efficient representation iof the rule for later execution when the rule is triggered by devices or other events.

     

    2. Rule.eval() is a function belonging to a class of functions available under Rule.*. To make it easier to write functions we often define a shorthand for Rule.eval, called 'rule'

    Please login or register to see this code.

     

    1.  To turn on a lamp for a certain amount of time when a motion sensor is detected we can write like this 

    (I just saw that @jompa68 answered you question with a bit more advanced example with many sensors and lights)

    Please login or register to see this code.

    when the lamp is breached we turn on the lamp, wait 5min and then  turn off the lamp.

    More clever is that we would like to turn off the lamp when the sensor has been "safe" for 5min

    Please login or register to see this code.

    Here we have 2 rules. One that turns the lamp on if sensor is breached. 

    The other that monitors the expression "motion:safe", and if it is true for 5min executes the action "lamp:off"

     

    These rules can be much more complex and if you have use-cases you want to solve but can't figure out how, please post in this thread and ask.

    The documentation on Github is ok but not completely up to date as the evolution of the framework is quite rapid and most of the discussions and announcements are here in the Fibaro forum.

     

    Regards /JanG

    Link to comment
    Share on other sites

  • Topic Author
  • 5 hours ago, petrkl12 said:

    @jgab

    it looks that this rule doesn't work:

    Please login or register to see this code.

     

     

    Oops, when I rewrote the engine I forgot to add it. And I realised that I don't use any trigger variables in my scenes now so I didn't miss it. 

    I have pushed v3, B33 with a fix for it.

    However, it's a bit differently expressed now. I did not like the '_' underscore notation as it reminds more of variables that should be ignored according to Lua practices.

    Now instead you have to explicitly declare the variable as a "trigger variable" and it can have any name. Of course they can still start with '_' if you like it...

    Please login or register to see this code.

    Util.defTriggerVar(<var name>[,<expression>]) takes an optional initialisation value that doesn't cause a trigger at declaration time. In any rule executed later that the declared trigger variable is assigned a new value it will create a trigger event of typ #variable{name=<name>, value=<value>}

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • I realised that I just checked in a version with more extensive automatic conversion of values for fibaro globals inside ER rules.

    Values are always stored as strings in fibaro globals, but when we access them from ER rules we automatically convert them back to bools/numbers/tables..

    Please login or register to see this code.

    The last one can trip up your code. If you have json tables stored in fibaro globals (like HomeTables) they will be automatically json.decoded when you access them. If you now call json.decode on them you will get an error as they are already decoded for you.

    However, this is only inside ER rules and does not affect calls to fibaro:getGlobal.

    At the moment rule("$myVar[2] == 4") is a bit expensive as the value in $myVar is converted from string to table at every access. However, it is possible in some cases to cache that value and I will implement it in a future release. (on the other hand, json.decode is surprisingly fast on the HC2, at least for moderately sized tables)

     

    Edited by jgab
    Link to comment
    Share on other sites

    49 minutes ago, jgab said:

     

    Oops, when I rewrote the engine I forgot to add it. And I realised that I don't use any trigger variables in my scenes now so I didn't miss it. 

    I have pushed v3, B33 with a fix for it.

    However, it's a bit differently expressed now. I did not like the '_' underscore notation as it reminds more of variables that should be ignored according to Lua practices.

    Now instead you have to explicitly declare the variable as a "trigger variable" and it can have any name. Of course they can still start with '_' if you like it...

    Please login or register to see this code.

    Util.defTriggerVar(<var name>[,<expression>]) takes an optional initialisation value that doesn't cause a trigger at declaration time. In any rule executed later that the declared trigger variable is assigned a new value it will create a trigger event of typ #variable{name=<name>, value=<value>}

    OK, Thanks.

     

    Is it possible to use mass definition? i.e.

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • Just now, petrkl12 said:

    OK, Thanks.

     

    Is it possible to use mass definition? i.e.

     

    Please login or register to see this code.

     

    Next release maybe ;-)

    Problem is the initialisation value. Same for all vars? or list of value per variable?

    For now you can do

    Util.map(Util.defTriggerVar,{roomMotion1,roomMotion2,roomMotion3}) to declare and set all to nil

    Util.map(function(n) Util.defTriggerVar(n,0) end,{roomMotion1,roomMotion2,roomMotion3}) to declare and set all to 0

    Util.mapkl(function(n,v) Util.defTriggerVar(n,v) end,{roomMotion1=0,roomMotion2=1,roomMotion3=0}) to declare and set all to individual values

    Link to comment
    Share on other sites

    11 minutes ago, jgab said:

    Next release maybe ;-)

    Problem is the initialisation value. Same for all vars? or list of value per variable?

    For now you can do

    Util.map(Util.defTriggerVar,{roomMotion1,roomMotion2,roomMotion3}) to declare and set all to nil

    Util.map(function(n) Util.defTriggerVar(n,0) end,{roomMotion1,roomMotion2,roomMotion3}) to declare and set all to 0

    Util.mapkl(function(n,v) Util.defTriggerVar(n,v) end,{roomMotion1=0,roomMotion2=1,roomMotion3=0}) to declare and set all to individual values

     

    Thanks, only same value for all could be OK

    Link to comment
    Share on other sites

    @jgab

    error in :

    Please login or register to see this code.

     

    table index is nil

    here:

     function self.defTriggerVar(var,expr) _triggerVars[var]=true; self.defvar(var,expr) end

     

    and this also doesn't work:

    Util.map(Util.defTriggerVar,{roomMotion1,roomMotion2,roomMotion3})

     

    Link to comment
    Share on other sites

  • Topic Author
  • Sorry, too early in the morning. Need name strings of variables.

    Please login or register to see this code.

    and

    Please login or register to see this code.

     

    13 hours ago, jompa68 said:

    Hello Karl Heinz,

    here is an example how you can do it.

    Please login or register to see this code.

    Remember to add 111 and 222 to header in scene.

    Your second question is simple, rule is short name for rule.eval. You set this value in the beginning of function main and then you can user rule instead of rule.eval

    Please login or register to see this code.

     

     

    and it should be

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    @jgab Thanks!

     

    Now all my ER scenes are converted to version 3. Thanks a lot!

     

    Your framework is number one!

     

    And Fibaro should hire you to help them create the best system for home automation !

     

     

    • Like 2
    Link to comment
    Share on other sites

    Guest kallecux

    I have many Hue-Lamps and i tried the sample above. (Light on for x minutes when movement is detected).

     

    Whe i use a Hue-Lamp i get the error:

     

    [DEBUG] 14:08:08: Error in 'Rule:4[trueFor(00:01,Bewegung_Wohnzimmer:safe & Schreibtisch:isOn) => Schreibtisch:off;...]': /opt/fibaro/scenes/69.lua:1581: fibaro:get(83,"value"),/opt/fibaro/scenes/69.lua:533: ID:83 does not support property 'value'

     

    I think this is because the Hue-Lamp has no property "value". Is there an other way to control the hue lamp?

    There is an experimental hue-support....ist ER3.

     

    There is another question: 

    Why are in this rule two brackets "[[   ]]" at the begin and the end?

    rule([[trueFor(00:10,basement_move:safe & lights:isOn) => lights:off; log('Turning off lights after 10min of inactivity basement')]])

    When i read other examples there are no brackets...

     

    Sunny greetings!

    Karl Heinz 

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, kallecux said:

    I have many Hue-Lamps and i tried the sample above. (Light on for x minutes when movement is detected).

     

    Whe i use a Hue-Lamp i get the error:

     

    [DEBUG] 14:08:08: Error in 'Rule:4[trueFor(00:01,Bewegung_Wohnzimmer:safe & Schreibtisch:isOn) => Schreibtisch:off;...]': /opt/fibaro/scenes/69.lua:1581: fibaro:get(83,"value"),/opt/fibaro/scenes/69.lua:533: ID:83 does not support property 'value'

     

    I think this is because the Hue-Lamp has no property "value". Is there an other way to control the hue lamp?

    There is an experimental hue-support....ist ER3.

     

    There is another question: 

    Why are in this rule two brackets "[[   ]]" at the begin and the end?

    rule([[trueFor(00:10,basement_move:safe & lights:isOn) => lights:off; log('Turning off lights after 10min of inactivity basement')]])

    When i read other examples there are no brackets...

     

    Sunny greetings!

    Karl Heinz 

     

    The two brackets "[[   ]]" is used for Lua strings that can span over multiple lines. Makes formatting of long rules nicer.

    Please login or register to see this code.

    Yes, ER has it's own support for Hue lamps and you need to set them up.

    In the beginning of the scene do 

    Please login or register to see this code.

    <HueUserName> is the Hue key  and <IP> id the ip address to the Hue hub "192.18.1.xx"

    Then you need to assign them deviceID, i.e. numbers. To do that you need to know the name of the lamp in the Hue app.

    If you have setup _HueHubs you can do Hue.dump() to get a list of all the devices registered with the Hue hub.

     

    So, if you have a Hue lamp with the name "Schreibtisch" you do

    Please login or register to see this code.

    By doing that you have access to the light with fibaro:call/fibaro:get and can do things like

    Please login or register to see this code.

    and they behave almost like any other fibaro light, in a nicer and more compatible way then Fibaro's own implementation (that's what I think at least).

    You can also use them inside ER rules like ordinary lights. However, it's good to have a variable defined as 12000 in this case or have {Schreibtisch=12000} in a "hometable".

    Please login or register to see this code.

    One issue is that we would like to get events if the light changes state - like we get triggers when Z-wave device changes state. Unfortunately Hue doesn't support that so we need to poll the light with regular intervals to see if it changed state. We can do that with the Hue.monitor command. It will monitor the Hue light in the background and if it changes state it will post a trigger that we react to.

    Please login or register to see this code.

    In this case it allows us to have an up to date state of the Schreibtisch lamp so that the :isOn command returns a correct value.

    ER rules can access Lua global variables so we can do "Schreibtisch=12000" instead of "rule('Schreibtisch=12000')" and still access 'Schreibtisch' inside ER rules. However, it's usually better to keep them in a "hometable" if you have many identifiers.

    Here 'Bewegung_Wohnzimmer' is assumed to be a normal Z-Wave sensor otherwise we need to monitor that sensor too.

    In general, if you don't need triggers from the Hue device you don't need to monitor it (it takes some resources and generates traffic towards the Hue hub).

    Do you have many Hue hubs? In that case you can add them to the _HueHubs list.

     

    Here are some more info

     

     

    Edited by jgab
    Link to comment
    Share on other sites

    Guest kallecux

    @jgab

    Thank you for the fast response!

     

    Now im "playing" with the Hometable and i read this topic from AutoFrank

    Tutorial - Using a Hometable to store device and scene ID's

    but i get this error in ER3

     

     

    [DEBUG] 16:53:37: Demo - EventRunner v3.0 B33

    [DEBUG] 16:53:37: Fibaro software version: 4.550

    [DEBUG] 16:53:37: HC2 uptime: 0 hours

    [DEBUG] 16:53:37: fibaro:getValue(1,"sunriseHour")="05:43"

    [DEBUG] 16:53:37: fibaro:getValue(1,"sunsetHour")="21:18"

    [DEBUG] 16:53:37: Sunrise 05:43, Sunset 21:18

    [DEBUG] 16:53:37: Hue system inited (experimental)

    [DEBUG] 16:53:37: 

    [DEBUG] 16:53:37: Loading rules

    [DEBUG] 16:53:37: Error loading rules:/opt/fibaro/scenes/69.lua:915: bad argument #1 to 'pairs' (table expected, got nil)
     

    Is there an other tutorial to create the right HomeTable?

     

    Thanks

    Karl Heinz 

     

    Link to comment
    Share on other sites

    1 hour ago, kallecux said:

    @jgab

    Thank you for the fast response!

     

    Now im "playing" with the Hometable and i read this topic from AutoFrank

    Tutorial - Using a Hometable to store device and scene ID's

    but i get this error in ER3

     

     

    [DEBUG] 16:53:37: Demo - EventRunner v3.0 B33

    [DEBUG] 16:53:37: Fibaro software version: 4.550

    [DEBUG] 16:53:37: HC2 uptime: 0 hours

    [DEBUG] 16:53:37: fibaro:getValue(1,"sunriseHour")="05:43"

    [DEBUG] 16:53:37: fibaro:getValue(1,"sunsetHour")="21:18"

    [DEBUG] 16:53:37: Sunrise 05:43, Sunset 21:18

    [DEBUG] 16:53:37: Hue system inited (experimental)

    [DEBUG] 16:53:37: 

    [DEBUG] 16:53:37: Loading rules

    [DEBUG] 16:53:37: Error loading rules:/opt/fibaro/scenes/69.lua:915: bad argument #1 to 'pairs' (table expected, got nil)
     

    Is there an other tutorial to create the right HomeTable?

     

    Thanks

    Karl Heinz 

     

    I have done like this,.

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, kallecux said:

    @jgab

    Thank you for the fast response!

     

    Now im "playing" with the Hometable and i read this topic from AutoFrank

    Tutorial - Using a Hometable to store device and scene ID's

    but i get this error in ER3

     

     

    [DEBUG] 16:53:37: Demo - EventRunner v3.0 B33

    [DEBUG] 16:53:37: Fibaro software version: 4.550

    [DEBUG] 16:53:37: HC2 uptime: 0 hours

    [DEBUG] 16:53:37: fibaro:getValue(1,"sunriseHour")="05:43"

    [DEBUG] 16:53:37: fibaro:getValue(1,"sunsetHour")="21:18"

    [DEBUG] 16:53:37: Sunrise 05:43, Sunset 21:18

    [DEBUG] 16:53:37: Hue system inited (experimental)

    [DEBUG] 16:53:37: 

    [DEBUG] 16:53:37: Loading rules

    [DEBUG] 16:53:37: Error loading rules:/opt/fibaro/scenes/69.lua:915: bad argument #1 to 'pairs' (table expected, got nil)
     

    Is there an other tutorial to create the right HomeTable?

     

    Thanks

    Karl Heinz 

     

     

    Well, what you can start with is to define a hometable in the ER scene, like the example table that is provided.

    The advantage with storing it in a fibaro global variable is that other scenes can also use it. However, start by defining on by hand in the main() of the ER scene and you can later save it to a fibaro global if you need it. Something like below. I don't know how you want to structure it but rooms and devices are usually ok.

    This also allows easier names per device as you can have both bedroom.lamp and kitchen.lamp.

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    Guest kallecux

     

    Thank you for the hint, but i am looking for the LUA-Script which fills a global to read it in ER3 for that:

     

    --or read in "HomeTable" from a fibaro global variable (or scene)
    local HT = type(_homeTable)=='number' and apiget("/scenes/".._homeTable).lua or fibaro:getGlobalValue(_homeTable) 
    HT = type(HT) == 'string' and json.decode(HT) or HT
     

    Greetings!

    Link to comment
    Share on other sites

  • Topic Author
  • 7 minutes ago, jompa68 said:

    any improvments on this code @jgab? Possible to use in v3?

     

     

    It should be compatible with v3. 

    No improvements - still as complicated as before :-) 

    This is a way to code using a "state machine", maybe I can come up with some syntax support for rules to make it easier to write. Will think about it.

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