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

Posted
12 hours ago, jgab said:

Hi,

No removing handlers are not implemented (yet). And even if so you need to be able to programmatically generate the new handler.

I would recommend to have an Event pattern that triggers on all deviceIds, and then filter the correct ones inside the handler function.

Ex.

Please login or register to see this code.

then you just need to update the table triggerIDs with the current interesting deviceIDs.

 

 

after a few hours of figuring out how to filter it I got this 😅 
It works :D but I dont know the efficiency 😅

had to store the motionsensor_trigger with the ID number as index and value to get it working xD   

 

From console:

motionsensorid_trigger: {"2705":"2705"}

 

  if motionsensorid[1] ~= nil then
    Event.id='fibarosensors'
    Event_std.tagColor='Orange'
    Event {type='device', id={motionsensorid}, property='value', value='$value'}
    function Event:handler(event)
      self:debug("Sensor ID: ",event.id,"value: ",event.value)
      print("motionsensorid_trigger:", json.encode(motionsensorid_trigger))
      local var = ""..event.id..""
      print("test1 ",var)
      print("test2 ",motionsensorid_trigger[var])
 
        if motionsensorid_trigger[var] == var then
              print("---------------")
          self:debug("Sensor ID: ",event.id,"value: ",event.value,"works")
          if (event.value==true) then
              cancel_pir_timers()
                if fibaropir == 0 then
                  funpirstatuson()
                  fibaropir = 1
                end
          elseif (event.value==false) then
            fibaro_pir_check_off()
          end
            print("---------------")
        else
        return
        end  
 
    end ---function
  end  ----if not nil

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • Topic Author
  • Posted

    Please login or register to see this code.

     

    I would do something like this. It's easier if you have the keys as string in the motionsensorid_trigger table.

    Note that

    Event {type='device', property='value', value='$value'}

    triggers on all device events with value property, but we exit the handler directly if the deviceId is not in motionsensorid_trigger table...

    otherwise we go about and do what should be done...

    • Like 1
    • 2 months later...
    Posted

    I am trying to remake an outdoor light controller :D

    And checking out this event handler type:

    Event.id='_'
    Event{type='cron', time='sunrise '..offset_sunrise..' * * *'}
    function Event:handler(event)
      self:debug("sunrise with offset")
      Sunrise_event()
    end

     

    Is it a way to make it refresh the event if the offset_sunrise change? 
    It isnt any big problem, it update within the next sunrise event :)

    or should I go the quickvar way you showed me earlier if I need this to update faster? 🤔

     

     

    Posted
    On 1/18/2025 at 11:04 PM, Brors94 said:

    I am trying to remake an outdoor light controller :D

    And checking out this event handler type:

    Event.id='_'
    Event{type='cron', time='sunrise '..offset_sunrise..' * * *'}
    function Event:handler(event)
      self:debug("sunrise with offset")
      Sunrise_event()
    end

     

    Is it a way to make it refresh the event if the offset_sunrise change? 
    It isnt any big problem, it update within the next sunrise event :)

    or should I go the quickvar way you showed me earlier if I need this to update faster? 🤔

     

     

     

    @jgab

     

    Event:post {type='cron'}  this trows an error in the eventlib 

    is it possible to do an event post on the cront type to trigger the event? I tried some thing but didnt work 🤔

    Since if the event is triggered it reset with the new offset🤔 
    Then i could make a save button that disable triggers for 1 sec, then post the event and reset it with new values and then reenable the evnts again 🤔 
    Or is this a long work around? xD


    Or you got a recommandation on how to trigger on sunset/sunrise/timers with the posebility to cancel/change the timer/offset ? :D 

     

     

  • Topic Author
  • Posted

    No, cron expect a constant time pattern.

    I would recommend doing something like this

     

    Please login or register to see this code.

     

    • Thanks 1
  • Topic Author
  • Posted

    Alt.

    Please login or register to see this code.

     

    • Like 1
  • Topic Author
  • Posted

    Alt2.
    Store the offset in a permanent storage, like a fibaro GV and just restart the QA
     

    Please login or register to see this code.

     

    • Like 1
    • 2 weeks later...
    Posted (edited)

    @jgab Jan would you please check the trueFor function in eventLib?

     

    I am using it like this

     

    Please login or register to see this code.

     

    and here is the handler

     

    Please login or register to see this code.

     

    for some reason, when i change the "leftopendelay" in QA variable, and save it, the handler triggers even if the sensor is not breached. I am not totally sure, that it is happening only after re-saving or changing the delay, becuase seems to me hard to replicate, but it is happening daily..

     

    I am getting warnings about  Garage is still opened, but it is not opened.

     

    Is the trueFor function correct in the library?

     

     

    UPDATE

     

    Okay Jan, this is beyond my comprehension...

     

    The alerts are cominbg from a nonexisting quickapp...There was a quickapp, that used control my garage status.. Fortunatelly in all quickapps I always LOG all actions that are made by the quickapp to be easier to identify what is coming from where.

     

    Look at this 

    The log is coming from a device 3860..so does the false alert...

    That 3860 device was using also Evetlib..(i dont know if this is related to eventLib)

     

    Please login or register to see this image.

    /monthly_2025_02/image.png.3e9db4715ae1f72062699f118935dfee.png" />

     

    Also check the history...There is the log about that alert beeing sent..but NO DEVICE ID

     

     

    Please login or register to see this attachment.

     

    I also tried to idetify, what is that device, if it is still somehow in the system...so a tried

     

    Please login or register to see this code.

     

     

    this is what i got..

     

    image.png.700a5c5aa5537900bd31d6e42b292725.png

     

     

    What on earth is this? Any clue?

     

    There is only one thing that crossed my mind...

     

    In that deleted quickapp, i had Evetlib running...Can it be that somehow the truefor loop is stuck in the memory somehow???

    Edited by Neo Andersson
    • 3 weeks later...
    Posted (edited)

    I have stupid question, as usual.

    I would like to use eventLib, so I get first example, and it's not working (of course eventLib is included).  It means, handler was never executed, when device turns to false. Then, add dummy event - none.
    590 is "empty" binary switch - just to switch on and off.


    But "recurring timer" is working ok.
    There is something trivial here. Could you help?

    Please login or register to see this code.

     

    Update. This event:

    Please login or register to see this code.

    works. But that's all I can repair.

    Edited by Łukasz997
    Posted (edited)
    33 minutes ago, Łukasz997 said:

    I have stupid question, as usual.

    I would like to use eventLib, so I get first example, and it's not working (of course eventLib is included).  It means, handler was never executed, when device turns to false. Then, add dummy event - none.
    590 is "empty" binary switch - just to switch on and off.


    But "recurring timer" is working ok.
    There is something trivial here. Could you help?

    Please login or register to see this code.

     

    Update. This event:

    Please login or register to see this code.

    works. But that's all I can repair.

     

    try adding this on the top of the QA 🤔 

     

    local Event = Event_std
    Event.id='start'
    Event{type='QAstart'}
    function Event:handler(event)
    Event:attachRefreshstate()
    end
     

     

     

    you could also do a event like this:

     

    Event.id='_'
    Event {type='device', id=590, property='value', value='$value'}
    function Event:handler(event)
    if event.value == false then
        Print("590 is false!")
    elseif event.value == true then
        Print("590 is true!")
    end
    end

     

    Edit:

    I dont know what is most effective, but the lua code runs so fast I dont see any need to do it any other way :D 

     

    Edited by Brors94
    • Thanks 1
    Posted (edited)

    Well, after adding ...attachRefreshstate()... it starts to work.

    To be quite a truthful, I don't know what I've done. Thank you.

     

    But... a half year ago I made the similar experiment, and I can swear that the example is working ok (without above modification).

    Edited by Łukasz997
    Posted
    3 minutes ago, Łukasz997 said:

    Well, after adding ...attachRefreshstate()... it starts to work.

    To be quite a truthful, I don't know what I've done. Thank you.

     

    But... a half year ago I made the similar experiment, and I can swear that the example is working ok (without above modification).

     

    @jgab have to answer why and how it works xD But I think it is how the "evenlib" file is included into the main file :D

     

  • Topic Author
  • Posted

    Yes, eventLib is a mechanism for declaring event handlers and posting events inside your program.

    By calling Event:attachRefreshstate() it will start a listener for system refreshState events (that is in a very different format) and transform them to the event format {type=...}, mimicking the Scene sourceTrigger format, and then Event:post them to eventLib.

    The reason to make it a 2 step is that I use eventLib to structure my programs using only user posted events, not listening to refreshStates. Running a refreshState listener is a waste of cpu in that case...

    I believe it always been like this with eventLib. I may have had another event library in the past where it was always pulling in refreshState events. I think fibaroExtra does that...

     

    • Like 1
    • Thanks 1
    Posted (edited)
    W dniu 22.02.2025 o 05:33, jgab napisał:

    Yes, eventLib is a mechanism for declaring event handlers and posting events inside your program.

    I understood. And have some questions if I may:

    1. Is it possible to 'pass' events from one quickapp to another via Event:post? If true, maybe we can sent the parameters - like 'value' of the device. In turn - if this would be also true, the 'call to a library' can be implemented.

    2. Do you think about creating 'mini' enevtlib limited only to the devices? It's quite often when only this is needed but user have to include complete lib code to his project.

    3. When we have eventLib - is TriggeQA quickapp still usable or you recommend to replace it by EL?

    Maybe it's trivial, but - apologise - I have limited knowledge about event mechanism.

     

    Edited by Łukasz997
    Posted
    On 2/22/2025 at 5:33 AM, jgab said:

    Yes, eventLib is a mechanism for declaring event handlers and posting events inside your program.

    By calling Event:attachRefreshstate() it will start a listener for system refreshState events (that is in a very different format) and transform them to the event format {type=...}, mimicking the Scene sourceTrigger format, and then Event:post them to eventLib.

    The reason to make it a 2 step is that I use eventLib to structure my programs using only user posted events, not listening to refreshStates. Running a refreshState listener is a waste of cpu in that case...

    I believe it always been like this with eventLib. I may have had another event library in the past where it was always pulling in refreshState events. I think fibaroExtra does that...

     


    I think your 9. lib sourcesubscribber did that pulling in refreshstate events too :) Since if you us it on alot of QA's you get a heavy load on the cpu 🤔

  • Topic Author
  • Posted (edited)
    5 hours ago, Łukasz997 said:

    I understood. And have some questions if I may:

    1. Is it possible to 'pass' events from one quickapp to another via Event:post? If true, maybe we can sent the parameters - like 'value' of the device. In turn - if this would be also true, the 'call to a library' can be implemented.

    2. Do you think about creating 'mini' enevtlib limited only to the devices? It's quite often when only this is needed but user have to include complete lib code to his project.

    3. When we have eventLib - is TriggeQA quickapp still usable or you recommend to replace it by EL?

    Maybe it's trivial, but - apologise - I have limited knowledge about event mechanism.

     

     

    1. Yes, it's prepared for that. It won't work for a library as you would like to return values. It works as well with the standard fibaro.call for that.

    So, it's prepared but you need to add some code.

    Please login or register to see this code.

    and call it to setup a function, self:sendGlobalEvent(event) the posts the event.

    The event posted will be sent to every QA listening for refreshStateEvents, and then if they have a handler for it they will be triggered.

     

    I tested the concept in my new emulator, by starting 2 QAs, one listening and one posting events
    Looks like this:

     

    Please login or register to see this code.

     

     

    2. I'm not sure what you mean by "only to devices"?
    There is relatively little code added for events not related to devices...

    3. eventLib and  TriggerQA serves somewhat different purposes.

    TriggerQA is only for "refreshState" events and kind of mimics the sourceTrigger of Scenes. Focus on ease-of-use.

    EventLib is a more full fledged event library for more complex event handling, requiring a bit more coding skills.

    1 hour ago, Brors94 said:


    I think your 9. lib sourcesubscribber did that pulling in refreshstate events too :) Since if you us it on alot of QA's you get a heavy load on the cpu 🤔

    In general, I don't start polling for refreshState unless needed.

    However, with the "new" polling model for /refreshStates it's a little better as the request will hang until there are events available, consuming no cpu.

    On the other hand, if the system produces a lot of events all the time, there will always be events....

    Edited by jgab
    • Like 1
    • Thanks 1
  • Topic Author
  • Posted

    Not really related to EventLib, but if you want to do a library QA so you can call functions in that QA and return values, I recommend to add this little library in the QA library, and QA clients
     

    Please login or register to see this code.

    Here I call it rpc_lib and I start a client QA calling my function (in my emulator).
     

    Please login or register to see this code.

     

    You see the client calling Foo(4,6) and it's returning the value immediately, so we can do print("Sum 4+6",Foo(4,6))
    which would not be possible if we had asynchronous events return the result.

    • Thanks 1
    Posted (edited)

    Thank you, Jan, for all the explanations.

    About "little version": when I did a look at the evenLib code I saw there on example trigonometric computations. And because of that I believed, that in the library there's a lot of code not related to the device's event.

     

    Edited by Łukasz997
  • Topic Author
  • Posted
    18 minutes ago, Łukasz997 said:

    Thank you, Jan, for all the explanations.

    About "little version": when I did a look at the evenLib code I saw there on example trigonometric computations. And because of that I believed, that in the library there's a lot of code not related to the device's event.

     

    Yes, there is a function for calculating sunrise/sunset for a given time. Used when triggering on the sunset next day. (HC3 will only give us the current day)

  • Topic Author
  • Posted (edited)
    1 hour ago, jgab said:

    Not really related to EventLib, but if you want to do a library QA so you can call functions in that QA and return values, I recommend to add this little library in the QA library, and QA clients
     

    Here I call it rpc_lib and I start a client QA calling my function (in my emulator

    You see the client calling Foo(4,6) and it's returning the value immediately, so we can do print("Sum 4+6",Foo(4,6))
    which would not be possible if we had asynchronous events return the result.

     

    Just a comment about the rpc function. 

    The normal fibaro.call(deviceId,method,...) is asynchronous.

    When we call it, it returns immediately (usually nil), and the receiving QA continue to process the request in parallel.

    This is the most efficient way to call a function in another QA if we are not interested in any return value.

     

    The magic with the rpc library is that the call return with the value. It means that the QA will pause waiting for the result - there is no other way... the call are synchronous.

    However, benchmarking the above example, I get that the Foo(4,6) takes ~45ms roundtrip, which is acceptable I think.

     

    Edited by jgab
    • Like 1

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