Search code examples
thingsboard

How to get live connection status of devices in ThingsBoard?


I recently started working with thingsboard in context of my bachelor-thesis. Right know I am trying out different functionalities in order to check which requirements thingsboard already satisfies and which have to be implemented by me.

Within that process I am stuck at the requirement that thingsboard dashboard should give live information about the connectivity status of a registered device (connected, disconnected, active, inactive).

To solve that I've tried out editing the rule chain used by the device profile assigned to the device of interest. Specifically I worked with the message type switch and tried out various action nodes connected to it via ConnectEvent, DisconnectEvent, ActiveEvent and InactiveEvent. What I would like to have is the connectivity status of the device to be stored in an SERVER_SCOPE attribute and then being displayed in the entity widget. All I've achieved is to generate an alarm when disconnected and cleared when (re)connected. I know there is a server side attribute called active that changes true immediately when device connects but it only turns false after the inactivityTimeout expired.

I also checked the ThingsBoard Device Connectivity Status page, the Create Alarm when the Device is offline guide and took long researches but couldn't get it to work.

Do you guys have any solutions or tips for me?

Kind regard.


Solution

  • With the help of @JacksonB I managed to solve my problem.

    What did I do?

    1. Create a blue Transform-script node.

    2. Change message type to POST_ATTRIBUTES_REQUEST and add desired attributes. Here's my code for that:

      var newType = "POST_ATTRIBUTES_REQUEST"

      msg.connectivity = "connected"

      return {msg: msg, metadata: metadata, msgType: newType};

    3. Than just connect this to an post attribute node in the rule chain like shown in the image. Link to screenshot of rule chain

    No I am able to instantly see if a device is connected or disconnected by displaying the value of that attribute on my dashboard.