Wrong variable provided in scope

Hello there,

We are currently having a problem with the type of variable being provided in the scope.
We are running an analysis that only runs when the temperature variable is of certain value. The purpose of our analysis is to send out a notification to the user regarding the device and variable information. We are getting the correct device but sometimes wrong variable.
For example:

{“hwisel_user_id”:“EQVi1m”,“device_id”:“5f43e10a36cf9100276ec6d6”,“message”:“The temperature on your heater tank is below 45c for the device: Sensative Humidity Sensor (Solomon). Variable: historyseqnr, Value: 65535”,“deviceName”:“Sensative Humidity Sensor (Solomon)”}

When the correct output we get sometimes is:

{“hwisel_user_id”:“EQVi1m”,“device_id”:“5f43e10a36cf9100276ec6d6”,“message”:“The temperature on your heater tank is below 45c for the device: Sensative Humidity Sensor (Solomon). Variable: temperature, Value: 15”,“deviceName”:“Sensative Humidity Sensor (Solomon)”}

The correct variable in this case is temperature. I will provide you the code on how we are getting the variable value. Please note that the analysis should only run when the variable is temperature.

async function init(context, scope) {

if (scope[0].variable == “temperature”){
var message_string = The temperature on your heater tank is below 45c for the device: ${device_name}. Variable: ${your_variable.variable}, Value: ${your_variable.value}
….
}
}

If you can assist us on this, we would greatly appreciate it.

Thanks!
Daniel

Hi Daniel,
That happens because the “scope” from the analysis doesn’t include only the variable that was triggered by the action, but all other variables inserted at same request.

You can easily check what you’re receiving on the scope variable by adding this to the code:

context.log(JSON.stringify(scope));

Which I recommend you to do, so you can have better understanding of what you’re working with.

In order to make sure you’re getting the correct variable, do this:

const temperature_var = scope.find(data => data.variable === "temperature"); // search scope for the data with param variable = temperature. Returns it if found.
if (temperature_var) {
var message_string = `The temperature on your heater tank is below 45c for the device: ${device_name}. Variable: ${temperature_var.variable}, Value: ${temperature_var.value}`
}