form_payload encoding for downlink analysis

Hi,
I use the template “Sending downlink using dashboard” to send a parameter (number of seconds, between 0 and 3600) to a device through TTN.
I created an Input Form and trigger the analysis from it.
When the input is of type “Number”, and I put for example ‘182’, the analysis log sends an error:
The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received type number (182)”.
If I put other types of input, eg. Clock, or text, it seems the input is iterperted as Hexadecimal data. This means it should be a series of characters between 0 and F, (otherwise I get an error again from the script), and when received by my device, the payload is decoded from hexadecimal.
Some examples:
If input is ‘15’, the devices shows ‘21’ (= 1x16 + 5 ).
If input is ‘2345’, TTN downlink console shows ‘23 45’ and the device shows 35 and 69.
If input is ‘abcdef’, TTN downlink console shows ‘ABCDEF’ and the device shows 171 205 239.
If the input is ‘XYZ’, the scripts shows an error “Cannot read property ‘data’ of undefined”.

Can you help about having a simple and efficient encoding in the Analysis script so that data (number or text) is properly transferred, and received and decoded as the original data?

Hi philippe,
I’m not sure I understand your issue. The Downlink template expects to receive a Hexadecimal value, with the value of the form_payload to be always a string.

If you plan to develop some kind of encoding, you need to edit the script to make sure the value of the payload sent by the axios function is a hexadecimal string.

Some examples on how to do that is editing the line 40:

const payload = scope.find(x => x.variable === 'form_payload') || { value: env.payload, origin: env.device_id };

to something like this:

let payload = scope.find(x => x.variable === 'form_payload') || { value: env.payload, origin: env.device_id };
payload.value = String(payload.value) // force to string

or

let payload = scope.find(x => x.variable === 'form_payload') || { value: env.payload, origin: env.device_id };
payload.value = Number(payload.value).toString(16) // force to hexadecimal

or

let payload = scope.find(x => x.variable === 'form_payload') || { value: env.payload, origin: env.device_id };

// add manual encoding with an IF
if (payload.value === "11") {
 payload.value = "001100";
}

Hi Vitor,
I think this is exactly what I need. I’m not a JS developer and didn’t know what the Analysis expects to receive and has to send to TTN, nor how to force hex encoding in this language. So probably you did put everything I need here. I will study and adapt your example.
Thanks again.
Philippe