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

    Change properties values any undefined amount of time

    Hi, I have this current situation:

    ThingTemplate named "MyTemplate" with three properties: Prop1 (Number), Prop2 (number), Prop3 (number).

    25 Things named "TH-1"..."TH-25".

    Those properties will change every x seconds (where x is a random number) and I can see on a mashup the new value on three gauges each one binded to a property.

    Now I have the following questions:

    1. What I should write in the service to change the thing any random time? Because if it is a fixed time, I can make a Timer Thing but what about a random time? Would you make me a simple example of code?
    2. I know the widget "Autorefresh" which refreshes the page automatically every T seconds where T is set in the widget properties but I have random time. What can I do to autorefresh when it triggers?


    Thank you

      • Re: Change properties values any undefined amount of time
        carlesc Ninja

        1. With a normal timer, which every time it's executed "rolls a dice" to decide it has to be executed the corresponding service or not. How to roll a dice? with for instance (Math.random()>0.5)


        2. Right now TW doesn't supports to send events to the browser from the server. Then you need to do a Poll every n-seconds, if you don't want to refresh the whole gauges every fixed n-seconds, you can set a property called "anyUpdates" and every n-seconds you query for that property, if that property it's true then you call the real data pull and you set again anyUpdates=false. If you have multiple users looking each one from their browser, then you will need to set this anyUpdates on the user session.

          • Re: Change properties values any undefined amount of time
            fmanniti Creator

            I am sorry for my question but, since I'm quite new in "ThingWorx's world", I still didn't understand very well the services situation, so I didn't understand your answer.

            From what I got the service is a function while the subscription is an action that "happens" when an event occurs.

            If I see it in this way, I guess that what I need is not a service but a subscription and that my event should occur randomly.

            So, that's what I've done. May you please tell me if there is something wrong?

            1. Created a Thing called: TH-1 which is a Generic Thing and which as a number property called Temp
            2. Created a Stream called TestStream with a Data Shape called TestDataFeed with a field called T (number)
            3. Now, you said that I must use a normal time so I created a Timer Thing called RandTime; the problem is that this TimerThing already has a configuration with a time set at 60 seconds.
            4. Anyways, I go to TH-1 subscriptions and Add a new subscription where the source is RandTime and the Event is Timer.
            5. Then I wrote this code:
              // values:INFOTABLE(Datashape: TestDataFeed)
              var values = Things["TestStream"].CreateValues();
              values.T = me.Temp; //NUMBER
              var params = {
                values : values
              // AddStreamEntry(tags:TAGS, timestamp:DATETIME, source:STRING("me.name"), values:INFOTABLE(TestDataFeed), location:LOCATION):NOTHING
            6. This works, but it changes my value not randomly.
            7. If I would write a random value changing in a random time in a normal HTML page I would do this
              var myVar;
              function doRandom() {
              (function loop() {
                  var rand = Math.round(Math.random() * (3000 - 500)) + 500;
                  setTimeout(function() {
                  }, rand);


            I would like to know: According to what are you saying, am I right in part or I'm totally wrong?
            Thank you

            • Re: Change properties values any undefined amount of time
              fmanniti Creator

              I was thinking about your "roll the dice" solution.

              I don't know if, maybe, I got it wrong, but I don't know if it is the right solution.

              What I understood was this kind of solution, tell me if you meant something else:


              You make a regular timer thing (RandTimer) which is configured any 1 second.

              In your thing template you make one more property that you call "var".

              In the subscription of the thing template you give a random value to var from 0 to 1.

              If var<0.5 you change myProp, else you don't.


              Is it correct according to your idea?

              Because in this case it is not exactly a really random amount of time because my value change any 1*(number of failures) seconds, but the number of failures follow the 2^(-n) law and it obviously is bigger for 1, 2 or 3, and lower (almost 0) for 10, 11. For sure I will never get to 100.



              Instead, I was thinking about this, but I don't know if it is possible.

              Any second, I "stop" the timer


              var timer = Things["RandTimer"];  

              (Is this correct to stop my timer?)

              I declare a new variable calle newTimeRate which i a random number.

              I change via code randTimer rate with the new value, than I enable the Timer.

              Is it possible to do this?

              And, if so, would you please tell me which is the code to change the timer rate?

              Thank you very much

                • Re: Change properties values any undefined amount of time
                  carlesc Ninja

                  First approach should work, you can also store the Timeout on a variable, and only execute it when Timeout<"now"


                  For the Second approach you will need a second timer which Enables the Timer, you can try to play around changing the UpdateRate on the timer, but for my experience that will put you in troubles ( for instance, without a restart some TW versions doesn't updates the Timer frequency ).