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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

How to update all Thing's properties from script

tcoufal
13-Aquamarine

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

ACCEPTED SOLUTION

Accepted Solutions
jkaczynski
12-Amethyst
(To:tcoufal)

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.

View solution in original post

8 REPLIES 8
ttielebein
14-Alexandrite
(To:tcoufal)

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!

jkaczynski
12-Amethyst
(To:tcoufal)

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.

Since it's now a "correct answer" - for future reference of others I cleaned the code snippets and added descriptive comments.

Funny, I was working on this same problem today.  If you're specifying timestamps for ValueStream logging as per solution #2, instead of using UpdatePropertyValues with a NamedVTQ, it may be simpler to use a Add*ValueStreamEntry service.  I couldn't get UpdatePropertyValues to work at all with an InfoTable property, but AddInfoTableValueStreamEntry did the trick in this case.

Hi Chris Maloof​,

Yes, that's also one of the additional possibilities, but it's rather for smaller amount of properties, because it adds only one value per service invocation. The UpdatePropertyValues service can add multiple at once.

ankigupta
12-Amethyst
(To:tcoufal)

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.

tcoufal
13-Aquamarine
(To:ankigupta)

Thanks that is helpful. It always bothered me (those multiple entries in value stream)...

tcoufal
13-Aquamarine
(To:tcoufal)

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

Announcements


Top Tags