Search code examples
thingsboard

Thingsboard polygon colour change


I am using an arduino to send telemetryu to a thingsboard dashboard. I want to change the 4 polygon colours on an image map widget based on the values of the binary data I am sending from the arduino /red for 1 and green for 0). The data is sent as an array "states[1, 1, 1, 0, 0]. This changes based on switching state. Kindly assist with a polygon colour change function that would accomplish this. I have established comms between arduino and thingsboard.


var states = dsData[dsIndex]['states'];
var states1 = states[1];
var states2 = states[2];
var states3 = states[3];
var states4 = states[4];
var states5 = states[5];
{
var states1 = dsData[dsIndex]['states'];
if (states1 == 1) {
    return "red";
} else {
    return "green";
}
if (states2 == 1) {
    return "red";
} else {
    return "green";
}
if (states3 == 1) {
    return "red";
} else {
    return "green";
}
if (states4 == 1) {
    return "red";
} else {
    return "green";
}
if (states5 == 1) {
    return "red";
} else {
    return "green";
}
}

Solution

  • congratulation on your first post.

    Regarding ThingsBoard, I will assume you are using CE version (but keep in mind that in future posts like this you should specify version of any software you are using and also all relevant hardware).

    Regarding polygon, your issue comes from having only one Device, and all telemetry in it. This is a problem since devices are meant to be dots on the map, while assets are the ones that are covering some area on the map (regardless if it will be actual geographical map or a picture).

    Therefore I suggest that in rule chain you have for input data processing (e.g. root rule chain) you add blue "script" node which will parse every single bit of your array (states[1, 1, 1, 0, 0]) to a different message. After that you need to use create relation node which will switch originator to the assets (which are going to hold one of the bits each). On the end you need a save timeseries or attributes node to actually save your data.

    P.S. this is way have you can separate one message to multiple messages:

    [{
    
    msg: msg1,
    
    metadata:metadata1
    
    msgType:"POST_TELEMETRY_REQUEST"
    
    },{
    
    msg: msg2,
    
    metadata:metadata2
    
    msgType:"POST_TELEMETRY_REQUEST"
    
    }]