Function node -local variables-

Can you use local or time variables within the code?
It seems that they are reset at each cycle and not at startup vome I would expect.
in the case how to get the loss of persistence at startup but not in the cycle?

It is fundamental to Node-red that generally, each node works with the message sent to it without any knowledge of previous messages.

So in a function node a variable is initialised afresh for each message. Once the message is processed the variable is destroyed.

There is of course provision for variables outside of the flow of messages - context variables.
Their scope can be confined to a single node, an editor tab (flow) or global.

let subtotal = context.get('subtotal') ?? 0  // or flow.get, global.get
subtotal += msg.payload
context.set('subtotal', subtotal)
1 Like

If @jbudd's suggestion does not solve your problem then please give an example of exactly what you mean.

// Definisci la variabile 'chiave' all'inizio del codice, così puoi modificarla facilmente
let chiave = "GPIOIN_7";  // Sostituisci questa stringa con la chiave che desideri utilizzare

// Recupera il valore precedente dal flusso
let valoreMemorizzato = flow.get('valoreMemorizzato') || ''; // Usa una stringa vuota come valore di default

// Inizializza lo stato
let stato = "Online"; // Lo stato predefinito è "Online"

// Verifica se il messaggio in ingresso è "agg"
if (msg.payload === "agg") {
    // Se la richiesta è "agg", invia l'ultimo valore memorizzato su out1
    msg.payload = valoreMemorizzato;  // Invia il valore precedente memorizzato
    let obj = createObject();         // Crea l'oggetto da inviare su out2
    node.send([msg, { payload: obj }, { payload: stato }]);  // Invia il messaggio su out1, l'oggetto su out2, e lo stato su out3
    return null;  // Non inviare nuovamente il messaggio
}

// Normalizzazione dell'ingresso (gestisce 1/0, ON/OFF)
if (msg.payload === 1 || msg.payload === "ON" || msg.payload === "on") {
    msg.payload = "ON";
} else if (msg.payload === 0 || msg.payload === "OFF" || msg.payload === "off") {
    msg.payload = "OFF";
} else {
    msg.payload = "Invalid input";  // Gestione di casi imprevisti
    stato = "Offline"; // In caso di input non valido, imposta lo stato su "Offline"
}

// Confronto con il valore precedente
if (msg.payload !== valoreMemorizzato) {
    // Se il valore è diverso, invia il messaggio su out1 e l'oggetto su out2
    flow.set('valoreMemorizzato', msg.payload);  // Memorizza nel contesto di flusso
    let obj = createObject();                    // Crea l'oggetto da inviare su out2
    node.send([msg, { payload: obj }, { payload: stato }]);  // Invia il messaggio su out1, l'oggetto su out2, e lo stato su out3
    return null;  // Non inviare il messaggio direttamente
} else {
    // Se il valore è lo stesso, non inviare nulla (evitare operazioni non necessarie)
    return null;  // Ignora il messaggio
}

// Funzione per creare l'oggetto da inviare su out2
function createObject() {
    return {
        "name": chiave,  // Usa la variabile chiave
        "stat_t": `DATI FOTOVOLTAICO/INGRESSI/${chiave}`,  // Usa la variabile chiave
        "avty_t": `DATI FOTOVOLTAICO/INGRESSI/${chiave}/LWT`,  // Usa la variabile chiave
        "cmd_t": `DATI FOTOVOLTAICO/INGRESSI/${chiave}/command`,  // Usa la variabile chiave
        "pl_avail": "Online",
        "pl_not_avail": "Offline",
        "uniq_id": chiave,  // Usa la variabile chiave
        "icon": "mdi:electric-switch",
        "dev": {
            "ids": ["Quadro Domotica"],
            "name": "Quadro Domotica",
            "mf": "TopPino",
            "mdl": "Aggregatore mqtt",
            "sw": "0.1",
            "sa": "Fotovoltaico",
        },
    };
}

in this code I need to forget the ultimate value but can’t figure out how to do

valoreMemorizzato 

I have to reset it

Not sure I understand the question but you can delete a context variable with a change node.

In a function saving a value of undefined will delete it.

I’m crazy about this thing, you can give me a specific example on the code posted, what I want to get is that at the first start does not have to perform a comparison but send directly the value on the outputs (3). to get this I’m trying to make forget the value saved in context, another alternative is just skip the comparison, but only at a restart of the node, during normal flow must make comparisons to avoid repeated sends.

Apologies Giuseppe but it's clear English is not your first language and I have no Italian.

I can not tell what you are trying to do.

Can you perhaps explain again using very short sentences?
A diagram might help too.

If necessary we can use the internet to translate.

I’m using a translator... I’m Italian.
I will try to make a diagram

If you want to know if this is the first time through a function node since a node-red restart you can do

let firstTime = context.get("firstTime") ?? true
if (firstTime) {
  context.set("firstTime", false)
  // do things you want to do the first time
} else {
  // do things when it is not the first time
}

[Edit] That is unless, in settings.js, you have set the default context storage to 'file', in which case you would have to specify the memory context in the get and set functions.