8 Replies Latest reply on Feb 8, 2017 2:59 PM by jkaczynski-2 RSS
    tcoufal Communicator

    How to update all Thing's properties from script

    Hi All,

    I have one question.

     

    How can I achieve updating of all properties (not necessarily all of them) for Things[myThing] from script.

    I've found only two services:

    UpdatePropertyValues - it uses dateshape Values, which I cannot find (sort of like VTQ, but value is type of VARIANT)

    SetPropertyValues - which expects some datashape before it allows to fill the infotable.

     

    Strangely enough there is also service SetProperties , but it is visible only in Mashups..

    Any Best-practice. I'am very tired today, so maybe solution will be easier than I think.

     

    Thanks a lot

      • Re: How to update all Thing's properties from script
        ttielebein Collaborator

        I believe it is simple: Things["EdgeThing"].isConnected

         

        If you enter the entity with the properties under the tab called "Entities" (like "Snippets") you can see the properties there and use the blue arrow buttons to add them to your service. Hope this helps!

        • Re: How to update all Thing's properties from script
          jkaczynski-2 Creator

          Hello Tomas Coufal,

           

          There's a bunch of possibilities:

          1. Easiest.

          Just try to do:

           

          Things[myThing].propertyName = value;
          

           

          2. More flexible.

          Allows to pass also a timestamp - important when you'd like to log the values of your property and want to have set also a timestamp.

           

          // since input for UpdatePropertyValues is of type InfoTable, create one
          var createITParams = { infoTableName : "InfoTable", dataShapeName : "NamedVTQ" };
          var properties = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(createITParams);
          
          // create a row with data
          var row = new Object();
          row.name = "propertyName"; // name of your property
          row.value = 5; // desired value
          row.time = new Date(); // you can set any timestamp you need - important when using ValueStreams to log
          row.quality = "GOOD"; // optional
          properties.AddRow(row);
          
          Things[myThing].UpdatePropertyValues({ values: properties });
          
          

           

          3. Other.

           

          3a. InfoTable should have a field with name and type that conforms to the name and type of your property. Let's say I have property "a" of type "NUMBER":

           

          // since input parameter for SetPropertyValues is InfoTable, create one
          // can be InfoTable without datashape, we will create necessary fields dynamically
          var createITParams = { infoTableName: "MyIT" };
          var properties = Resources["InfoTableFunctions"].CreateInfoTable(createITParams);
          
          // add a field to our InfoTable (name and baseType should conform to our existing properties)
          var newField = new Object();
          newField.name = "a";
          newField.baseType = 'NUMBER';
          properties.AddField(newField);
          
          // create a row with data
          var newEntry = new Object();
          newEntry.a = 5; // the field name in this object ("a") conforms to the name of our property (and field)
          properties.AddRow(newEntry);
          
          Things[myThing].SetPropertyValues({ values: properties });
          
          

           

          3b. SetProperties is Mashup-only service so it's not available from the scripts.

           

          Hope it helps.

          Regards,

          J.

          • Re: How to update all Thing's properties from script
            Ankit Gupta Ninja

            Hi Tomas Coufal, If you are just updating values; you can simply update each property value in a Service. But if you are logging the properties; then you should use updatePropertyValues with same time so there is single entry in value Stream for the update.

            • Re: How to update all Thing's properties from script
              tcoufal Communicator

              I am using #1 quite often. I was looking for some solution how to update all properties at once. I was considering efficiency to be a factor while setting properties one by one. I think that you have some typos in #2 and #3 if I am not mistaken. But I can get the gist.

              Thanks

              Apropos is there any difference between Update.... and Set....?

              Tomas