Sending as much data as you want for the price of 1 data input

Sending as much data as you want for the price of 1 data input

@Kelvin Welter

Is this possible?

Yes, that’s possible. Today I will teach you how you can save money by sending multiple data inputs as if it was only 1. To do this, we must build a custom Payload Parser, you may never have done this before, but I will demonstrate just how simple it is.

image

Why should I build a custom Payload Parser?

The main reason for you to do this is that it enables you to merge your variables into the metadata of a “main variable”. And so we are taking advantage of the fact that you can put whatever you want into the metadata of the variables we want and have it only count as 1 data input.

Ok, but what are the trade-offs?

This practice saves you money, but there are some disadvantages to it. The first disadvantage is that you will not be able to use variables within the metadata in Actions. So, if you need to use Actions on a variable, don’t put it in the metadata. Another disadvantage is that the data within the metadata will not be indexed, which means that you will not be able to work with it in analysis.

As you can see, if your data will need some processing like Analysis or Actions, it is not recommended that you submit it within metadata. Otherwise, you can send them through metadata and show it in widgets later using the Formula feature. I explain this process in more depth in this post (Using Formula to display metadata values for my variable).

How can I implement it into my application?

Finally, I will show you how to implement this method into your application. Imagine that you have 4 variables that are: current, frequency, voltage and power. Of these 4 variables, your Actions and Analyzes only process the voltage variable. From what we saw earlier, we now know that the other 3 variables can be placed in the voltage variable metadata.

So here is what we are going to do; Go to the Device that is sending the data and go to the Payload Parser tab. Then, enable the option “Run your own parser”.

Note that in the image above, when you enable this option, a snippet comes ready in the editor. With some changes to this snippet, we will be ready to send multiple data inputs as one. The next step is to configure the variable that I highlighted in the image above: ignore_vars. This variable is what will ignore data that we don’t want to be counted.

So, since I want the variables current, frequency and power to be in the metadata of the voltage variable, and I don’t want them to be counted as a data input, I will put these variables inside that array and it will look like this:

const ignore_vars = ['current', 'frequency', 'power']; 

After that, I search for the variables I want and insert them into the voltage variable metadata. With all this, the code looks like this:

// Add ignorable variables in this array. 
const ignore_vars = ['current', 'frequency', 'power']; 
 
// Getting the variables I want to insert into the tension variable metadata 
const current = payload.find((x) => x.variable === 'current'); 
const frequency = payload.find((x) => x.variable === 'frequency'); 
const power = payload.find((x) => x.variable === 'power'); 
 
// Getting the index of the tension variable 
const tensionIndex = payload.findIndex((x) => x.variable === 'tension'); 
 
// If the tension does not have metadata field, I add a 
// new object to the metadata field 
if (tensionIndex !== -1 && !payload[tensionIndex].hasOwnProperty('metadata')) { 
  payload[tensionIndex].metadata = {} 
} 
 
// After all that is complete, I add my variables into the metadata 
if (tensionIndex !== -1) { 
  payload[tensionIndex].metadata.current = current.value; 
  payload[tensionIndex].metadata.frequency = frequency.value; 
  payload[tensionIndex].metadata.power = power.value; 
} 

// Remove unwanted variables. 
payload = payload.filter(x => !ignore_vars.includes(x.variable)); 

The code is all commented upon so that you may understand it better and so that you can adapt it to use with the variables you want. If you have any questions related to the code, ask here in this post.

Well, that’s what I wanted to teach you in this post. Remember, to display the data that is in the metadata, you must use the Formula feature. Take a look at this post talking about it (Using Formula to display metadata values for my variable).

Thanks!