@vitorfdl thanks for the response!
A good suggestion. In the more verbose code (below) it does find the desired variable but still no new sendData value is seen after execution. No authorization error are seen either so it seems the access policy is ok.
Any other ideas?
const { Analysis, Device, Resources } = require("@tago-io/sdk");
async function myAnalysis(context, scope) {
try {
const variable = scope[0];
const device_id = variable.device;
// Get device token
const tokens = await Resources.devices.tokenList(device_id);
const device_token = tokens[0]?.token;
if (!device_token) {
return context.log("No token found for device");
}
const device = new Device({ token: device_token });
// Read last volt
const data = await device.getData({ variable: "volt", query: "last_item" });
console.log(`Found ${data.length} records for my_key`);
if (!data.length) {
return context.log("No previous volt data found.");
}
context.log(`Previous volt: ${data[0].value}`);
// Write new volt
await device.sendData({
variable: "volt",
value: 1234,
metadata: { source: "analysis_test" },
});
context.log("✅ Sent volt = 1234");
} catch (error) {
context.log("Analysis error:", error.message || error);
}
}
Analysis.use(myAnalysis);