Unable to use device.sendData() function

Hi,

I am trying to create an analysis using device.sendData() without success, even with a simple code based on the snippet Operate data from device:

/*
 * Analysis Example
 * Operate data from devices
 *
 * Read information from a variable generated by devices,
 * run a simple calculation in real-time, and create a new variable with the output.
 *
 * Instructions
 * To run this analysis you need to add a device token to the environment variables,
 * To do that, go to your device, then token and copy your token.
 * Go the the analysis, then environment variables,
 * type device_token on key, and paste your token on value
 */

const { Analysis, Device, Utils } = require("@tago-io/sdk");

// The function myAnalysis will run when you execute your analysis
async function myAnalysis(context) {
  // reads the values from the environment and saves it in the variable env_vars
  const env_vars = Utils.envToJson(context.environment);

  if (!env_vars.device_token) {
    return context.log("Missing device_token environment variable");
  }

  const device = new Device({ token: env_vars.device_token, region: "usa-1" });

  // Multiplies the water_level value by 2 and inserts it in another variable
  const obj_to_save = {
    variable: "water_level_double",
    value: 10,
  };

  try {
    await device.sendData(obj_to_save);
    context.log("Successfully Inserted");
  } catch (error) {
    context.log("Error when inserting:", error);
  }
}

module.exports = new Analysis(myAnalysis);

// To run analysis on your machine (external)
// module.exports = new Analysis(myAnalysis, { token: "YOUR-TOKEN" });

I got the device token from:

Any obvious thing I missed? Thanks

Hi @ogimenez ,

Did you set the device_token on environment variables in your analysis? If so it should work, btw sending the error log message which is thrown on your console is always helpful as well.

-Guilherme

1 Like

Hi @guilhermeco,

Sorry I thought I did include the error from the console, here it is:

[2021-11-15 23:07:05] Error when inserting: Function Parse Error: TypeError: Cannot read property 'value' of undefined 

Then yes I did set the env variable:

You’re sending an undefined value. if you set the value to hardcoded 10 you as the code you sent , you will see the error will vanishes.

Try running the following code and you will see it will work:

/*
 * Analysis Example
 * Operate data from devices
 *
 * Read information from a variable generated by devices,
 * run a simple calculation in real-time, and create a new variable with the output.
 *
 * Instructions
 * To run this analysis you need to add a device token to the environment variables,
 * To do that, go to your device, then token and copy your token.
 * Go the the analysis, then environment variables,
 * type device_token on key, and paste your token on value
 */

const { Analysis, Device, Utils } = require("@tago-io/sdk");

// The function myAnalysis will run when you execute your analysis
async function myAnalysis(context) {
  // reads the values from the environment and saves it in the variable env_vars
  const env_vars = Utils.envToJson(context.environment);

  if (!env_vars.device_token) {
    return context.log("Missing device_token environment variable");
  }

  const device = new Device({ token: env_vars.device_token });

  // Multiplies the water_level value by 2 and inserts it in another variable
  const obj_to_save = {
    variable: "water_level_double",
    value: 2,
  };

  try {
    await device.sendData(obj_to_save);
    context.log("Successfully Inserted");
  } catch (error) {
    context.log("Error when inserting:", error);
  }
}

module.exports = new Analysis(myAnalysis);

// To run analysis on your machine (external)
// module.exports = new Analysis(myAnalysis, { token: "YOUR-TOKEN" });

You’re sending an undefined value. if you set the value to hardcoded 10 you as the code you sent , you will see the error will vanishes.

Unless I am mistaken, the value is already hardcoded to 10. What is the difference in your code compared to mine except you changed the 10 to 2? Anyway I still get the same error:

[2021-11-18 14:26:55] Duration: 1.2s Billed Duration: 2s.
[2021-11-18 14:26:55] Analysis finished.
[2021-11-18 14:26:55] Error when inserting: Function Parse Error: TypeError: Cannot read property 'value' of undefined
[2021-11-18 14:26:55] > TagoIO-SDK: No region or env defined, using fallback as usa-1.
[2021-11-18 14:26:54] Starting Analysis...

Hi @ogimenez ,

Indeed it doesnt seems to be much different than what you’ve mentioned, can you please copy the code I’ve provided and see if the error still persists?

On my side it’s working and you should get this working as well.

-Guilherme

Hi @guilhermeco , I still get the error with your code

Hi!
Does your device use a custom payload parser?
Maybe you get the the error because the parser can’t parse the data you send to your device.
If you disable your own parser on the device the code what provided by @guilhermeco will work.

Hi,
also check if your device is not set to “Active”,
I got this same error and took some time to find out why.

@lonevo @adriano.pellegrin good catch gentlemen! :slight_smile:

Hi, I tried to disable the payload parser but I still have the same issue

Hi @ogimenez ,

can you please try to create a new HTTPS device and try using it? Remember to copy the token from the new device created.

Let us know if it works now. If so we can isolate the analysis. (I mean, not a problem related to it)

1 Like

Hi @ogimenez,
Please, go to your Device’s page, and then go Live Inspector tab and press play.

Then run your script and you should be able to see the logs filling up your Live Inspector. If you can copy the Live Inspector content and place it here, it will help a lot to understand where the error is happening.

1 Like