clearTimeout

clearTimeout

I would like to know why the following code when the condition that the last variable was "Undefined_Close" is met and the second to last was === "Open" cancels the SetTimeout of the "Undefined_Open" that is in process. Because if an "Open" data arrives but just during the creation time interval of the "Undefined_Open" there is a "Close" and this itself has a shorter creation time interval than the Open, then you should cancel the " Undefined_Open".

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

async function hdaEncantada(context) {
  try {
    const env_vars = Utils.envToJson(context.environment);
    const strega7_master = new Device({ token: env_vars.strega7_master });
    const vivid_master = new Device({ token: env_vars.vivid_master });
    const vivid_data = new Device({ token: env_vars.vivid_data });

    const result_master = { variable: "state", qty: 4 };
    const array_master = await vivid_master.getData(result_master);
    const state_master = array_master.map((data) => data.value);

    console.log("STATE MASTER -->", state_master);

    const result_data = { variable: "master_prueba", qty: 1 };
    const array_data = await vivid_data.getData(result_data);
    const state_data = array_data.map((data) => data.value);

    console.log("master -->", state_data);

    const result_ahorro = { variable: "ahorro_prueba", qty: 1 };
    const array_ahorro = await vivid_data.getData(result_ahorro);
    const state_ahorro = array_ahorro.map((data) => data.value);

    console.log("ahorro -->", state_ahorro);

    let undefined_OpenId;
  let sendUndefinedOpen = true;

    if (state_master[0] === "Open") {
     
      if (state_master[1] === "Close") {
        return;
      } else {
     undefined_OpenId = setTimeout(() => {
          const newDate = {
            variable: "state",
            value: "Undefined_Open",
          };
          vivid_master.sendData(newDate);
        }, 60 * 1000);
      }
    } else if (state_master[0] === "Undefined_Open") {
      if (state_master[1] === "Close") {
        return;
      }  else {
        // APAGAR AIRE MASTER
        const newMaster = {
          variable: "switch_simulacion",
          value: 0,
        };
        await strega7_master.sendData(newMaster);
        console.log("MASTER --> APAGADO");
      }
    } else if (state_master[0] === "Close") {

      if (state_master[2] === "Close") {
        return;
      } else {
        setTimeout(() => {
          const newDate = {
            variable: "state",
            value: "Undefined_Close",
          };
          vivid_master.sendData(newDate);
        }, 15 * 1000);
      }
    } else if (state_master[0] === "Undefined_Close") {
      if (state_master[2] === "Close") {
        return;
      }  else if (state_master[2]=== "Open"){
    clearTimeout(undefined_OpenId);
    context.terminate()
        }else {
        // ENCENDER AIRE MASTER
        const newMaster = {
          variable: "switch_simulacion",
          value: 1,
        };
        await strega7_master.sendData(newMaster);
        console.log("MASTER --> ENCENDER");
      }
    }
  } catch (error) {
    console.log(error);
  }
}

module.exports = new Analysis(hdaEncantada);