Skip to main content
1-Visitor
June 13, 2016
Solved

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

  • June 13, 2016
  • 7 replies
  • 5090 views

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

Best answer by CarlesColl

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

7 replies

1-Visitor
June 13, 2016

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
fmanniti1-VisitorAuthor
1-Visitor
June 14, 2016

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)
1-Visitor
June 14, 2016

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

fmanniti1-VisitorAuthor
1-Visitor
June 14, 2016

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?

1-Visitor
June 14, 2016

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.