Search code examples
thingsboardtelemetry

How to retrieve telemetry for all customer devices from Thingsboard via websocket


I want to get the latest telemetry for all devices related to a single customer from thingsboard CE via API over a websocket. So that I can update a jquery datatable in real-time when measurements are received.

after manually adding a relation for customer->device via GUI latest telemetry data is now flowing to the CUSTOMER entityType in Thingsboard.

However; the data returned does not contain any device attribute information that I can use to match the ws response to the device that generated the measurements.

{"subscriptionId":10,"errorCode":0,"errorMsg":null,"data":{"battery_voltage":[[1559065098932,"2.8623046875"]],"pressure":[[1559065098932,"0.05001525953412056"]],"relative_humidity":[[1559065098932,"53.7109375"]],"temperature":[[1559065098932,"83.78323364257812"]]},"latestValues":{"battery_voltage":1559065098932,"temperature":1559065098932,"pressure":1559065098932,"relative_humidity":1559065098932}}

endpoint in Thingsboard:/api/ws/plugins/telemetry

this code works and returns latest telemetry update, but it doesn't specify which device in the returned result.

$( document ).ready(function() { 
...
webSocket.onopen = function () {
   var object = {
       tsSubCmds: [{
           entityType: "CUSTOMER",
           entityId: "00000000-0000-0000-0000-0000...",
                        scope: "LATEST_TELEMETRY",
                        cmdId: 10
                    }
                ],
                historyCmds: [],
                attrSubCmds: []
            };
            var data = JSON.stringify(object);
            webSocket.send(data);
            console.log("Message is sent: " + data);
            console.log("socket open: ");
        };

How can i enrich the telemetry timeseries data with device attribute?


Solution

  • The solution I found was to update the rule-chain in the GUI so that a copy of the incoming DEVICE telemetry data is duplicated and saved to the related CUSTOMER entity.

    Step by step:

    1. login to thingsboard as a tenant administrator

    2. click on the Root Rule Chain

    3. find the Save timeseries node on the Post telemetry branch

    4. drag a new filter...script node onto the canvas

    5. connect the new script node to the output of the Save timeseries node on the Post Telemetry branch

    6. edit the filter script name: -> Should Duplicate

    7. edit the filter script function -> return true;

    8. drag a new Transformation...change originator node to the canvas

    9. connect the new transformation node to the output of the filter node

    10. edit the Transformation node name: Reassign to Customer

    11. edit the originator source: Customer

    12. drag a new Action...save timeseries node onto the canvas

    13. Connect the new Action node to the output of the Transformation node

    14. edit the Action node name:Save Related Telemetry

    Your root rule chain should now look similar to this: