How to save storage and I/O using metadata

In this article, we are going to learn how to work with metadata and display their values in a dashboard.

Metadata can be used to drastically reduce the number of Input/Output transactions and register storage used. Metadata simply allows us to add extra data by appending it to a variable.

Important things to know before using metadata:

  1. Metadata information is not checked by the Action. - This means you won’t be able to trigger an Action based on specific variable that is inside your metadata object, and you’ll have to switch the key over to the variable’s value.

  2. You need to you formulas to display the metadata in the widget. - Good news is that this is very easy to do.

  3. It is not possible to create an Analysis that will easily access the metadata information, it is necessary to know which main variable the metadata is associated with. - This is because when creating a query you won’t be able to filter the device data by the metadata key.

  4. There is a limit of metadata size per post, today it is set to 10kB.

Still, metadata can be very helpful as it provides a very cost effective way to store data. So here’s an example on how to use metadata:

Step 1 - Set the JSON to come like the example below:

Step 2 - Through the live inspector you should receive a response as below:

Step 3 - To make sure, go to your buckets and check the “metadata” column to see if you are receiving the correct format

Step 4 - Go to your widget, select the primary variable and then go to the formula settings. Replace the $METADATA.color$ formula with for example, $METADATA.percentualload$ and the fix Unit if applicable as seeing below:

Step 5 - Don’t forget to change the alias as it will grab the name of the primary variable, we need to change it to the name of the actual variable.

Result:

Hi Freddy Minuzzi,

Do you have some tutorial or code example from access the metadata information with a analysis?

Hi Gabriel!

Here’s an example which is sending a variable with a bunch of metadata with it:

Along with a dashboard to start it.

const { Analysis, Resources } = require(“@tago-io/sdk”);

function getRandomFloat(min, max) {

return Math.random() * (max - min) + min;

}

function getRandomInt(min, max) {

return Math.floor(Math.random() * (max - min + 1)) + min;

}

async function sendMetadata(context, scope) {

const device_id = scope[0].device;

const onlinePercentage = getRandomInt(0, 100);

const offlinePercentage = 100 - onlinePercentage;

const obj_to_save = {

variable: “data”,

value: “no data in the value”,

metadata: {

“temperature”: getRandomFloat(20, 40),

“rpm”: getRandomInt(0, 5000),

“oil_level”: getRandomInt(0, 100),

“gasoline_level”: getRandomInt(0, 100),

“button1status”: Math.random() < 0.5 ? “pushed” : “not pushed”,

“button2status”: Math.random() < 0.5 ? “pushed” : “not pushed”,

“online”: onlinePercentage,

“offline”: offlinePercentage,

“humidity”: getRandomFloat(60, 90)

}

};

try {

await Resources.devices.sendDeviceData(device_id, obj_to_save);

context.log(“Successfully Inserted”);

} catch (error) {

context.log(“Error when inserting:”, error);

}

}

module.exports = new Analysis(sendMetadata);

Thanks for your replay Freddy!

But I need an analysis to extract a value from a variable contained in the metadata to use this value in some aplications such as formulas and export the variable in file .csv

Hi, Freddy!

I have a variable in metadata with a “-”, the name is “generator_l1-n_voltage”, I trying using in a chart but I give a error, can you help with this?