Dragino LHT65 Decode

Hello

I have the Problem that the payload decoder don’t make a correct decoded container from the Dragino LHT65 input…

The Original Decoder from Dragino:

function Decode(fPort, bytes){
var data = {
//External sensor
Ext_sensor:
{
“0”:“No external sensor”,
“1”:“Temperature Sensor”,
“4”:“Interrupt Sensor send”,
“5”:“Illumination Sensor”,
“6”:“ADC Sensor”,
“7”:“Interrupt Sensor count”,
}[bytes[6]&0x7F],

   //Battery,units:V
   BatV:((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000,
   
   //SHT20,temperature,units:
   TempC_SHT:((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2),
   
   //SHT20,Humidity,units:%
   Hum_SHT:((bytes[4]<<8 | bytes[5])/10).toFixed(1),
   
   //DS18B20,temperature,units:
   TempC_DS:
   {
     "1":((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2),
   }[bytes[6]&0xFF],       
   
   //Exti pin level,PA4
   Exti_pin_level:
   {
     "4":bytes[7] ? "High":"Low",
   }[bytes[6]&0x7F], 
   
   //Exit pin status,PA4
   Exti_status:
   {
     "4":bytes[8] ? "True":"False",
   }[bytes[6]&0x7F],    
   
   //BH1750,illumination,units:lux
   ILL_lux:
   {
     "5":bytes[7]<<8 | bytes[8],
   }[bytes[6]&0x7F],  

    //ADC,PA4,units:V
    ADC_V:
   {
     "6":(bytes[7]<<8 | bytes[8])/1000,
   }[bytes[6]&0x7F],  
   
    //Exti count,PA4,units:times
    Exit_count:
    {
      "7":bytes[7]<<8 | bytes[8],
    }[bytes[6]&0x7F],  
    
    //Applicable to working mode 4567,and working mode 467 requires short circuit PA9 and PA10
    No_connect:
    {
      "1":"Sensor no connection",
    }[(bytes[6]&0x80)>>7],  

};
return data;
}

But the Output is:

msg.payload : Object
object
applicationID: “1”
applicationName: “Testgeraet”
deviceName: “Dragino_LHT65”
devEUI: “qEBBlnGCe9o=”
rxInfo: array[2]
txInfo: object
adr: true
dr: 0
fCnt: 78
fPort: 2
data: “y7YJCwITAQgCf/8=”
objectJSON: “{“BatV”:2.998,“Ext_sensor”:“Temperature Sensor”,“Hum_SHT”:“53.1”,“TempC_DS”:“20.50”,“TempC_SHT”:“23.15”}”
tags: object
empty
confirmedUplink: false
devAddr: “AIxCWw==”

Now, i want the Container: objectJSON:
objectJSON: “{“BatV”:2.998,“Ext_sensor”:“Temperature Sensor”,“Hum_SHT”:“53.1”,“TempC_DS”:“20.50”,“TempC_SHT”:“23.15”}”

Have like This:

objectJSON:
BatV: 2.982
Ext_sensor: “Temperature Sensor”
Hum_SHT: “88.5”
TempC_DS: “17.06”
TempC_SHT: “17.36”

What i must change?

Sorry but i am an beginner with Chirpstack and Json…
Chers
Marco

Can you try to format your code better? It is very hard to read right now. Use code blocks surrounded by ``` (or just use the button) so they are formatted nicely for you like this:

function something(someargument) {
    console.log('hello', someargument);
}

Secondly, I don’t understand your question. Are you asking how to read json and change it? Are you getting any errors with the decodePayload function? Or are you just asking a regular javascript question? If you are just asking how to print a javascript object as a nicely formatted string (which is what I think you mean?) then I would suggest starting a javascript tutorial somewhere. Quick hit on google: JavaScript Objects

You’d probably want something like

console.log(msg.payload.objectJSON);

Sorry the Forum Software has reformatted the script…

The decoder output works without any errors. The main Problem is that the decoder output is like this:

objectJSON: “{“BatV”:2.998,“Ext_sensor”:“Temperature Sensor”,“Hum_SHT”:“53.1”,“TempC_DS”:“20.50”,“TempC_SHT”:“23.15”}”

(Yes all values in a single obekt in a single line, seperated with a “,”)

Butt i will have it with every Value in value from a object:

objectJSON:
BatV: 2.982
Ext_sensor: “Temperature Sensor”
Hum_SHT: “88.5”
TempC_DS: “17.06”
TempC_SHT: “17.36”

Here are the links to the Decoder:

Decoder for Chirpstack:
https://www.dragino.com/downloads/downloads/LHT65/payload_decode/LHT65chirpstack%20decoder.txt

Decoder for TTS (it gives all values correct back(in tts)):
https://www.dragino.com/downloads/downloads/LHT65/payload_decode/ttnv2_payload_decode_v1.8.txt

Paload Return from TTS:

Payload Return from Chirpstack:

Please take the time to properly write your question. You say TTS but mean TTN for instance. That makes it harder to answer your question. The easier you make it to answer, the better the answers you will get.

It sounds to me like you’re asking indeed how to write javascript code. JSON is just a text representation of javascript objects. So you need to learn how to write javascript code.
I think you are using NodeRED there (judging from your screenshots)? You didn’t mention that anywhere in your question. That is quite relevant information. So what you’re trying to do is match the output format that Chirpstack gives you, to the output format for TTN? So in that case you want to read up on node-red’s functions. Writing Functions : Node-RED. But in any case, this is not the forum to ask. YOu are just asking a very basic javascript question. So either ask on stack overflow in a generic “how do I do this in javascript”, where you isolate your question to purely the javascript problem. Or ask on the node-red fora on how to do it with a node-red function.

1 Like

I will the decoder in this format, that the Chirpstack Server Decode the values in a object with each value is Separatet, not separatet with a “,” in a single line.

like in the pictures fron TTN…

this is a decoder problem (the problem is the script not the decoder himself, i know) butt i will have the output from chirpstack like the decoder in TTN, it is possible?

I use node red only for test, its very easy to see what i get. i dont will use node-red to use or change the data, i will later do all with PHP and mysql, my script is Working with TTN, but not with the actual Chirpstack decoder…

I am really sorry but I really don’t understand your problem then. It really sounds to me like you’re asking “How can I print a javascript object?” Maybe language may be a barrier here (I am not a native english speaker either), but try to phrase your questions clearer if that is not your question. Maybe this will help:

@Marco I think I got you, your problem ist that TTN returns an object right away you can directly acces, but Chirpstack just has the objectJSON, in fact it sends you JSON with an embedded JSON object containing the object from your decoder.
I don’t think you can change that with the decoder nor with any other setting in Chirpstack. You have to deal with that in Node-RED.
Do you already parse the message from Chirpstack with a “json” node? I assume because all I get form Chirpstack is a string payload with JSON.
I do a first parse of the whole string, this gives me an object like on your screenshot above with the objectJSON field as another JSON string. Then I move selected data to other fields, like DR and other stuff I want to have and at last I move the objectJSON to payload and parse that again with a “json” node. I created a subflow for this:


Here is the code for my subflow:

[
    {
        "id": "5f0ae4bb.d59e3c",
        "type": "change",
        "z": "a9da7c2e.e88f7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "applicationID",
                "pt": "msg",
                "to": "payload.applicationID",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "applicationName",
                "pt": "msg",
                "to": "payload.applicationName",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "deviceName",
                "pt": "msg",
                "to": "payload.deviceName",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "devEUI",
                "pt": "msg",
                "to": "payload.devEUI",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "devAddr",
                "pt": "msg",
                "to": "payload.devAddr",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "loRaSNR",
                "pt": "msg",
                "to": "payload.rxInfo[0].loRaSNR",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "adr",
                "pt": "msg",
                "to": "payload.adr",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "dr",
                "pt": "msg",
                "to": "payload.dr",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "rssi",
                "pt": "msg",
                "to": "payload.rxInfo[0].rssi",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "fCnt",
                "pt": "msg",
                "to": "payload.fCnt",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "fPort",
                "pt": "msg",
                "to": "payload.fPort",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "confirmedUplink",
                "pt": "msg",
                "to": "payload.confirmedUplink",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "payload.data",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.objectJSON",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 80,
        "wires": [
            [
                "1ad92403.64b4ac"
            ]
        ]
    }
]

I hope this helps a little bit…

1 Like

Now I get what you mean (thanks to @Marco ). Ok, you just need
https://www.w3schools.com/js/js_json_parse.asp
You can do

const payload = JSON.parse(msg.payload.objectJSON)
console.log(payload.BatV)