$get() to access more than 1 value


#1

Hello Community,

I am working with the HTML Canvas Demo (http://help.ubidots.com/developer-guides/html-canvas-widget-introductory-demo) and modifying it according to my needs.

I need to get not only the latest value of a variable, but the last 10 elements.
From my device I send an array of values to a certain variable. I want to display the list of the values sent last time, the whole array.

Using the funtion of the example:
$.get(url, { token: token, page_size: 1 }, function (res) {
if (lastValue === null ||res.results[0].value !== lastValue.value) {
lastValue = res.results[0].value;

I need to access no only the position ‘0’ but also the positions ‘1’ to ‘9’ for example.

Could you please help me out here?

Thanks a lot!

Francisco


#2

Hello @fmir,

If you desire to obtain the last 10 elements from your variable you need to assign the page_size equal to 10. For a detailed explanation of it, refer to this section of the Ubidots REST API Reference.

The response is going to be an array containing the last 10 elements of your variable. Then, you should loop the array in order to get the parameters desired.

I hope this would help you! :smiley:

All the best,
Maria C.


#3

Thanks Maria!
It helps, a lot… Apparently, I get more data from the result :slight_smile:

Now, I am facing another problem which I don’t understand.

I am sending from my device (MKR GSM 1400 from Arduino) over MQTT this payload {“e”:[1,2,3]}

Ubidots is not always getting the 3 values and/or in the proper order :-/

The communication is not reliable :frowning:
Any tricks for that?

The point is to send an array of values (error codes in my case) and display the whole list!

Thanks again!


#4

The payload sent is not in the proper format. If you noted in the image provided by you, the timestamp for each value is the same, so the Ubidots backend will take it as only value. At this point, if you refresh the page you will see how two of the three values sent are going to be removed.

If you desire to send more that one value in the same request, you should assign the timestamp in the request for each value. Take as reference the payload below to build your own! :smiley:

{"e": [{"value": 1, "timestamp": 1530199430000}, {"value": 4, "timestamp": 1530199430001}]}

All the best,
Maria C.


#5

Thanks Maria! I really appreciate your help :slight_smile:

Your correction worked pretty good, but I still have a problem, and I don’t understand why.

This is the JSON I send:

{“id”:3,“nC”:391529,“nE”:3,“e”:[{“value”:1,“timestamp”:1530624018},{“value”:2,“timestamp”:1530624028},{“value”:3,“timestamp”:1530624038}],“position”:{“value”:10,“context”:{“lat”:41.32940,“lng”:2.04516}},“timestamp”:1530624018}

It arrives at ubidots and it is well interpreted.

As you can see, the location (context) is received properly with the correct timestamp:

The id also arrives perfectly and shows the correct timestamp:

But the array of e is now received completely (the 3 values) but the timestamp is not correct :-/ The date it shows is wrong :frowning:

Any idea what am I doing wrong? Where is the error in my JSON?

Thanks again for your support!

Have a great day,

Francisco


#6

Hey Francisco,

I’m glad to read that :smiley:

As you can note in the payload, you are handling the timestamp values in seconds when the Ubidots backend uses the timestamp in milliseconds. For a detailed explanation of it, you can reference the Ubidots REST API Reference.

I just made a test using your payload but assigning the timestamp in milliseconds and now the expected timestamp is the one assigned

{"id":3,"nC":391529,"nE":3,"e":[{"value":1,"timestamp":1530624018000},{"value":2,"timestamp":1530624028000},{"value":3,"timestamp":1530624038000}],"position":{"value":10,"context":{"lat":41.32940,"lng":2.04516}},"timestamp":1530624018000}

I hope this would help you!

All the best,
Maria C.


#7

Thanks Maria and sorry to bother you (and the community again!).

What I don’t understand is why the timestamp I use (1530624018) does work for the other keys of the JSON :-/

As you can see in my other screenshots, the position, the ID and so on, have a correct date (the pictures are from yesterday though). It means to me that the timestamp used at the end of the JSON does work, but inside the array of e[], it doesn’t… That is what confuses me!

I am having troubles multiplying my timestamp by 1000 so I get the milliseconds value you suggest -.- Which type of variable do you use to get a value in milliseconds (13 digits?) :open_mouth:

Thanks a lot and we keep in touch :slight_smile:

FM


#8

Don’t worry about that Francisco, we are here you help the Ubidots community! :smiley:

The timestamp works properly for the other one because the timestamp of the respective variables are being assigned by the Ubidots backend. If you check carefully the variables which are being sent, you will noted that 5 variables are being sent but the only variable which has the proper JSON object to handle the timestamp is the variable called “e”:

{
    "id":3,
    "nC":391529,
    "nE":3,"e":[{"value":1,"timestamp":1530624018000},{"value":2,"timestamp":1530624028000},{"value":3,"timestamp":1530624038000}],
    "position":{"value":10,"context":{"lat":41.32940,"lng":2.04516}},
    "timestamp":1530624018000
}

In case you desire to send the handle the timestamp for the variables “location” and “id”, you should follow the following structure:

{
    "id":{"value":3, "timestamp": xxxxxxxxxxxxx},
    "nC":391529,
    "nE":3,"e":[{"value":1,"timestamp":1530624018000},{"value":2,"timestamp":1530624028000},{"value":3,"timestamp":1530624038000}],
    "position":{"value":10,"context":{"lat":41.32940,"lng":2.04516}, "timestamp": xxxxxxxxxxxxx}
}

I hope this clears your doubts!

Regards,
Maria C.


#9

All doubts solved :slight_smile:
Everything works perfectly now… At least what is related to this issue.

Thank you VERY much for your help.
You answered awesomely fast and clear.

You will read me again soon :smile:

Have a great week!

FM


#10

We are glad to read that, welcome to Ubidots! :smiley:

All the best,
Maria C.