Hi Gabriel,
Great! So from what I understood, you can fix this by running an Analysis, the Analysis would then verify if the minimum humidity is below the maximum humidity.
Does this input form already trigger an Analysis when you submit data?
Heres an example of an Analysis that could acheive this, its using the device-token but you can use “scope[0].device” if you wish to use the device you configured in the input form.
const { Analysis, Resources, Utils, Services } = require(“@tago-io/sdk”);
/**
* Analysis to validate and process humidity data from an input form.
* @param {TagoContext} context - The Tago context object.
* @param {Data} scope - The scope containing data from the dashboard and actions.
*/
async function humidityAnalysis(context, scope) {
// Assuming ‘max_humidity’ and ‘min_humidity’ are passed in the scope.
const maxHumidityData = scope.find((data) => data.variable === ‘max_humidity’);
const minHumidityData = scope.find((data) => data.variable === ‘min_humidity’);
if (!maxHumidityData || !minHumidityData) {
console.error(“Max or Min Humidity data not found.”);
return;
}
const maxHumidity = Number(maxHumidityData.value);
const minHumidity = Number(minHumidityData.value);
// Check if the max humidity is not bigger than the min humidity.
if (minHumidity >= maxHumidity) {
// Notify the user of the error.
const notificationService = new Services({ token: context.token }).notification;
await notificationService.send({
title: “Humidity Data Error”,
message: “Min humidity cannot be higher or equal to max humidity.”,
});
console.error(“Min humidity cannot be higher or equal to max humidity.”);
return;
}
// If there’s no error, add the data to the device.
const deviceID = “enter-your-device-id-here”; // Replace with your actual device ID.
await Resources.devices.sendDeviceData(deviceID, [
{ variable: “max_humidity”, value: maxHumidity },
{ variable: “min_humidity”, value: minHumidity },
]);
console.info(“Humidity data successfully added to the device.”);
}
Analysis.use(humidityAnalysis);