Display device name on map

Hi everyone, I’m trying to show the name of the device on the map widget, but am not sure how to incorporate it into my payload parser…if that’s the way to even do it…
The gps device is correctly set up and is showing location of device on the map.
What I would like is for the Device to show the name…where “Mocha” is in the image below.
image
Here is my payload parser, showing where I need the device_name to be referenced.

console.log(payload);

const payload_raw = raw_payload.find(x => x.variable === ‘ttn_payload’);

if(payload_raw){

try {

const payload_data = JSON.parse(payload_raw.value);

const data = [

  { variable: 'heading', value: payload_data.payload_fields.headingDeg },

  { variable: 'speed', value: payload_data.payload_fields.speedKmph, unit: 'km/h' },

  { variable: 'battery_voltage', value: payload_data.payload_fields.batV, unit: 'V' },

  { variable: 'location', value: 'need device_name here', location: { lat: payload_data.payload_fields.latitudeDeg, lng:  payload_data.payload_fields.longitudeDeg } },

  ]

 payload = data;

} catch (e) {

// Print the error to the Live Inspector.

console.error(e);

// Return the variable parse_error for debugging.

payload = [{ variable: 'parse_error', value: e.message }];

}

}

Hi @sevils, because the map widget only accepts to read the variables of the device, you will need to put it (the name) into your parser, so your payload should look something like that:

let payload = [{ variable: "ttn_payload", value: "..." }, { variable: "device_name", value: "Mocha" }];

then what you could do so you don’t need to change your parser too much, is the following: in the line where you wrote payload = data, you can do payload = payload.concat(data); that way you would save the device_name as well as the ttn_payload for debugging (if needed).

and if you need to know which variables were parsed together, you can insert a serie to all variables, after you done everything needed with the payload, you can generate a serie number: const serie = new Date().getTime(); and then insert it into all variables doing so: payload = payload.map((x) => ({ …x, serie }));

so it should look something like that:

    const payload_raw = payload.find((x) => x.variable === "ttn_payload");
    const device_name = payload.find((x) => x.variable === "device_name");
    if (payload_raw && device_name) {
        try {
            const payload_data = JSON.parse(payload_raw.value);
            const data = [
                { variable: "heading", value: payload_data.payload_fields.headingDeg },
                { variable: "speed", value: payload_data.payload_fields.speedKmph, unit: "km/h" },
                { variable: "battery_voltage", value: payload_data.payload_fields.batV, unit: "V" },
                { variable: "location", value: device_name.value, location: { lat: payload_data.payload_fields.latitudeDeg, lng:  payload_data.payload_fields.longitudeDeg } },
            ]
            payload = payload.concat(data);
            const serie = new Date().getTime();
            payload = payload.map((x) => ({ ...x, serie }));
        } catch (e) {
            console.error(e);
            payload = [{ variable: "parser_error", value: e.message }];
        }
    }
1 Like