Skip to main content
1-Visitor
December 22, 2016
Solved

Data received from ThingTemplates

  • December 22, 2016
  • 4 replies
  • 2189 views

Hello. I've an issue and I was wondering if anyone can help me.

I'm currently trying to fetch property history for Things that I've simulated in my local Thingworx composer through the API using the ThingTemplates/Services/QueryImplementingThingsWithPropertyHistory and my goal is to get only the latest entry for each Thing that I've. I thought I could achieve this using the MaxDataItems=1 but I'm getting something like this:

"rows": [

    {

      "Temperature": 68,

      "name": "CD 001",

      "Gyroscope": 0,

      "CustomerName": "Something",

      "Cash": 45,

      "Pressure": 20,

      "Stock": 71,

      "timestamp": 1482420116565

    },

    {

      "Temperature": 68,

      "name": "CD 001",

      "CustomerName": "Something",

      "Cash": 45,

      "Pressure": 20,

      "Stock": 71,

      "timestamp": 1482420116564

    },

    {

      "name": "CD 001",

      "Cash": 45,

      "Pressure": 20,

      "Stock": 71,

      "timestamp": 1482420116561

    },

    {

      "name": "CD 001",

      "Cash": 45,

      "Stock": 71,

      "timestamp": 1482417836498

    },

    {

      "name": "CD 001",

      "Cash": 45,

      "timestamp": 1482417836497

    },

     ...

]

As you can see I'm getting several rows for the same device with name "CD 001". Can anyone tell me if there is a better way to fetch just the first entry (which is the most complete one as well as the most recent) or is this behavior happening just because I'm simulating data on the same timer?

Thank you for your time and help in advance

Best answer by supandey

Hi Francisco,

here's how you can use topN:

var params = {

  maxItems: undefined /* NUMBER */,

  t: undefined /* INFOTABLE */ (pass your infoTable here with data)

};

// result: INFOTABLE

var result = Resources["InfoTableFunctions"].TopN(params);

4 replies

22-Sapphire I
December 22, 2016

Create a Service where you pull the records, and then use TopN to get the first record is probably what you'll need to do.

You can't just get 1 record with the Queries they will always return up to 500 or more.

fsiviero1-VisitorAuthor
1-Visitor
December 27, 2016

Thank you for your help.

If you see this example:

"rows": [

    {

      "Temperature": 71,

      "name": "CD 001",

      "Gyroscope": 0,

      "CustomerName": "Teracode",

      "Cash": 31,

      "Pressure": 19,

      "Stock": 82,

      "timestamp": 1482839719197

    },

    {

      "Temperature": 71,

      "name": "CD 001",

      "CustomerName": "Teracode",

      "Cash": 31,

      "Pressure": 19,

      "Stock": 82,

      "timestamp": 1482839719196

    },

    {

      "Temperature": 71,

      "name": "CD 001",

      "Cash": 31,

      "Pressure": 19,

      "Stock": 82,

      "timestamp": 1482839719195

    },

    {

      "name": "CD 001",

      "Cash": 31,

      "Stock": 82,

      "timestamp": 1482839119166

    },

    {

      "name": "CD 001",

      "Cash": 31,

      "timestamp": 1482839119165

    },

    {

      "Temperature": 74,

      "name": "CD 010",

      "Gyroscope": 0,

      "CustomerName": "Hotel B",

      "Cash": 95,

      "Pressure": 19,

      "Stock": 34,

      "timestamp": 1482839719119

    },

    {

      "Temperature": 74,

      "name": "CD 010",

      "Cash": 95,

      "Pressure": 19,

      "Stock": 34,

      "timestamp": 1482839719118

    },

    {

      "name": "CD 010",

      "Cash": 95,

      "Pressure": 19,

      "Stock": 34,

      "timestamp": 1482839719117

    },

    {

      "name": "CD 010",

      "Cash": 95,

      "Stock": 34,

      "timestamp": 1482839119122

    },

    {

      "name": "CD 010",

      "Cash": 95,

      "timestamp": 1482839119121

    },

    {

      "Temperature": 69,

      "name": "CD 011",

      "Gyroscope": 0,

      "CustomerName": "Hotel A",

      "Cash": 91,

      "Pressure": 22,

      "Stock": 38,

      "timestamp": 1482839719122

    },

    {

      "Temperature": 69,

      "name": "CD 011",

      "Cash": 91,

      "Pressure": 22,

      "Stock": 38,

      "timestamp": 1482839719121

    },

    {

      "name": "CD 011",

      "Cash": 91,

      "Pressure": 22,

      "Stock": 38,

      "timestamp": 1482839719120

    },

    {

      "name": "CD 011",

      "Cash": 91,

      "Stock": 38,

      "timestamp": 1482839119124

    },

    {

      "name": "CD 011",

      "Cash": 91,

      "timestamp": 1482839119123

    },

    {

      "Temperature": 72,

      "name": "CD 012",

      "Gyroscope": 0,

      "CustomerName": "PTC HQ",

      "Cash": 119,

      "Pressure": 19,

      "Stock": 16,

      "timestamp": 1482839719126

    },

    {

      "Temperature": 72,

      "name": "CD 012",

      "CustomerName": "PTC HQ",

      "Cash": 119,

      "Pressure": 19,

      "Stock": 16,

      "timestamp": 1482839719125

    },

    {

      "Temperature": 72,

      "name": "CD 012",

      "Cash": 119,

      "Pressure": 19,

      "Stock": 16,

      "timestamp": 1482839719124

    },

    {

      "name": "CD 012",

      "Cash": 119,

      "Pressure": 19,

      "Stock": 16,

      "timestamp": 1482839719123

    },

    {

      "name": "CD 012",

      "Cash": 119,

      "Stock": 16,

      "timestamp": 1482839119126

    },

    {

      "name": "CD 012",

      "Cash": 119,

      "timestamp": 1482839119125

    },   

...

You can see there are multiple entries returned for CD 001, CD 010, CD 011 and CD 012, even though I used maxDataItems=1. I wanted the most complete one for each of these Things, so it would be something like:

"rows": [

    {

      "Temperature": 71,

      "name": "CD 001",

      "Gyroscope": 0,

      "CustomerName": "Teracode",

      "Cash": 31,

      "Pressure": 19,

      "Stock": 82,

      "timestamp": 1482839719197

    },

    {

      "Temperature": 74,

      "name": "CD 010",

      "Gyroscope": 0,

      "CustomerName": "Hotel B",

      "Cash": 95,

      "Pressure": 19,

      "Stock": 34,

      "timestamp": 1482839719119

    },

    {

      "Temperature": 69,

      "name": "CD 011",

      "Gyroscope": 0,

      "CustomerName": "Hotel A",

      "Cash": 91,

      "Pressure": 22,

      "Stock": 38,

      "timestamp": 1482839719122

    },

    {

      "Temperature": 72,

      "name": "CD 012",

      "Gyroscope": 0,

      "CustomerName": "PTC HQ",

      "Cash": 119,

      "Pressure": 19,

      "Stock": 16,

      "timestamp": 1482839719126

    },

...

How would I apply topN to the call? I never used it before.

supandey5-Regular MemberAnswer
5-Regular Member
December 27, 2016

Hi Francisco,

here's how you can use topN:

var params = {

  maxItems: undefined /* NUMBER */,

  t: undefined /* INFOTABLE */ (pass your infoTable here with data)

};

// result: INFOTABLE

var result = Resources["InfoTableFunctions"].TopN(params);