Analisysy - If last_input > X create variable device_status with value online/offline - possible ?

Analisysy - If last_input > X create variable device_status with value online/offline - possible ?

Hello

Trying to create  analisys which checking last_input , when bigger then checkin_time (this value i can set env_variable )  then create variable device_status with value offline , when checkin is ok create variable with value online.



I trying with below analisys but not success 


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

async function myAnalysis(context) {
  // Transform all Environment Variable to JSON.
  const env = Utils.envToJson(context.environment);

  if (!env.account_token) {
    return context.log("You must setup an account_token in the Environment Variables.");
  } else if (!env.checkin_time) {
    return context.log("You must setup a checkin_time in the Environment Variables.");
  } else if (!env.tag_key) {
    return context.log("You must setup a tag_key in the Environment Variables.");
  } else if (!env.tag_value) {
    return context.log("You must setup a tag_value in the Environment Variables.");
  }

  const checkin_time = Number(env.checkin_time);
  if (Number.isNaN(checkin_time)) return context.log("The checkin_time must be a number.");

  const account = new Account({ token: env.account_token });

  const devices = await account.devices.list({
    page: 1,
    amount: 1000,
    fields: ["id", "name", "last_input"],
  });

  if (!devices.length) {
    return context.log(`No device found with given tags. Key: ${env.tag_key}, Value: ${env.tag_value}`);
  }

  context.log("Checking devices: ", devices.map((x) => x.name).join(", "));

  const now = dayjs();
  const alert_devices = [];

  for (const device of devices) {
    const last_input = dayjs(new Date(device.last_input));
    // Check the difference in minutes.
    const diff = now.diff(last_input, "minute");
    // Determine the device status
    const status = diff > checkin_time ? "offline" : "online";
    context.log(`Device ${device.name} is ${status}`);

    // Add/update the variable "device_status" for the device
    await account.devices.variables.upsert({
      device_id: device.id,
      data: [
        {
          variable: "device_status",
          value: status,
        },
      ],
    });
  }

  context.log('OFFLINE DEVICE ID LIST: ', alert_devices);

  // ... Rest of your code ...

}

module.exports = new Analysis(myAnalysis);