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
  • 13 hours ago, kubo said:

    Thanks @jgab it helps me a lot.

    Now i'm trying to turn on light on motion sensor1, wait 5sec, and then if motion sensor2 is safe, turn off light. Please what is best practice to do "if" like this?

    Thanks

    Please login or register to see this code.

    First we declare 2 variables for the IDs of sensor and the light. We can use Lua global variables in rules.

    First rule says that if sensor is breached (the syntax is <ID>:<test>). :breached is a test that is true when a sensor is breached. It will then run the action :on for light1.

    It is very common that one wants to do something if something has been true (or false9 for a certain amount of time

    trueFor(<time>,<expression>) will be true when <expression> has been true for the time <tiime>.

    In the example above when sensor1 has been safe for 5s (Are you sure about 5s? it's very fast)

    The <expression> can be more complex, ex. we can test that the light is really on for that time too

    Please login or register to see this code.

     

     

    Link to comment
    Share on other sites

    11 hours ago, jgab said:

    Please login or register to see this code.

    First we declare 2 variables for the IDs of sensor and the light. We can use Lua global variables in rules.

    First rule says that if sensor is breached (the syntax is <ID>:<test>). :breached is a test that is true when a sensor is breached. It will then run the action :on for light1.

    It is very common that one wants to do something if something has been true (or false9 for a certain amount of time

    trueFor(<time>,<expression>) will be true when <expression> has been true for the time <tiime>.

    In the example above when sensor1 has been safe for 5s (Are you sure about 5s? it's very fast)

    The <expression> can be more complex, ex. we can test that the light is really on for that time too

    Please login or register to see this code.

     

     

    Yes, but what you wrote is for one sensor and light. But i thought 2 sensors with one light.
    When sensor1 is breached turn on light, and then if after 5 seconds also sensor2 is breached let light on, otherwise turn off the light.

     

    You can imagine it like corridor with crossroad where if sensor1 is breached there is 90% that then also sensor2 will be breached, but for that 10% i want to turn off the light after 5 seconds.

     

    My second question is how can i read and consume data from api?

    I am trying do something like this:

    rule("@now+00:00:10 => GetData();ConsumeData();")

    Problem is that ConsumeData is called earlier then GetData set data property. Should i do something like this different way?

    I use this code to get data. Do you have some functions to get/post data? 

     

     

    And last one, I turn on light on motion sensor breached. But in some situations i dont want to turn on light. Can I, for example on switch double click set Global variable in fibaro with post to /globalVariables, and then on motion sensor breached read this value and depends on value turn on light or not? Or is there better solution?

     

    Thanks

    Edited by kubo
    Link to comment
    Share on other sites

  • Topic Author
  • 12 hours ago, kubo said:

    Yes, but what you wrote is for one sensor and light. But i thought 2 sensors with one light.
    When sensor1 is breached turn on light, and then if after 5 seconds also sensor2 is breached let light on, otherwise turn off the light.

     

    You can imagine it like corridor with crossroad where if sensor1 is breached there is 90% that then also sensor2 will be breached, but for that 10% i want to turn off the light after 5 seconds.

    Ok, misunderstod.

    This will turn on the light if s2 is breached within 5s of s1. It will turn off lights if both s1 and s2 is safe for 3min.

    Please login or register to see this code.

    The  turn off period I was a bit unsure about.

    You can also make it direction agnostic s1->s2 or s2->s1 

    Please login or register to see this code.

     

    Quote

     

    My second question is how can i read and consume data from api?

    I am trying do something like this:

    rule("@now+00:00:10 => GetData();ConsumeData();")

    Problem is that ConsumeData is called earlier then GetData set data property. Should i do something like this different way?

    I use this code to get data. Do you have some functions to get/post data? 

     

    yes, there are http.get,http.put, http.post, http.delete. - and they are synchronous(!)

    Rules can suspend/resume while waiting for asynchronous http calls to complete. The advantage is that the rule becomes easier and other rules can run meanwhile. 

    I discovered a bug in authorization for http so you need yo upgrade to fix65.


     

    Please login or register to see this code.

     

    Note, that inside rules you can use global Lua variables/functions but not local. I would do a Lua function that creates the url given the command you want to send. There is also  way to define a Lua function that makes a synchronous request for rules - let me know if you want that instead (you could have a GetData(cmd) that returns the result synchronous)

     

     

    Quote

     

    And last one, I turn on light on motion sensor breached. But in some situations i dont want to turn on light. Can I, for example on switch double click set Global variable in fibaro with post to /globalVariables, and then on motion sensor breached read this value and depends on value turn on light or not? Or is there better solution?

    Thanks

    Not sure what you want. Fibaro globals are accessible with the name prefixed by '$'

    ex.

    Please login or register to see this code.

     

    Edited by jgab
    Link to comment
    Share on other sites

    Jan <<<<<<HC2>>>>>

    Is this a correct trigger?

    If knopZolder has been on between 22:00..07:00 hour and than that button is switched off => alarmPlug switched off

    Please your valueable advice

    Please login or register to see this code.

    Thanks,

    //Sjakie

    Link to comment
    Share on other sites

    4 hours ago, jgab said:

    Ok, misunderstod.

    This will turn on the light if s2 is breached within 5s of s1. It will turn off lights if both s1 and s2 is safe for 3min.

    Please login or register to see this code.

    The  turn off period I was a bit unsure about.

    You can also make it direction agnostic s1->s2 or s2->s1 

    Please login or register to see this code.

     

     

    yes, there are http.get,http.put, http.post, http.delete. - and they are synchronous(!)

    Rules can suspend/resume while waiting for asynchronous http calls to complete. The advantage is that the rule becomes easier and other rules can run meanwhile. 

    I discovered a bug in authorization for http so you need yo upgrade to fix65.


     

    Please login or register to see this code.

     

    Note, that inside rules you can use global Lua variables/functions but not local. I would do a Lua function that creates the url given the command you want to send. There is also  way to define a Lua function that makes a synchronous request for rules - let me know if you want that instead (you could have a GetData(cmd) that returns the result synchronous)

     

     

    Not sure what you want. Fibaro globals are accessible with the name prefixed by '$'

    ex.

    Please login or register to see this code.

     

    Thank you very much @jgab, you show me the correct way.

    Now i do some test with post, publish/subscribe, but i have a problem.

    When i do post("#test, now+00:00:30"), it is planned for weird time: now is 13:02, Posting {"type":"test"} at [Timer:02:05:44].

    and when i try to use publish:
    rule("subscribe(#test2)")

    rule("#test2 => log('catched test2 event');")

    rule("52:isOn => log('publish');publish(#test2);")

     

    It logs No subscriber for {"type":"test2"}, please what i am doing wrong? Sorry for these basic questions, i try to search in this topic, but i can't figure it out, how to use it properly.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 2 hours ago, kubo said:

    Thank you very much @jgab, you show me the correct way.

    Now i do some test with post, publish/subscribe, but i have a problem.

    When i do post("#test, now+00:00:30"), it is planned for weird time: now is 13:02, Posting {"type":"test"} at [Timer:02:05:44].

    and when i try to use publish:
    rule("subscribe(#test2)")

    rule("#test2 => log('catched test2 event');")

    rule("52:isOn => log('publish');publish(#test2);")

     

    It logs No subscriber for {"type":"test2"}, please what i am doing wrong? Sorry for these basic questions, i try to search in this topic, but i can't figure it out, how to use it properly.

     

    The time format is wrong for post.

    If the time is a number that is smaller that os.time() then it's considers number of seconds from now.

    If it's a number > os.time() then it's considered to be absolute time.

    'now' is a variable that is the number of seconds since midnight and thus < os.time() - and then you add 30s to it... so that's going to be wrong.

     

    The way to do it is to use the time constant format

    +/00:00:03

    post(#test,+/00:00:30) - post event 39s from now

    which is the same as post(#test,ostime()+00:00:30) but shorter

    If you want to post at exact time during the day you can use the format 

    t/15:00

    post(#test,t/15:00)

    will post at exact 15:00.

    Another useful time format is 

    n/15:00

    post(#test,n/15:00)

    it will post at 15:00 if the current time is 15:00 or less. If the time has passed 15:00, it will post the event next day at 15:00.

     

    Then the publish/subscribe thingy - it seems to be broken for some reason - I will have a look into it. It was more common in the past on the HC2 but these days most of my QAs communicate with fibaro.calls between them. Anyway, it's a useful function and I will fix it.

    Link to comment
    Share on other sites

  • Topic Author
  • 4 hours ago, Sjakie said:

    Jan <<<<<<HC2>>>>>

    Is this a correct trigger?

    If knopZolder has been on between 22:00..07:00 hour and than that button is switched off => alarmPlug switched off

    Please your valueable advice

    Please login or register to see this code.

    Thanks,

    //Sjakie

    The parenthesis are unnecessary

    22:00..07:00 & appPhone.knopZolder:isOn & !appPhone.knopZolder:isOn & appPhone.alarmPlug:isOn

    The time test (22:00..07:00) will work as a filter only allowing the rule to be true between 22:00 and 07:00

    The time test will also trigger the rule at 22:00 because if the other switches/buttons are on at 21:59:59 and it turns 22:00 the rule will run and all conditions are true.

    The rule will not remember if knopZolder has been turned on and off in that time period. The only thing that matters is that all buttons are :isOn in the period 22:00..07:00.

    If you need to remember if knopZolder been on you need to save that in a variable or something.

    Link to comment
    Share on other sites

    1 hour ago, jgab said:

    The time format is wrong for post.

    If the time is a number that is smaller that os.time() then it's considers number of seconds from now.

    If it's a number > os.time() then it's considered to be absolute time.

    'now' is a variable that is the number of seconds since midnight and thus < os.time() - and then you add 30s to it... so that's going to be wrong.

     

    The way to do it is to use the time constant format

    +/00:00:03

    post(#test,+/00:00:30) - post event 39s from now

    which is the same as post(#test,ostime()+00:00:30) but shorter

    If you want to post at exact time during the day you can use the format 

    t/15:00

    post(#test,t/15:00)

    will post at exact 15:00.

    Another useful time format is 

    n/15:00

    post(#test,n/15:00)

    it will post at 15:00 if the current time is 15:00 or less. If the time has passed 15:00, it will post the event next day at 15:00.

     

    Then the publish/subscribe thingy - it seems to be broken for some reason - I will have a look into it. It was more common in the past on the HC2 but these days most of my QAs communicate with fibaro.calls between them. Anyway, it's a useful function and I will fix it.

    Thanks for time explanation. Can you please provide some basic example of calls between two QA's?

    Link to comment
    Share on other sites

  • Topic Author
  • 2 hours ago, kubo said:

    Thanks for time explanation. Can you please provide some basic example of calls between two QA's?

    The easiest way is to use the remote command

    remote(<QA ID>,<event>)

    Ex. Client ID sending an event to another ER4 QA to ask it to sum 2 values

    Please login or register to see this code.

    Server receiving the request , computing the sum and answear back

    Please login or register to see this code.

     

    The remote command will add an extra field to the event ._from with the ID of the sending QA.  That field can be used to send an event back to the QA - In this example the Server QA will send the answer back to whatever QA asked.

     

    The drawback is of course that the "client" need to know the ID of the Server QA. The publish/subscribe solves that by any subscriber getting the event from any publisher.

    You can lookup the QA id by name to make it a little less "fragile" if the server QA is reinstalled and gets a new id. 

    ex.

    Please login or register to see this code.

     

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • This event mechanism that is the base of EventRunner is really powerful.

    First all HC3 events like devices and globals changing values (almost everything that happens in the HC3 generates an internal event that can be picked up with /refreshStates)

    ...all these events are lifted up as ER events, which allow us to write rules that trigger on them.

    A binary sensor 88 that is breached sends an event {type='device', id=88, property='value', value=true, old=false}

    and we can trigger a rule on that with

    Please login or register to see this code.

    rules also allow us to write it as 

    Please login or register to see this code.

    but the underlying code is the same.

     

    But we can also post our own events - to the same QA with post() or to other QAs with remote().

    Posting to our own rules is almost like calling subroutines as events can carry arguments like the #sum event in the previous example.

     

    We can do loops easily by

    Please login or register to see this code.

    This will log Ding! every minute

    We can do loops that could down, 

    Please login or register to see this code.

     

    It's easy to pickup events in Lua 


     

    Please login or register to see this code.

     

     

     

    Edited by jgab
    Link to comment
    Share on other sites

    On 7/12/2021 at 1:00 PM, jgab said:

    Please login or register to see this code.

    First we declare 2 variables for the IDs of sensor and the light. We can use Lua global variables in rules.

    First rule says that if sensor is breached (the syntax is <ID>:<test>). :breached is a test that is true when a sensor is breached. It will then run the action :on for light1.

    It is very common that one wants to do something if something has been true (or false9 for a certain amount of time

    trueFor(<time>,<expression>) will be true when <expression> has been true for the time <tiime>.

    In the example above when sensor1 has been safe for 5s (Are you sure about 5s? it's very fast)

    The <expression> can be more complex, ex. we can test that the light is really on for that time too

    Please login or register to see this code.

     

     

    Thanks for detailed explanation. 

    At my HC3, QA sometimes restarts automatically. Is it normal, or it's just me? Can it be because i have testing QA, and there is some error (no error in logs), which cause QA restart?

    I have this problem: i started to water the garden ("long term job"), during watering QA restarts, and then "finish" event wasn't fired. 

    I thought that with events (post) i resolve this issue, but they live in context of QA, so with QA restart planned events are discarded.

    Is something to resolve this issue?

    Link to comment
    Share on other sites

  • Topic Author
  • 4 hours ago, kubo said:

    Thanks for detailed explanation. 

    At my HC3, QA sometimes restarts automatically. Is it normal, or it's just me? Can it be because i have testing QA, and there is some error (no error in logs), which cause QA restart?

    I have this problem: i started to water the garden ("long term job"), during watering QA restarts, and then "finish" event wasn't fired. 

    I thought that with events (post) i resolve this issue, but they live in context of QA, so with QA restart planned events are discarded.

    Is something to resolve this issue?

    Is the ER4 QA that restarts? 

    If it's not generating an error message when it's crashing it's a bit strange and something I would like to understand/fix, because it's nothing that I have observed myself for a very long time.

     

    It does restart when autoupdating depending on what time it's scheduled too (default rule 05:00)

     

    One problem when restarting the QA (even by hand) is events posted in the future as they will be killed.

    Ex,

    Please login or register to see this code.

    If the QA is restarted after 07:00 then we will never get the #end event.

    If we change the post time to an absolute time we could use a catchup rule

    Please login or register to see this code.

    Here we make 07:00 a catchup that means that it will trigger when starting up if the time is after 07:00

    We also guard it so it won't run unless it's between 07:00 and 08:00

    Then we post the #end event at absolute time 08:00

     

    However, if it was fixed times I would just make it 2 timed events. If it restarts between 07:00 and 08:00 it doesn't matter.

    Please login or register to see this code.

     

    If the times are calculated you need to store them somewhere - and that could be a fibaro global (I'm implementing support for quickAppVariables, will probably be $$<name>)

    Please login or register to see this code.

     

    So, in general one should craft the rules so that they can withstand a restart. Also because it's common to manually restart the QA because some rule is adjusted or added.

    It may require some planning when writing the rules.

    One way is catchup rules to make sure that timed rules are triggering so that we arrive at a known state when the QA start. Ex. lights that should turn on or off in the morning or night. 

    Other things one can do is to query the environment to understand what "state" it is when the QA starts up. Ex. TimeOfDay (like morning, lunch, evening, night) that is common to use I always calculate when the QA starts up and I don't even store it in a fibaro global as it is only used in the ER4 QA handling time scheduling.

    Link to comment
    Share on other sites

    Jan,

    regret to inform you last night around midnight 2 QA's restarted 1301 and the other I cant remember.

    Yesterday afternoon also a restart.

    In both cases I was not behind my computer.

    In degug I dont see any strange things.

    I thought all my errors where gone but this morning I spot around 8 pieces so I was  a bit surprised

    Can it be related with fix 65? Since when we have fix 65? Is it possible to add the date update behind the version?

    Thanks for your excellent ER4 and service,

    Regards,

    //Sjakie

    Link to comment
    Share on other sites

    This morning this happened to the ER4:

     

    14.07.2021] [04:18:03] [DEBUG] [QUICKAPP96]: fibaro.call(119,"turnOff") => nil
    [14.07.2021] [05:00:00] [TRACE] [QUICKAPP96]: New file version:EventRunner4Engine.lua - v0.5fix65
    [14.07.2021] [05:00:00] [DEBUG] [QUICKAPP96]: 1 files needs to be updated
    [14.07.2021] [05:00:00] [DEBUG] [QUICKAPP96]: 0 files needs to be added
    [14.07.2021] [05:00:00] [DEBUG] [QUICKAPP96]: 0 files needs to be deleted
    [14.07.2021] [05:00:00] [DEBUG] [QUICKAPP96]: Updating EventRunner
    [14.07.2021] [05:01:00] [ERROR] [QUICKAPP96]: QuickApp crashed
    [14.07.2021] [05:01:00] [ERROR] [QUICKAPP96]: Unknown error occurred:
    [14.07.2021] [05:02:00] [ERROR] [QUICKAPP96]: QuickApp crashed
    [14.07.2021] [05:02:00] [ERROR] [QUICKAPP96]: Unknown error occurred:
    [14.07.2021] [05:03:00] [ERROR] [QUICKAPP96]: QuickApp crashed
    [14.07.2021] [05:03:00] [ERROR] [QUICKAPP96]: Unknown error occurred:
    [14.07.2021] [05:04:00] [ERROR] [QUICKAPP96]: QuickApp crashed
    [14.07.2021] [05:04:00] [ERROR] [QUICKAPP96]: Unknown error occurred:
    [...]

    [14.07.2021] [09:17:00] [ERROR] [QUICKAPP96]: QuickApp crashed
    [14.07.2021] [09:17:00] [ERROR] [QUICKAPP96]: Unknown error occurred:

     

    What can I do?

    Link to comment
    Share on other sites

    34 minutes ago, jwi said:

    What can I do?

    Have restarted the HC3 services and it's OK again.

    Link to comment
    Share on other sites

  • Topic Author
  • 2 minutes ago, jwi said:

    Have restarted the HC3 services and it's OK again.

    Well, that's worrying... ;-) much better if it was a bug we could fix.

    I looked through the fix65 and there is nothing that could cause a crash in there. 

    If you have issues you can go to the forum and the first post on EventRunner4 and download the EventRunner4.fqa file there.

    Then upload it to the hc3 and copy the files by hand from the uploaded version to the existing version - thus updating all the code.

    If you don't care if the QA changes id you can just copy the main file from the old QA to the uploaded version and use the uploaded QA.

     

    I'm away on vacation now so I can't test on my HC3 - only on the emulator. Back this weekend.

     

    fix65 was uploaded yesterday, July 13.

    Link to comment
    Share on other sites

    24 minutes ago, jgab said:

    Well, that's worrying... ;-) much better if it was a bug we could fix.

    I looked through the fix65 and there is nothing that could cause a crash in there. 

    If you have issues you can go to the forum and the first post on EventRunner4 and download the EventRunner4.fqa file there.

    Then upload it to the hc3 and copy the files by hand from the uploaded version to the existing version - thus updating all the code.

    If you don't care if the QA changes id you can just copy the main file from the old QA to the uploaded version and use the uploaded QA.

     

    I'm away on vacation now so I can't test on my HC3 - only on the emulator. Back this weekend.

     

    fix65 was uploaded yesterday, July 13.

     

    Thanks Jan for the hint, I think that was just an HC3 problem because when I tried to change something,

    I immediately got an HC3 error with the usual, Bad gateway, refresh the page, restart services, reboot HC.

    Anyway, I wish you a pleasant vacation.

    Link to comment
    Share on other sites

  • Topic Author
  • v0.5fix67.

    • quickAppVariables are supported. Prefix name with '$$'
      Ex.
      rule("@sunset => $$myVar = 99")
    • publish/subscribe works again - I hope.
    • Thanks 1
    Link to comment
    Share on other sites

    About the crashes last night and this morning.  Can it be related to Fibaro somehow ?  as i had some 

     

    Please login or register to see this image.

    /monthly_2021_07/image.png.ecbea018b1b814e12e10586d2292d5f8.png" />

     

    which have no other explanation.

     

     

     

     

    Link to comment
    Share on other sites

    22 minutes ago, Momos said:

    About the crashes last night and this morning.  Can it be related to Fibaro somehow ?  as i had some 

     

    Please login or register to see this link.

     

    which have no other explanation.

     

     

     

     

     

    I don't think it could be a coincidence. These services run on the HC

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