7 Replies Latest reply on Jun 15, 2016 6:32 AM by carlesc RSS
    fmanniti Creator

    How to change a thing's property with random values?

    I'm sorry for the question (maybe stupid) but I'm very new with ThingWorx.


    I have a thing shape, thing template and 25 things with some properties. Let's call the properties Alpha, Beta, Gamma.


    1. I want that every 5 seconds Alpha change its value with a random number between 1 and 100
    2. I want Beta to change its value according to the new Alpha value (example: Beta = Alpha * 2.5)
    3. I want Gamma to change its value with a random number (just like Alpha) every x seconds and x must be a random number too


    I guess I must use some Javascript into the Snippets but I'd be glad if you could give me an help while I try to see by myself

      • Re: How to change a thing's property with random values?
        carlesc Ninja

        Hi Fabio,


        On server side Javascript:

          1. Build a Timer Thing which triggers every 5 seconds
          2. Subscribe to the previous event from the Thing which owns Alpha property, and set property with Math.floor(Math.random()*100)+1
          1. Subscribe to Property Change event on Alpha from the thing which owns Beta,
          2. set bet = thingwithAlpha.Alpha*2.5
          1. same as 1
          • Re: How to change a thing's property with random values?
            fmanniti Creator

            Thanks for your help but since I'm very new with Thingworx I don't know if I understood it all.
            I write here all the steps I've done and if you can tell me what's wrong I'd be glad.


            1. I created a thing Shape named EvolutionShape with the following properties: Alpha (number), Beta (number), Gamma (number)
            2. I created a thing template named EvolutionTemplate
            3. I created a Thing named Ev-Th1
            4. I can see Alpha and Beta into a Mashup called EvolutionMash and they are represented by two led digits. In the same mashup there also is a Autorefres button.
            5. I created a stream called EvolutionProcessValueStream and a data shape named EvolutionProcessData whose fields definitions are A and B
            6. Than I created a timer thing just by doing: +Thing --> Thing Template: Timer --> Configuration: 5000 and I named it EvolutionTimer5S
            7. Now I went back on my thing template EvolutionTemplate and I went to Subscription-->Add my subscription -->Source: EvolutionTimer5S --> Event:
            8. Timer--> Checked Enabled;
              Then I went on Snippets and: Add Stream Entry --> and insert the code for EvolutionProcessValueStream.
              Now, into the code I changed
              var values = Things["EvolutionProcessValueStream"].CreateValues();
              values.A = Math.floor(Math.random()*100)+1; //NUMBER
              values.B = me.Alpha*2.5; //NUMBER

              Then I Saved and I went back on my mashup expecting to see data changing every 5 seconds (refreshing the page)
            • Re: How to change a thing's property with random values?
              fmanniti Creator

              Ok. I (almost) did it.

              I'll write here all the detail

              1. Created a Thing Shape EvolutionShape with the following properties
                1. Alpha, Number
                2. Beta, Number
                3. Gamma, Number
              2. Created a Thing Template, EvolutionTemplate, which is a GenericThing and implements EvolutionShapes
              3. Create a Thing named, Ev-Th1
              4. Create a Thing named EvolutionTimer5s which has 5000 as value into Update Rate under Configuration Link
              5. Create a Stream and name it EvolutionProcessValuesStream and a Data Shape named EvolutionProcessData
                1. In Field Definition of Data Shape I made the field A and B (that I will use respectively for Alpha and Beta)
              6. Open EvolutionTemplate and go to Subscription link and Add a new subscription, which is the EvolutionTimer5s (remember to check Enabled)
                Add this code
                var values = Things["EvolutionProcessValuesStream"].CreateValues();
                values.A = Math.floor(Math.random()*100)+1; //NUMBER
                values.B = values.A*2.5;
                var params = {
                  values : values

              I just have some more doubts.

              1. If I make a test via EvolutionProcessValuesStream -->Services-->GetStreamEntriesWithData I actually see values changing every 5 seconds but
                If I go on http://localhost/Thingworx/Things/Ev-Th1/Properties/ I don't see Alpha and Beta values updating. What does it means? Isn't it working?
              2. I wanted to try to change the Beta value: instead of being 2.5 times Alpha, I want it to increase of 5 at any trigger (so 0, 5, 10, 15, etc...) but if I simply write
                values.B +=5;
                when I do the test I see Beta has only 5 as value and doesn't increase.
              3. About my point 3, I want Gamma to change randomly any randon x seconds. The thing is that I set the time into the configuration of the Timer Thing and I tried to paste the command Math.random() but it doesn't work. It wants a number and not a formula.


              Thank you very much for your help



                • Re: How to change a thing's property with random values?
                  carlesc Ninja

                  On the subscription you should set the properties in order to se the values changing on the property:

                  me.Alpha = values.A;

                  me.Betha = values.B;


                  To do 2, you should change your code:


                  me.Alpha = Math.floor(Math.random()*100)+1;

                  me.Betha = me.Betha +5;


                  then set values.A and values.B with the previous values:

                  values.A = me.Alpha;

                  values.B = me.Betha;



                  3. Properties doesn't support formulas, you should write a service which does the job.

              • Re: How to change a thing's property with random values?
                carlesc Ninja

                If it's a Value Stream, the history recording it's done "automagically", you don't need to create values neither insert it just update the properties.


                You need to assign the Value Stream ("EvolutionProcessValueSTream" ) to thing Ev-Th1 ( or to the EvolutionTemplate), and then set Alpha, Beta and Gamma as logged properties