Utilisation global.set et global.get

Bonjour à tous je cherche a détecter un changement d'état d'un shelly button pour détecter que je suis absent,
j'ai décider de faire cette fonctionnalité avec le noeud function.
Auparavant j'ai essayé avec les noeud filter sur changement mais le résultat est pas satisfaisant
Dans le noeud function les global.get et global.set et surtout le global.set ne fonctionne pas

Voici mon code certainement perfectible

let compteur = Number(global.get("homeassistant.homeAssistant.states['input_number.Presence_compteur'].state")|| 0);
let packet_id = Number(global.get("homeassistant.homeAssistant.states['sensor.bthome_sensor_c1d6_packet_id'].state") || 0);
let memo = Number(global.get("homeassistant.homeAssistant.states['input_number.presence_packet_id_memo'].state") || 0);
let presence_ok = Number(global.get("homeassistant.homeAssistant.states['input_boolean.presence_temporaire_jm'].state") || 0);

compteur++;
let difference = Math.abs(packet_id - memo);

if (compteur >= 10) { //Compteur >= xx
if (difference >0) { //difference entre packed_id et Packed_id -x
presence_ok=1; //Mise à 1 Presence
memo = packet_id; //Memo packet_id
compteur=0; //RAZ compteur
}
}

if (compteur >= 20) { //Compteur >= xx
if (difference =0) { //difference entre packed_id et Packed_id -x
presence_ok=0; //Mise à 0 Presence
compteur=0; //RAZ compteur
}
}

if (compteur > 1000000) {
compteur = 0;
}

global.set("homeassistant.homeAssistant.states['input_number.presence_compteur'].state", compteur);
global.set("homeassistant.homeAssistant.states['input_number.presence_packet_id_memo'].state", memo);
global.set("homeassistant.homeAssistant.states['input_boolean.presence_temporaire_jm'].state", presence_ok);

msg.payload = presence_ok;
msg.compteur= compteur
return msg;

le global.set est utilisé pour écrire la valeur calculé dans le noeud function

Merci par avance pour votre retour

Welcome to the forums @jmauv1

To help you along.

  • I have moved this to the General category - you're not developing a Node for Node RED, so should not belong to the Developing Nodes category.

  • Whilst not a requirement by any means, but around 95% of this forum is English, so if you are able to translate - it will aid in quicker response - I have done that for you (Below)

  • Please use the code formatting options that are here - your code is not easily read without it
    I have also done that for you


Translation (and Code)

Hello everyone I am looking to detect a change in the status of a shelly button to detect that I am absent,

I decided to do this feature with the function node.

Previously I tried with the node filter on change but the result is not satisfactory

In the node function the global.get and global.set and especially the global.set does not work

Here is my code certainly perfectible

let compteur = Number(global.get("homeassistant.homeAssistant.states['input_number.Presence_compteur'].state") || 0);
let packet_id = Number(global.get("homeassistant.homeAssistant.states['sensor.bthome_sensor_c1d6_packet_id'].state") || 0);
let memo = Number(global.get("homeassistant.homeAssistant.states['input_number.presence_packet_id_memo'].state") || 0);
let presence_ok = Number(global.get("homeassistant.homeAssistant.states['input_boolean.presence_temporaire_jm'].state") || 0);

compteur++;
let difference = Math.abs(packet_id - memo);

if (compteur >= 10) { //Compteur >= xx
    if (difference > 0) { //difference entre packed_id et Packed_id -x
        presence_ok = 1; //Mise à 1 Presence
        memo = packet_id; //Memo packet_id
        compteur = 0; //RAZ compteur
    }
}

if (compteur >= 20) { //Compteur >= xx
    if (difference = 0) { //difference entre packed_id et Packed_id -x
        presence_ok = 0; //Mise à 0 Presence
        compteur = 0; //RAZ compteur
    }
}

if (compteur > 1000000) {
    compteur = 0;
}

global.set("homeassistant.homeAssistant.states['input_number.presence_compteur'].state", compteur);
global.set("homeassistant.homeAssistant.states['input_number.presence_packet_id_memo'].state", memo);
global.set("homeassistant.homeAssistant.states['input_boolean.presence_temporaire_jm'].state", presence_ok);

msg.payload = presence_ok;
msg.compteur = compteur
return msg;

The global.set is used to write the calculated value in the function node

Thank you in advance for your feedback

1 Like

You can not change the state of an entity inside home assistant within a function node. You need to use an action node to change the state of an entity.

homeAssistant globals are there for retrieving values only.

So you are in the Node Red module for haos. Right?
Global variables are global for Node Red. There is no access to those in HAOS. You therefore need to install Node Red companion on the HA. Is that done? Next, use the palette dedicated to the interface between the two platforms.
From there, all entities are accessible and can be manipulated.
From your description, I can't tell if that's the case.
What do your global variables look like when viewed from the variable display menu?