Skip to main content
14-Alexandrite
September 30, 2024
Solved

UpdatePropertyValues not working

  • September 30, 2024
  • 2 replies
  • 3253 views
Hi,

I’m currently using Thingworx 9.3.9 and facing an issue while trying to update the property values of boolean properties in a Thing. I’m using the UpdatePropertyValue default service for this purpose.

However, the service is not allowing me to update the values for the current time. When I try to update the values for current time minus 2 or 4 minutes, it works, but not consistently.

Note: The DataChangeType for these properties is set to value.

Method 1: I am using a dynamic DataShape for updating the property values via UpdatePropertyValue.

------------------------------------------------------------------------------------------------------
var currentTime = new Date();
currentTime.setMinutes(currentTime.getMinutes() - 2);
var DataShapeparams = {
infoTableName: "MyUpdateTable" /* STRING */
};
var UpdateEntryIT = Resources["InfoTableFunctions"].CreateInfoTable(DataShapeparams);

var nameField = new Object();
nameField.name = 'name';
nameField.baseType = 'STRING';
UpdateEntryIT.AddField(nameField);

var valueField = new Object();
valueField.name = "value";
valueField.baseType = 'BOOLEAN';
UpdateEntryIT.AddField(valueField);

var dateField = new Object();
dateField.name = "time";
dateField.baseType = 'DATETIME';
UpdateEntryIT.AddField(dateField);

var qualityField = new Object();
qualityField.name = "quality";
qualityField.baseType = 'STRING';
UpdateEntryIT.AddField(qualityField);

var updateRow = new Object();
updateRow.name = Propertyname; // << your actual property name here
updateRow.value = true;// << your new value for that property here
updateRow.time = currentTime;
updateRow.quality = 'GOOD';

UpdateEntryIT.AddRow(updateRow);

Things[Thingname].UpdatePropertyValues({
values: UpdateEntryIT /* INFOTABLE {"dataShape":"NamedVTQ"} */
});
-----------------------------------------------------------------------------
Method 2 - using NamedVTQ datashape (static datashape)
let newEntry = {
name: Propertyname, // STRING [Primary Key]
time: currentTime, // DATETIME
value: false,//Things[Thingname][Propertyname], // VARIANT
quality: undefined // STRING
};
UpdateEntryIT.AddRow(newEntry);
Things[Thingname].UpdatePropertyValues({
values: UpdateEntryIT /* INFOTABLE {"dataShape":"NamedVTQ"} */
});
Best answer by Rocko

Lots of conditions!  @VladimirRosu_116627 gave you a good option here, and mine would be similar in the sense you get the data using QPH and engineer the result by manually adding rows if needed.

 

First would query without a startdate and maxItems=2, oldestfirst=false. Enddate would be 7AM. This will give you at least two rows, always. Then you check if the first row has a date of exactly 07:00:00:000 (unlikely, but possible) and if so you duplicate the value with a new timestamp.

 

2 replies

Rocko
19-Tanzanite
September 30, 2024

That's a very long piece of code for people to study. I would recommend to reduce it to the necessary. This should also help you in identifying the problem.

 

Also note there is a formatting option for source code in the forum editor so you can make it more readable.

14-Alexandrite
September 30, 2024

Hi Rocko, I have update the code please check now.

Rocko
19-Tanzanite
September 30, 2024

My I ask why you are doing it this way instead of just assigning a value like this:

Things[Thingname][Propertyname]=true; // or false, whatever your new value is

 

This will automatically set the value for the current time.

19-Tanzanite
September 30, 2024

If you have DataChange set to Value, then any property updates that you push (via the service you mentioned or any other way) would need to have a different value to be recorded in the property (meaning to be visible in composer and in the associated Value Stream, if you enabled Logging).

Can you specify if the value you're attempting to set is different to the value that is already there on the property ?

Because if not, then the behavior would be as expected.

You can also test this quickly by setting the DataChange to "Always".

There's another thing that you would need to specify, and that is how exactly are you verifying if the property update was successful? If you use composer, with a date in the past, the system will not display it in the Properties tab, but you can see it when using QueryPropertyHistory types of services.

14-Alexandrite
October 4, 2024

Hi VladimirRosu,

I’m trying to store the same value for a property. I've been using UpdatePropertyValue to update property values for a previous timestamp, assuming that data changes wouldn't affect this service, whether the value is the same or different, since it's for a past timestamp.

However, I've decided to take a different approach. I’m now logging the property at the current timestamp, regardless of the data change type or the property value. To ensure the value is always logged, I’m directly writing an insert query into the database.

19-Tanzanite
October 4, 2024

Got it.

As @Rocko said above, inserting in the DB is dangerous - simply because we don't guarantee the schema change across versions and when that changes, you'd need to refactor.

Logging a property at the current timestamp, regardless of value can be done if you set the DataChange as Always - this would be the official approach and it's just a checkbox. Just for this alone, there's no reason in this case to go to SQL queries šŸ™‚