Search code examples
iotsensorsthingsboard

Thingsboard input String widget


i'am working with thingsboard as a plateform to supervise my sensors ,and i want to know if there is any way to add in my dashboard a field that allow me to write comments and those comments will be written in postgresql database ?

i Tried to use input widget but it dosent work


Solution

  • it's a bit harder to make comments that will not overwrite previous comments since you would keep those comments under server attributes (you can't work with postgreSQL directly from ThingsBoard).

    Solution to that is that you have input widget and then RPC call to rule chain where you would need to have some script that would put new comment as new field to the JSON array. This obviously requires a bit advanced knowledge about TB.

    Also you could probably do that thru "Custom action (with HTML template)".

    UPDATE: The easiest way you can have comments :

    1. Create JSON server attribute on entity (Device, Asset)

    2. (Optional) Create filter that will return only one entity

    3. Create "Update JSON attribute" widget and point it to the server attribute of entity on which you have done first step (help your self with step 2.)

      a. have something like this in that window: {"comments":[{"ts":1665472792000,"value":"My comm1"},{"ts":1668163473000,"value":"My comm2"}]}

    4. Create "HTML Value Card" widget, which also points to that entity and it's server attribute

    5. In "HTML Value Card" widget use this code in server attribute post-processing function:

    var comm = parsed["comments"]
    
    var returnValue = '';
    
    if (comm){
        var commLength = comm.length;
    }
    
    for (var i=0;i<commLength;i++){
        returnValue += "<span>"+comm[i]["value"]+"<span><br>";
    }
    
    return returnValue;

    1. This code in HTML of "HTML Value Card": ${nameOfYourServerAttribute}

    Yes, you will have to manually add comments directly to JSON, I don't have time to write some widget for that.

    Take a look at my example: Server attribute of Device Update JSON attribute Content of Update JSON attribute HTML Value Card Data key configuration HTML of HTML Value card End Result