Help with understanding payload

#1

Hi,

I’m really struggling with understanding the 'msg.payload" with objects.
I’m really looking for this since a while, but I really miss a basic link.

I’m just going to tell what I trie to do:

I’m playing with the homkit node to ‘simulate’ a locking mechanism.

The input of this node to open the lock needs this:

[{"id":"7ef6ccf3.9e93d4","type":"inject","z":"8a160ea4.9b8d5","name":"Op Slot","topic":"","payload":"{\"LockTargetState\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":253.16671752929688,"y":407.3333854675293,"wires":[["43573fc6.8f4c7"]]}]

The homekit nodes just passes this message.
What I now need to do is to confirm that the lock is actually open.

Therefore I now need this message at the input of the node:

[{"id":"1607c7b0.d4dfb8","type":"inject","z":"8a160ea4.9b8d5","name":"Is Gesloten","topic":"","payload":"{\"LockCurrentState\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":159.0000228881836,"y":716.0000171661377,"wires":[[]]}]

As you can see, I only need to change the text ‘LockTargetState’ to ‘LockCurrentState’.

I wanted to do this ‘automatically’ with the change function, but thats only for changing the value.
I’ve tried with a Function. I can analyse the ‘input’ but I have NO idea how to get a LockCurrentState object at the output.

It’s quite simpele but i really don’t see it.

Thanks for helping

[Edited by @knolleary to properly format the flows]

0 Likes

#3

You just need a change node using the ‘move’ option. Move msg.payload.LockTargetState to msg.payload.LockCurrentState

1 Like

#4

If you are getting confused by the bits of messages this page is also useful
https://nodered.org/docs/user-guide/messages

1 Like

#5

Hi,

thanks, the move works fine. I've tried with 'change' earlier, but that didn't had succes.

I've read the info on that page several times, but like I said, I'm missing something. Im used to program a plc with all kinds of variables. But there we don't talk about a payload. You have a variable type and it's value. Now you have the payload, that has his own variables and then the values.? I don't always understand how to 'talk' to it. Would it be possible to do the same with a change? Just look in the 'whole' payload and then change the texts?

So, 50% of the jobs is done.

I send to the homekit node (to open the door) LockTargetState : 1 and with the move and a delay I confirm by sending LockCurrentState : 1

But now it should also inmedialtely close the lock again, and confirm this. So now I need to send a LockTargetState : 0 if a LockCurrentState : 1 is passed.

I have written a funcition for that, but it is not doing what I want. It's doing notting actually.

So summary:

I send LockTargetState : 1 and the next steps should happen automatically:
==> 500ms LockCurrentState : 1 ==> 500ms ==> LockTargetState : 0 ==> 500ms ==> LockCurrentState : 0

The move is the part under the homekit function, the 'function' the part above the homekit function.

My code:

[{"id":"a8e59e67.fe4f3","type":"inject","z":"8a160ea4.9b8d5","name":"Openen","topic":"","payload":"{\"LockTargetState\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":179.00017547607422,"y":207.0002088546753,"wires":[["9f7f567d.122ea8"]]},{"id":"9f7f567d.122ea8","type":"homekit-service","z":"8a160ea4.9b8d5","accessory":"385e4065.8d274","name":"","serviceName":"LockMechanism","x":668.1666793823242,"y":209.3333396911621,"wires":[["5f161fa4.2bf1f","e172923c.c2f4","81321b85.40edc8"]]},{"id":"5f161fa4.2bf1f","type":"change","z":"8a160ea4.9b8d5","name":"Bevestiging Actie","rules":[{"t":"move","p":"payload.LockTargetState","pt":"msg","to":"payload.LockCurrentState","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":593.1668548583984,"y":318.33346366882324,"wires":[["357c3a4f.c43626"]]},{"id":"357c3a4f.c43626","type":"delay","z":"8a160ea4.9b8d5","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":787.0000305175781,"y":315.00000762939453,"wires":[["9f7f567d.122ea8"]]},{"id":"e172923c.c2f4","type":"function","z":"8a160ea4.9b8d5","name":"V","func":"var x = msg.payload.CurrentState;\nvar y = {\"LockTargetState\" :1};\n\nif (x== \"1\") {\n     msg = y;\n}\nreturn msg\n  \n","outputs":1,"noerr":0,"x":608,"y":114,"wires":[["a22b4603.d60548"]]},{"id":"a22b4603.d60548","type":"delay","z":"8a160ea4.9b8d5","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":812,"y":113,"wires":[["9f7f567d.122ea8"]]},{"id":"81321b85.40edc8","type":"debug","z":"8a160ea4.9b8d5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1033.0000305175781,"y":209.0000057220459,"wires":[]},{"id":"385e4065.8d274","type":"homekit-accessory","z":"","accessoryName":"Zijdeur","pinCode":"111-11-111","port":"","manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","accessoryType":"12"}]
0 Likes

#6

So the whole msg is a javascript object, that page shows you how you can get the right syntax to any item within it (including within msg.payload) by using the debug tool and the tooltips.

In order to be able to import your flow, you need to post it as code ( add 3 backticks to the start)

But personally I’d add a larger delay for the time being and then add a couple of debug nodes after your change and function.

0 Likes