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


Question

Posted (edited)

Hello guys

 

Maybe this is an absolute stupid question, but i will give it a try

 

Is there any possibility in LUA, to create my own event, that will fire, when a certain variable's value has changed ? Like ex i have a variable AGE, and its initial value is 10. Whenever, wherever this AGE variable got a new value, i would like to trigger an event, so I can do some action upon that event.

 

Is this too much that I want?

 

Big thanks for any help

 

 

 

 

 

 

 

 

Edited by Neo Andersson

6 answers to this question

Recommended Posts

  • 0
Posted (edited)
1 hour ago, Neo Andersson said:

Hello guys

Maybe this is an absolute stupid question, but i will give it a try

Is there any possibility in LUA, to create my own event, that will fire, when a certain variable's value has changed ? Like ex i have a variable AGE, and its initial value is 10. Whenever, wherever this AGE variable got a new value, i would like to trigger an event, so I can do some action upon that event.

Is this too much that I want?

Big thanks for any help

 

Is the variable a Lua variable or a fibaro global. Fibaro globals already emit events when they change value.

You can create custom events and fire them

Please login or register to see this code.

 

Edited by jgab
  • 0
  • Inquirer
  • Posted
    1 minute ago, jgab said:

     

    Is the variable a Lua variable or a fibaro global. Fibaro globals already emit events when they change value.

    You can create custom events and fire them

    Please login or register to see this code.

     

    It is a LUA variable, not fibaro global

     

    Maybe I explained mayself incorreclty...my goal is, to automatically get that event to be fired..

    like when a device state is changed we can listen to that in a function, and do some stuff..

     

    • 0
    Posted

    You can always fire a customEvent when your change the value of the variable - however, it's difficult to add the value to the customEvent in a safe way. One unsafe way is to encode the value in the events userDescription. 

    • 0
  • Inquirer
  • Posted (edited)

    Probably its my fault..I didnt explain correclty what is my goal.

    I dont want to fire the event myself..

    I want to fire that event by Fibaro, whenever the value changes.

    Like we have with devices

     

    Please login or register to see this code.

    The function above allowes me to do an action when device with ID 88 changes its value

    Something similar can be done?

     

     

    I have more thousends of code in one of my QA. In many many places, when AGE variable gets a new value, I need to update some UI element. So every time I need to write self:updateVeiw("lbl","text",age)

    If there was the possibility, to listen for thet value change, then I could call only once the updateView function, and not write it like 500 times

    Maybe my aproach is totally incorrect, sorry for that

    Edited by Neo Andersson
    • 0
    Posted (edited)

    Ok, instead of doing 

    AGE=x

    in 500 places, you could do 

    Please login or register to see this code.

    and then

    Please login or register to see this code.

    500 times.

     

    If AGE is a global variable you could do

    Please login or register to see this code.

     

    The advantage is that you don't need to change the code - use AGE=x. You can also check several variables with the same loop and have separate handlers for them. Disadvantage is that if AGE changes values quickly it won't show, however, you will have the latest 1s value which is probably not a problem for the UI.

    Could make it every 2000ms if it's not a realtime value.

    Edited by jgab
    • 0
  • Inquirer
  • Posted
    5 hours ago, jgab said:

    Ok, instead of doing 

    AGE=x

    in 500 places, you could do 

    Please login or register to see this code.

    and then

    Please login or register to see this code.

    500 times.

     

    If AGE is a global variable you could do

    Please login or register to see this code.

     

    The advantage is that you don't need to change the code - use AGE=x. You can also check several variables with the same loop and have separate handlers for them. Disadvantage is that if AGE changes values quickly it won't show, however, you will have the latest 1s value which is probably not a problem for the UI.

    Could make it every 2000ms if it's not a realtime value.

    Uhhhh...nice...thank you

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