# Conditionals and the correct format for JSON String

**URL:** https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589
**Category:** General
**Created:** [21 May 2023 17:48 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589 "2023-05-21T17:48:09Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)
#### Post date: [21 May 2023 17:48 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/1 "2023-05-21T17:48:09Z")

</div>

Hi.

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

```auto
msg.payload = {
    dps: 20,
    set: false
}

return msg;

```

will turn the lamp OFF,

```auto
msg.payload = {
    dps: 20,
    set: true
}

return msg;

```

will turn the lamp ON and,

```auto
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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [21 May 2023 18:01 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/2 "2023-05-21T18:01:55Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)
#### Post date: [21 May 2023 18:59 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/3 "2023-05-21T18:59:40Z")

</div>

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  
Node-red-contrib-tuya-smart-device  
The Readme is good but doesn't address this issue.

Thanks

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [21 May 2023 19:14 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/4 "2023-05-21T19:14:33Z")

</div>

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

```auto
{"multiple":true,"data":{"20":true,"22":1}}

```

```auto
[{"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":"payload.data[\"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":[]}]

```

---

<div class="post-metadata">

### Author: ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)
#### Post date: [21 May 2023 19:50 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/5 "2023-05-21T19:50:41Z")

</div>

I tried exactly the format you suggested, that is

```auto
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

---

<div class="post-metadata">

### Author: ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)
#### Post date: [21 May 2023 19:56 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/6 "2023-05-21T19:56:28Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [21 May 2023 20:00 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/7 "2023-05-21T20:00:20Z")

</div>

Did you bother to look at my example flow?

---

<div class="post-metadata">

### Author: ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)
#### Post date: [21 May 2023 20:30 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/8 "2023-05-21T20:30:25Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [21 May 2023 20:36 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/9 "2023-05-21T20:36:10Z")

</div>

Add the input node (0-100) where inject nodes are, add the Tuya node where the debug is.  
[How to import a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)  
It may pay to read the docs and watch the [essential videos](https://m.youtube.com/watch?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6&v=ksGeUD26Mw0&embeds_referring_euri=https%3A%2F%2Fnodered.org%2F&source_ve_path=OTY3MTQ&feature=emb_imp_woyt).

---

<div class="post-metadata">

### Author: ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)
#### Post date: [21 May 2023 21:26 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/10 "2023-05-21T21:26:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [21 May 2023 22:05 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/11 "2023-05-21T22:05:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [20 July 2023 22:05 UTC](https://discourse.nodered.org/t/conditionals-and-the-correct-format-for-json-string/78589/12 "2023-07-20T22:05:42Z")

</div>

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