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


  • 0

How to make a scene to centralize a function


robmac

Question

This technique can be used for a lot more but this simple case illustrates it in a very simple way.

 

I am not seeing a lot of example of using this feature posted so I thought this might give  a few who do not know some ideas. The technique is based around that second parameter of the fibaro:startScene method fibaro:startScene(sceneID,args).

 

args is a LUA table so you can pass pretty much anything to a scene via the API or from another piece of LUA. In this example only a table with one text element is passed.

 

Create a scene myDebugIllustration

Please login or register to see this code.

You may need to set the number of instances to a larger number wen you use this properly.

 

Now create another scene myTestScene

Please login or register to see this code.

Now remember that as I said earlier "a lua table can hold pretty much anything"

 

Enjoy

 

  • Like 1
Link to comment
Share on other sites

Recommended Posts

  • 0

What if you send a function in the table? At work so I can't test but too curious ;-) 

(guess they neuter it with a json.encode/decode ?)

Link to comment
Share on other sites

  • 0

Hi @robmac,

 

Thank you very much for sharing this feature. I wonder why this is still not documented on Fibaro developer site?! My HC2 is on 4.110 and this work nicely! This feature is exactly what I need for several solutions among which one is for sending messages! No more need for global variables.

 

@AutoFrank needs to see this for his Smart Message Hub solution !!

 

Link to comment
Share on other sites

  • 0
2 minutes ago, Sankotronic said:

Hi @robmac,

 

Thank you very much for sharing this feature. I wonder why this is still not documented on Fibaro developer site?! My HC2 is on 4.110 and this work nicely! This feature is exactly what I need for several solutions among which one is for sending messages! No more need for global variables.

 

@AutoFrank needs to see this for his Smart Message Hub solution !!

 

 

@robmac

@Sankotronic

 

Thanks for sharing this, I have been wondering if this was possible for a while now for simple functions such as url encode and other string sub functions

The most obvious use for SMH would be store/execute the SendMsgH function in a global instead of having to include in each scene

I must play around with this and see what may be achievable and see if its better than the use of GV

 

the list gets longer and the time available stays the same, need to prioritise better ;-)

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • 24 minutes ago, jgab said:

    What if you send a function in the table? At work so I can't test but too curious ;-) 

    (guess they neuter it with a json.encode/decode ?)

    I am sure it will work. It is a LUA table so as long as the implementation is not mad there will be no issue. 

    Link to comment
    Share on other sites

    • 0
    Just now, robmac said:

    I am sure it will work. It is a LUA table so as long as the implementation is not mad there will be no issue. 

     

    @robmac

     

    now I know what I'll be doing after dinner this evening :-)

    my wife will be delighted, she'll have the TV to herself this evening .....

     

    thanks

     

    Link to comment
    Share on other sites

    • 0
    1 minute ago, robmac said:

    I am sure it will work. It is a LUA table so as long as the implementation is not mad there will be no issue. 

     

    I'm already implementing this feature to SMsgH and one of my scenes that are sending push and popup messages to help Frank :) 

    Link to comment
    Share on other sites

    • 0
    1 minute ago, Sankotronic said:

     

    I'm already implementing this feature to SMsgH and one of my scenes that are sending push and popup messages to help Frank :) 

     

    @Sankotronic

     

    way ahead of me :-)

    thanks

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Just remember they are making a copy of the table not passing a reference. You can not return data to the original instance of the  calling scene as far as I know.  

     

    You could potentially chain it by killing the original instance of the calling scene and starting a new instance potentially. Just fork the code by checking for args.

    Link to comment
    Share on other sites

    • 0
    3 minutes ago, robmac said:

    Just remember they are making a copy of the table not passing a reference. You can not return data to the original instance of the  calling scene as far as I know.  

     

    You could potentially chain it by killing the original instance of the calling scene and starting a new instance potentially. Just fork the code by checking for args.

     

     

    Thanks @robmac, I feel we should be okay, I'm thinking we can pass the string (json table) to the message hub scene from the scene that wants to use the message hub and process it within the message hub scene itself. Is this what you're thinking as well @Sankotronic ?

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • you know about fibaro:debug(urlencode('this is a string'))

    Link to comment
    Share on other sites

    • 0
    2 minutes ago, robmac said:

    you know about fibaro:debug(urlencode('this is a string'))

     

    @robmacYou're full of gems this morning :-), tell me more 

    Link to comment
    Share on other sites

    • 0

    @robmac I would be surprised if it worked. I thought they spawn new instances of the LUA engine (or LUA context) for each scene instance. I would suspect that the json'ed the arg and hand it over to the new instance.

     

    Haven't tested this code but something like this would be a way to get a return argument back (ok, with a global var). Not for rapid calls, but for larger library functions... 


     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0

    Well as my quick experiment shows this doesn't work:

    Please login or register to see this code.

    but this does work:

    Please login or register to see this code.

     

     

    Link to comment
    Share on other sites

    • 0
    2 minutes ago, robmac said:

    and what about

     

     split(text, delimiter)

     

     

    @robmac

     

    well, now you're just teasing :mrgreen:,

    Where have you hidden the fibaro fountain of knowledge ?, can us mere mortals gain access ?

     

     

     

     

    Link to comment
    Share on other sites

    • 0
    1 minute ago, Sankotronic said:

    Well as my quick experiment shows this doesn't work:

    Please login or register to see this code.

    but this does work:

    Please login or register to see this code.

     

     

     

    @Sankotronic

     

    That's what I was thinking, except without the function in #2

    Could we get away with not wrapping it in a function is we wanted

    I assume it would work just as well ?

     

    This will be slick . a simple startScene to send the message to the hub.. :-D.... 

     

     

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 9 minutes ago, jgab said:

    @robmac I would be surprised if it worked. I thought they spawn new instances of the LUA engine (or LUA context) for each scene instance. I would suspect that the json'ed the arg and hand it over to the new instance.

     

    Haven't tested this code but something like this would be a way to get a return argument back (ok, with a global var). Not for rapid calls, but for larger library functions... 


     

    Please login or register to see this code.

     

    alas as they refuse to allow loadtext they have blocked it. What a pity a small change to line 93 and the reconstituting code and I would think it could be done easily. Probably think it is a 'security risk':roll:

     

    but I know a way around it anyway:mrgreen:

    Edited by robmac
    Link to comment
    Share on other sites

    • 0
    2 minutes ago, AutoFrank said:

     

    @Sankotronic

     

    That's what I was thinking, except without the function in #2

    Could we get away with not wrapping it in a function is we wanted

    I assume it would work just as well ?

     

    This will be slick . a simple startScene to send the message to the hub.. :-D.... 

     

     

    Well this syntax is working:

    Please login or register to see this code.

    Of course it doesn't have to be wrapped in function I'm just doing a quick test using your SendMsg function :) 

    Next step is to change code of SMsgH scene.

    Link to comment
    Share on other sites

    • 0
    1 minute ago, Sankotronic said:

     

    Well this syntax is working:

    Please login or register to see this code.

    Of course it doesn't have to be wrapped in function I'm just doing a quick test using your SendMsg function :) 

    Next step is to change code of SMsgH scene.

     

    @Sankotronic

    Really starting to gather legs at this stage .... :-)

    ...  thx

     

     

     

    Link to comment
    Share on other sites

    • 0

    Is it possible to pass to a scene special characters like ä,ö,ü, ł, ó, ą ?

    Actually I get different chararacters: ÄÅÄÅÅ

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