# Sintaxis in function for downloading a MQTT downlink message to a device in TTN

**URL:** https://discourse.nodered.org/t/sintaxis-in-function-for-downloading-a-mqtt-downlink-message-to-a-device-in-ttn/68759
**Category:** General
**Created:** [9 October 2022 11:50 UTC](https://discourse.nodered.org/t/sintaxis-in-function-for-downloading-a-mqtt-downlink-message-to-a-device-in-ttn/68759 "2022-10-09T11:50:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jinvers](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jinvers](https://discourse.nodered.org/u/jinvers)
#### Post date: [9 October 2022 11:50 UTC](https://discourse.nodered.org/t/sintaxis-in-function-for-downloading-a-mqtt-downlink-message-to-a-device-in-ttn/68759/1 "2022-10-09T11:50:55Z")

</div>

Hello,

For testing I have created a small TTN network using a Mikrotik wAP LoRA8 Kit gateway and an application in which I have a LoRAWAN modul that I'm controlling by AT commands.

The Node Red project I am using for testing in what you can see in the image:

![imagen](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/0538f8c5f41adc94a48a85618a29666772232afa.png)

When I send an uplink message I receive it well in the Debug window of the Node Red.

When I send a downlink message (just 3 bytes: 0x11 0x22 0x33) to the device, I can see that the Gateway receives the data, but this data is not forwarded to the application and neither the device. In the function node I have written the next:

```auto
return {
    "end_device_ids":{     
        "device_id": "eui-70b3d57ed005629d",
        "application_ids": {
            "application_id": "rfm6601-test-1"
        },
        "dev_eui": "70B3D57ED005629D",
        "join_eui": "323531322F002E00",
        "dev_addr": "260B7D71"
    },
    "payload": {
        "downlinks": [{
            "f_port": 15,
            "frm_payload": msg.payload.toString("base64"),
            "priority": "NORMAL"
        }]
    }
}

```

When I downlink the message, the debug window shows the next:

msg.payload : Object

object

downlinks: array[1]

0: object

f\_port: 15

frm\_payload: "17,34,51"

priority: "NORMAL"

The euis and IDs are the same than those I receive when I uplink data to the Node Red MQTT node.

Please, may you help me in developing the right function to get the message arrives to the end device in the TTN network? Excuse me if I have limited knowledge on this and this could be more a sintaxis issue.

Thank you very much in advance for your help.

Regards,  
Joel

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [9 October 2022 12:58 UTC](https://discourse.nodered.org/t/sintaxis-in-function-for-downloading-a-mqtt-downlink-message-to-a-device-in-ttn/68759/2 "2022-10-09T12:58:05Z")

</div>

I don't use any of that equipment nor do I use LoRa - however, [looking at other posts](https://discourse.nodered.org/t/extracting-data-from-a-ttn-payload-for-insertion-into-mariadb/51381/8) I suspect your message structure is not right.

Try this instead...

```auto
msg.payload = {
    "end_device_ids":{
        "device_id": "eui-70b3d57ed005629d",
        "application_ids": {
            "application_id": "rfm6601-test-1"
        },
        "dev_eui": "70B3D57ED005629D",
        "join_eui": "323531322F002E00",
        "dev_addr": "260B7D71"
    },
    "downlinks": [{
            "f_port": 15,
            "frm_payload": msg.payload.toString("base64"),
            "priority": "NORMAL"
    }]
}
return msg;

```

(this ↑ is pure guess work - if it does not work, point me to documentation and I'll try to help further)

---

<div class="post-metadata">

### Author: ![jinvers](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jinvers](https://discourse.nodered.org/u/jinvers)
#### Post date: [9 October 2022 14:29 UTC](https://discourse.nodered.org/t/sintaxis-in-function-for-downloading-a-mqtt-downlink-message-to-a-device-in-ttn/68759/3 "2022-10-09T14:29:41Z")

</div>

Thank you for your quick proposal.  
I have tested the changes but I have not seen that the downlink data could arrive to the TTN Application.  
What I have been able to recover from the former version of the function that I published, is a message like this:  
"Failed to transmit downlink".  
And the even details:

{  
"name": "ns.down.transmission.fail",  
"time": "2022-10-09T11:43:17.701176736Z",  
"identifiers": [  
{  
"device\_ids": {  
"device\_id": "eui-70b3d57ed005629d",  
"application\_ids": {  
"application\_id": "rfm6601-test-1"  
}  
}  
}  
],  
"context": {  
"tenant-id": "CgN0dG4="  
},  
"visibility": {  
"rights": [  
"RIGHT\_APPLICATION\_TRAFFIC\_READ"  
]  
},  
"unique\_id": "01GEY8WYP5N9QM02MWQY3Q4R97"  
}

Regards,  
Joel

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [8 December 2022 14:30 UTC](https://discourse.nodered.org/t/sintaxis-in-function-for-downloading-a-mqtt-downlink-message-to-a-device-in-ttn/68759/4 "2022-12-08T14:30:32Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
