Conditionals and the correct format for JSON String


Node Red newbie here, just installed and running under Home Assistant currently.

I have an input which gives me a value of 0-100. I want to send this to a Tuya lamp as the brightness.
If the output is '0', I need to send an 'OFF' command, otherwise I need to send an 'ON' command and a brightness value.
First up, how do I create this conditional and pass the brightness value across?

Secondly, I know that

msg.payload = {
    dps: 20,
    set: false

return msg;

will turn the lamp OFF,

msg.payload = {
    dps: 20,
    set: true

return msg;

will turn the lamp ON and,

msg.payload = {
    dps: 22,
    set: msg.payload

return msg;

will set the output to the required Lamp Brightness, but I can't find a valid way of combining the 'Lamp ON' and 'Lamp Brightness' messages together.

Any help appreciated, thanks

Some questions.

If the lamp is off and you send brightness command does it turn on also?

Does it work if you send on command followed by brightness command?

What node is this and is there a read me?

Thanks for your interest.

If it's OFF, it will ignore any brightness values until it's turned ON.

If it's ON it will ignore a '0' brightness command.

Sending the ON command followed by the brightness level is the correct way to set it

It's the Tuya node
The Readme is good but doesn't address this issue.


Tuya smart device allows you to send multiples according to read me.

[{"id":"c2907bf7dce13692","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":3340,"wires":[["11dc7557b30c4aeb"]]},{"id":"11dc7557b30c4aeb","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":3360,"wires":[["a85cd9d1c803e9fc"],["f5380c309d7677bc"]]},{"id":"0b9f3fcfd3515f88","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":130,"y":3380,"wires":[["11dc7557b30c4aeb"]]},{"id":"a85cd9d1c803e9fc","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"dps\":20,\"set\":false}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":3340,"wires":[["b169675c9f0deb73"]]},{"id":"f5380c309d7677bc","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"hold","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{\"multiple\":true, \"data\":{\"20\":true}}","tot":"json"},{"t":"move","p":"hold","pt":"msg","to":"[\"22\"]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":3380,"wires":[["b169675c9f0deb73"]]},{"id":"b169675c9f0deb73","type":"debug","z":"b9860b4b9de8c8da","name":"debug 298","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":3300,"wires":[]}]

I tried exactly the format you suggested, that is

msg.payload = { "multiple": true, "data": { "20": true, "22": msg.payload } }

return msg;

but Node Red through its toys out the pram, saying, "The workspace contains some nodes that are not properly configured:"
I also tried every variation I could think of, but with no luck

My apologies - I had an extra '}' in the script. When I remove it, the script works - thank you!

Ok, so how to I do a conditional to test if I'm turning the lamp ON or OFF?

Did you bother to look at my example flow?

Told you I was a newbie!

In my excitement that the first bit worked, I didn't recognise the second part of your answer and completely ignored it! Apologies once again.

I don't understand how to use your example flow - could you please explain what I should do with it?

Add the input node (0-100) where inject nodes are, add the Tuya node where the debug is.
How to import a flow
It may pay to read the docs and watch the essential videos.

I learn best when I've got something working to build on and experiment with.

What you've given me is brilliant. I've imported it as a flow, added the Loxone 'Input' and the Tuya 'output' and it works great, with 1 minor tweak still needed. The input is 0-100, the output is 0-1000, so I need to multiply the output by 10. I've tried modifying/adding a rule in the "change 3 rules" block, but can't get it to work.

If you were able to point me at a solution for this, I'll be a very happy newbie and I'll go watch some more essential videos.

When you learnt to read and write did you start with ABC's or did you go straight to experimenting with paragraphs?
[edit] p.s. I forgot to mention use a range node. Also If you read the manual you may learn where you can find example flows for some of the nodes you install