Someone here could you please help me with a sample of payload parser code to extract the value (marked in blue) and put it in the yellow marked variables? The code needs also handle with different topics (e.g. “topic”: “unit/bra”, “topic”: “unit/arg” etc). In the samples available, I didn’t find any case similar to this one, and I believe that it can also help other Tago.IO users trying some projects.
/*** Parses the value of the reading** Value Value of the entry. Optional if a Sum value is present,* otherwise required. Values are represented using three basic data* types, Floating point numbers ("v" field for "Value"), Booleans* ("vb" for "Boolean Value") and Strings ("vs" for "String Value").* Exactly one of these three fields MUST appear.* @param {Object} item* @returns {number | boolean | string}*/function parseValue(item) {if ('vb' in item) {return !!item.vb;}if ('v' in item) {return Number(item.v);}if ('vs' in item) {return item.vs;}}/*** Parses the measurement time* If either the Base Time or Time value is missing, the missing* attribute is considered to have a value of zero. The Base Time and* Time values are added together to get the time of measurement. A* time of zero indicates that the sensor does not know the absolute* time and the measurement was made roughly "now". A negative value is* used to indicate seconds in the past from roughly "now". A positive* value is used to indicate the number of seconds, excluding leap* seconds, since the start of the year 1970 in UTC.* @param {Moment | Date} curr_time current time when this code is running* @param {Object} item measurement object* @returns {Moment}*/function parseTime(curr_time = moment(), item) {if (!item.t) {return moment(curr_time).toISOString();}if (Number(item.t) < 0) {return moment(curr_time).subtract(item.t * -1, 'seconds').toISOString();}return moment(curr_time).add(item.t, 'seconds').toISOString();}/*** Removes unnacepted parameters from the variable name* @param {string} variable*/function parseVariable(variable) {const variableParsed = variable.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');if (!variableParsed) {return 'measurement';}return variableParsed;}/**** @param {Object[]} senml_obj*/function decoder(senml_obj) {const toTagoJSON = [];const serie = String(new Date().getTime());let curr_time = moment();let base_unit;let base_name;for (const item of senml_obj) {if (item.bt) {curr_time = moment(item.bt, 'X');}if (item.bn) {base_name = item.bn;}if (item.bu) {base_unit = item.bu;}const itemTago = {variable: parseVariable(item.n || base_name),unit: item.u || base_unit,value: parseValue(item),time: parseTime(curr_time, item),serie,group: serie,};toTagoJSON.push(itemTago);}return toTagoJSON;}try {if (Array.isArray(payload) && payload[0].bn) {payload = decoder(payload);}} catch (e) {console.log(e.message);}
Hey Fabiano! Tks for your reply! Follow my comments below in yellow: (more questions/doubts
1- You need to create an Action MQTT and setup the topics that you are sending data, see how to create it here.
I noticed that the “Action MQTT” link to the Knowledge page that you shared, is a little bit different that what I found when creating a new action, probably it was updated. Just to be clear/detailed, you asked: create an Action Type = MQTT Topic, Type of Action = Insert to the Device Bucket, Select the Device = “Device that is sending the MQTT messages” and in the Subscribe section, “add the topics that I want to listen”, Is this step-by-step right?
2- Build your own parser based on a snippet: “Parse example for senML”
The code/snippet that you shared is just an example, reference, not the final code to handle with the payload that I shared, right? I am asking it because in the Live Inspector I see after the “MQTT Device Publish” and “Raw payload”: “SyntaxError: The only valid meta property for new is ‘new.target’”
I am new with TagoIO (testing and understanding how it works), and so far I understood, even for basic cases such as this one that I shared, dev knowledge/team is a pre-requisite to use Tago.IO plataform. Is it also correct?
3- There is also a basic concept regarding the platform that is not clear for me, how the platform convert the payload information provided in variables available to be used in the dashboards? Some specific syntax of variable declared in the parser code is the process? Where I can search for the variables “available” and their details (source, current value etc)? I really appreciate if you can give me more details about how it works.
1 - Yes, it’s right. You also can use wildcards to set your topic as described in the documentation.
2- Yes, it’s just a start guide to you build your own parser rule. What sensor are you using? Remember that TagoIO has a lot of sensors with the parser done you can see them when creating a new device.
Code knowledge is mandatory just for payload parser and Analysis scripts.
3- The payload parser must result in an array object as described in this documentation.
The data from devices is stored in buckets, you can access the bucket inside of the device.