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

Random lighting


Guest nohed

Question

Guest nohed
Posted

I have a code that looks like this

fibaro:sleep(math.random(5000,12000));

fibaro:call(7, "turnOn");

fibaro:sleep(math.random(5000,12000));

fibaro:call(57, "turnOn");

fibaro:sleep(math.random(5000,12000));

fibaro:call(61, "turnOn");

but this code lights the lamp always in the same order. Can you get it so that they both shuffles time and ordningnen as the lights come on?

14 answers to this question

Recommended Posts

  • 0
Posted

Please login or register to see this code.

  • 0
Posted

Nice.

  • 0
Guest nohed
  • Inquirer
  • Posted

    nice

    Please login or register to see this image.

    /emoticons/default_wink.png" alt=";)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    • 0
    Posted

    Only problem is, that this is completely random (well not really, computers can't do truly random but anyway) and therefore, you might see the same lamp turn on several times in a row - appearing not to be random. If you want to make sure that they turnon in a "random sequence", you need to employ a randomizing algorithm. Basically a piece of code that ensures that a different lamp from the last will switch on every time - sort of what Apple had to do because their randomizer was too random and it sometimes chose the same song twice in a row - at random... But that might be overdoing it for this application

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" />

    • 0
    Posted

    I created device for such presence simulator. It will scan api for all lights (isLight = 1) and randomly turn them on and off (customizable times). Number of runs can be changed, one device will not be repeated (so number of runs must be lower or equal to number of devices).

    Please login or register to see this link.

    • 0
    Posted

    Hey guys,

    Dalle1985, there is a way to get around what you proposed:

    Please login or register to see this code.

    The reason this works in a random way is that the order that items get returned from the pairs() function is not defined, even for indexed tables. It's not truly random (as you previously pointed out), but should work fine for this.

    Incidentally, if you remove the sleep line this is a really efficient way of applying the same command to multiple devices.

    Cheers,

    Dan

    • 0
    Posted

    Hah - that is a very clever little piece of code! I didn't know that Lua would work like that - I thought it would just take it in the order you've arranged it in the array.

    • 0
    Posted

    You could seed the random by using:

    Please login or register to see this code.

    or

    Please login or register to see this code.

    Still not ideal, for the lack of milliseconds, so I've read

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" /> Or does the HC2 has them?

    • 0
    Posted

    Dalle1985,

    It will if you use ipairs() instead. ipairs() returns the values in the exact numeric order of the indices.

    Basically, if you need the order to be defined use ipairs(), if not use pairs()

    See here for more information -

    Please login or register to see this link.

    Cheers,

    Dan

    [ Added: 2014-10-30, 12:18 ]

    UPDATE:

    Just done some further testing on this, and although returned value order is not defined, it's actually the same every time, so the order might be random but it doesn't change when run multiple times. Below is a randomisation function you can add in to make it work:

    Please login or register to see this code.

    Thanks,

    Dan

    • 0
    Posted

    I use this lua to randomly turn on lights.

    Please login or register to see this code.

    And i would like to have in this snippet so the scene only runs when the lights are out, but where should i put it in?

    Please login or register to see this code.

    • 0
    Posted

     

    I use this lua to randomly turn on lights.

    Please login or register to see this code.

    And i would like to have in this snippet so the scene only runs when the lights are out, but where should i put it in?

    Please login or register to see this code.

    I use this code.

    But the light are not randomize going off...

    • 0
    Posted

    Mine works just fine, have you change device id:s to yours?

    I may have a newer version. I can post it tomorrow.

    • 0
    Posted

    This is the latest that i run and i works 

    Please login or register to see this code.

    • 0
    Posted

    what might even be easier to do. Is just set  a date or range of date's that you want to simulate. and run through the event log and replicate the behaviour of the devices... than you have real simulation.

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