Ubidots Community

[SOLVED] Last activity datetime in HTML Canvas Widget

It’s possible to obtain the datetime (DD/MM HH:mm) from the last activity/update of a device, to insert and view in the HTML Canvas Widget?
If so, what is the code needed to run this feature?

best regards
Jorge

Hi @JS79,

I hope all is well.

To your question, yes, is possible to obtain the last activity of a device or variable in the HTML Canvas widget. To this you need to make a GET request that returns the last activity, such as the ones below.

Obtain the last activity of a Device

 GET : https://industrial.api.ubidots.com/api/v1.6/devices/{device_label}/

Obtain the last activity of a variable 

 GET : https://industrial.api.ubidots.com/api/v1.6/devices/{device_label}/{variable_label}/

For more information, please refer to our API Documentation. Please keep in mind that the last activity returns a timestamp value in milliseconds, withing the logic of your HTML Canvas code, you have to transform the value to a Human readable date to display it in the widget.

All the best,
-Isabel

Thank you for your answer Isabel,
Due to my lack of information, I was unaware that each “dot” by definition is composed of a value and its respective timestamp, so I just extracted the timestamp and manipulated it as I intended.
In the meantime I was able to implement the functionality that I wanted, if it is useful for someone I leave here the code I used:

function getLastValue(variableId, token) {
var url = ‘https://industrial.api.ubidots.com/api/v1.6/variables/’ + variableId + ‘/values’;
$.get(url, { token: token, page_size: 1 }, function (res) {
if (lastValue === null || res.results[0].value !== lastValue) {

        var day = new Date(res.results[0].timestamp).toLocaleDateString("pt-PT");
        var hour = new Date(res.results[0].timestamp).toLocaleTimeString("pt-PT");
    
        $("#" + labelTime).html(hour + day);

        $("#" + labelValue).html(res.results[0].value);
    }
  });

Hi @JS79,

Glad to hear you were able to make what you were looking for and thank you for sharing out your solution, I’m sure it will be helpful to other users.

Have a nice day,
-Isabel