How can i change a variable with a payload


I like to set a variable true or false if the payload from a device is true or false.

In the moment I make the distinction in the python script. But I like to depend the message i send to the return message from the device.
(If I use the hardware switch or the software switch the node red should know if the light should go on or off.)
How can I do it ?

Thx in advance

You haven't been clear enough on what you want to do I like to set a variable true or false well where is this variable? is it a msg property? Do you plan to have a function node and define a variable in it?

Since msg.payload has the value 'true' or 'false' just move it to the variable you need.

You mention a hardware switch - what node is that being monitored by? Have you added a debug node to it to see what it is returning?

If this doesn't help then could you please explain yourself a little more.


I have a hardware light switch.
I can controle it with node red or normal (pushing the switch).
I controle the node red with a python script. The script listens to a RF switch. First press the light goes on and the variable sets 1. If the variable is one and the button get bressed again the light goes off (and the variable is 0 again). So I can controle the light (on and off function) with a single button.
But if the light get turned on or off with the hardware switch, the script does'nt get the information.
So I thought i can make the variable count in node red depending on the feedback of the switch.

If you turn the light on or off with the hardware switch, does it send a signal somewhere?
Can you request the switch status in any way?
Please give us the details of the signals. A URL where the hardware is described would be useful too.




I get this back

Please give more information of your setup.

  1. What exactly is this hardware switch which sends messages?

  2. How are these messages delivered?

  3. Where are these messages delivered to?

  4. What does "I control the Node-red with a python script" mean?

  5. A screen capture of your Node-red flow might help me understand.

  6. Your python source code might help me understand (remove any secret passwords etc before posting).

This is my flow.
The timestamp starts the python script. The scripts lisents to the RF button.
If the Button get pressed, the script gives stubeOn or stubeOff back (depending on the variable). Part of my python script:

while True:
# Schalter 1 Stube
if rfdevice.rx_code == 10830577 and stube == 0:
stube += 1
elif rfdevice.rx_code == 10830577 and stube == 1:
stube = 0

This is working fine!
But the light switch (Tuya DIY Switch) can be controled be it self.
And then it can happen, the light is on but the variable in the script is still on 0.
So the i send the turning on command again. I have to press the button again and the light turns off.

What i would like is;
Make a variable (same like "stube") but in node red depending on the feedback from the light switch.
And the python script posts only "stube" and depending on the new node red variable the command on or off will be posted.

You seem to have a Node-red dashboard switch, an "RF button" and a "Tuya DIY Switch".

If I understand correctly, the dashboard switch and RF button are successfully synchronised.

Operating the Tuya switch does not update the dashboard switch.

Unfortunately I have no experience interacting with Tuya devices. All my smart switches use Tasmota.
Maybe a Tuya user is reading and can help.

Thy for the effort!

Does the switch have the ability to send data via something like MQTT? Not knowing what the actual device is, it is impossible to know what it is capible of.

I could make it.
I made a funktion with:

if ( && &&["1"] === true) {"var1", 1);
} else if ( && &&["1"] === false) {"var1", 0);
return msg;
and the input funktion is;
var var1 ="var1");
if (msg.payload === "stube" && var1 === 1) {
msg.payload = { "dps": "1", "set": false };
return msg;
} else if (msg.payload === "stube" && var1 === 0) {
msg.payload = { "dps": "1", "set": true };
return msg;
} else {
return null;

You do not need context to do this. If you set the switches to react on input, then you can feed the output of python and tuya devices in to the dashboard switch, so it will stay in sequence.
I do not have your flow or all your inputs and output values so my example may need adjusting to work with your tuya devices. I have also simulated the tuya devices in the example, but these may not be correct either, as there is lots of missing info in your posts.

Any way hope it helps work out the wiring and logic

[{"id":"24a6de4d3ea58f69","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":1160,"wires":[["b14b663935c2c1aa","29b83b431941b459"]]},{"id":"b14b663935c2c1aa","type":"function","z":"65617ffeb779f51c","name":"simulate tuya device","func":"msg.payload = {\"data\":{\"devId\":\"51306830483fda2231dc\",\"dps\":{\"1\":msg.payload},\"t\":1680535178},\"deviceId\":\"51306830483fda2231dc\",\"deviceName\":\"Stube\"};\nlet output = [msg,msg]\noutput[([\"1\"] === true ? 1 : 0)] = null\nreturn output;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":1200,"wires":[["6122c4f4fef5f2cb","8f4e8413d895efbe"],["493ebca7b98b8932","8f4e8413d895efbe"]]},{"id":"29b83b431941b459","type":"ui_switch","z":"65617ffeb779f51c","name":"","label":"stube","tooltip":"","group":"2d4fe667.28f8ba","order":19,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":370,"y":1120,"wires":[["b14b663935c2c1aa"]]},{"id":"7b199a77eca0a7dc","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":1200,"wires":[["b14b663935c2c1aa","29b83b431941b459"]]},{"id":"6122c4f4fef5f2cb","type":"debug","z":"65617ffeb779f51c","name":"debug 254","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":1180,"wires":[]},{"id":"493ebca7b98b8932","type":"debug","z":"65617ffeb779f51c","name":"debug 255","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":1240,"wires":[]},{"id":"8f4e8413d895efbe","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\"1\"]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":1120,"wires":[["29b83b431941b459"]]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":2,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.