# Changing of msg.payload

**URL:** https://discourse.nodered.org/t/changing-of-msg-payload/46568
**Category:** General
**Created:** [1 June 2021 16:50 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568 "2021-06-01T16:50:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![JdT](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@JdT](https://discourse.nodered.org/u/JdT)
#### Post date: [1 June 2021 16:50 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/1 "2021-06-01T16:50:51Z")

</div>

Hello,

I am bloody new to Node-RED and would ask you for your help to solve my "problem".

I have a homebridge with NR running and connected a Shelly motion sensor and a Shelly PM to then switch its output on motion.

The motion sensor is giving me a payload which the PM is not "understanding" so I think I have to convert it somehow. I tried and googled around but did not come to a solution, so maybe some Pro's can help me!

 ![shelly1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d93c1f031bb301d7ef169157ce0efd2a53c0c5d.png)

In the first line is the message from the motion sensor and in the second what i need to send to the shelly output

Thx a lot  
cheerio

---

<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: [1 June 2021 17:03 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/2 "2021-06-01T17:03:53Z")

</div>

This is a common need and can be achieved in a change node or a function node or multiple nodes switch and change node combination.  
here is a change node example using JSONata expression.  
And using a switch node.

```auto
[{"id":"12cf7965.5ab35f","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload.MotionDetected","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110.33332824707031,"y":4766.33349609375,"wires":[["6264d36.70275ac"]]},{"id":"6264d36.70275ac","type":"switch","z":"c74669a0.6a34f8","name":"","property":"payload.MotionDetected","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":260,"y":4740,"wires":[["70c210d.dba867"],["6294e50b.cd001c"]]},{"id":"70c210d.dba867","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.On","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":4720,"wires":[["df2cf978.75a28"]]},{"id":"6294e50b.cd001c","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.On","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":4760,"wires":[["df2cf978.75a28"]]},{"id":"df2cf978.75a28","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":4840,"wires":[]},{"id":"dc48ccf2.11107","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.On","pt":"msg","to":"payload.MotionDetected = 0 ? true : false","tot":"jsonata"},{"t":"delete","p":"payload.MotionDetected","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":4840,"wires":[["df2cf978.75a28"]]},{"id":"73b548ec.6ff45","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload.MotionDetected","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":4840,"wires":[["dc48ccf2.11107"]]}]

```

similar can be done in a function node

---

<div class="post-metadata">

### Author: ![JdT](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@JdT](https://discourse.nodered.org/u/JdT)
#### Post date: [2 June 2021 06:08 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/3 "2021-06-02T06:08:13Z")

</div>

Thx, this worked perfect!

Is there a classical AND in NR?

cheers

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [2 June 2021 06:28 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/4 "2021-06-02T06:28:54Z")

</div>

> [@JdT](#):
>
> Is there a classical AND in NR

2 switch nodes in series

Or

A function node `if(X && y)`

Or

A JSONata expression

---

<div class="post-metadata">

### Author: ![JdT](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@JdT](https://discourse.nodered.org/u/JdT)
#### Post date: [2 June 2021 07:08 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/5 "2021-06-02T07:08:02Z")

</div>

Hmmmm...if I want to combine two inputs then I have to use JSON?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [2 June 2021 07:17 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/6 "2021-06-02T07:17:05Z")

</div>

> [@JdT](#):
>
> if I want to combine two inputs then I have to use JSON

Hi, I slight mixup there (probably a typo) but for clarification...

- JSON is a syntax for storing and exchanging data
- JSONata is a query and transformation language

And no, you dont _ **have to use** _ JSONata (as i said before, 2 switch nodes in series or a function node are viable solutions) & there are add-in contrib nodes for Boolean logic (I dont use any personally)

---

<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: [2 June 2021 08:34 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/7 "2021-06-02T08:34:48Z")

</div>

In JSONata use and / or [Boolean Operators · JSONata](https://docs.jsonata.org/boolean-operators)  
in Javasript use && / || [JavaScript Comparison and Logical Operators](https://www.w3schools.com/js/js_comparisons.asp)

---

<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: [1 August 2021 08:35 UTC](https://discourse.nodered.org/t/changing-of-msg-payload/46568/8 "2021-08-01T08:35:27Z")

</div>

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