cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

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

fmanniti
10-Marble

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

ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

7 REPLIES 7

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

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)

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
    }
    Things["EvolutionProcessValuesStream"].AddStreamEntry(params);


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


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.

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

Ok so, you're saying that my process is fine until 5 then you say to create a Value Stream instead of a Stream, am I right?

And one more question: if I don't have to create values or insert them into the properties, how can I "tell" my thing that its property alpha has to change randomly every 5 seconds?

Value Stream it's a "container" for logged properties, once created the Thing type ValueStream you should assign it to the Thing where you have logged properties.

About changing alpha property, you should change it setting it's value as I said on my first post through a Timer thing which triggers every 5 seconds.

Announcements


Top Tags