Trying to set msg.payload.target_temperature_c

#1

This is clearly a problem with my understanding, but I'm stumped!

I'm basically following the example in this node red documentation page, bottom of the section "Changing message properties", except I'm injecting a numerical value.

I am using a change node to 'copy' a value (i.e. msg.payload, received from an inject node) to msg.payload.target_temperature_c. However when I attach a debug node to output, it just shows the value I injected in msg.payload of the output, not in msg.payload.target_temperature_c.

End result: I want to be able to update my Nest thermostat with a value from a slider on dashboard... but this is the bit that's failing.

Here's my node. When I hit the inject button, I am hoping to see msg.payload.target_temperature_c as the value 19. (But I'm not)

[{"id":"d4e6120f.dc4ca","type":"debug","z":"a09201df.15c79","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1230,"y":780,"wires":[]},{"id":"fe5f46b2.f24778","type":"inject","z":"a09201df.15c79","name":"","topic":"","payload":"19","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":730,"y":780,"wires":[["35b88424.9a60dc"]]},{"id":"35b88424.9a60dc","type":"change","z":"a09201df.15c79","name":"","rules":[{"t":"set","p":"payload.target_temperature_c","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":780,"wires":[["d4e6120f.dc4ca"]]}]

0 Likes

#2

I'm no expert here.
But the following observations might help.

After the inject node, msg.payload is a string.

In the change node, you act if msg.payload is an object. This seems to fail.

In my local test installation, I expanded your change node the following way:

  1. set msg.tmp to msg.payload
  2. delete msg.payload
  3. set msg.payload.target_whatever to msg.tmp

This did the trick. But it might not be the most efficient solution. Let's see what the more knowledgeable forum members have to say.

Kind regards,

Urs.

2 Likes

#3

Modify the Change node so that it Moves msg.payload To msg.payload.target_temperatue_c and it will work.

[{"id":"2a30b5d9.53e5ea","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"19","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":445,"wires":[["f866e9.acc5c118"]]},{"id":"45f2ffe4.1fbcf8","type":"debug","z":"6dc690a3.1abc88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":508,"y":444,"wires":[]},{"id":"f866e9.acc5c118","type":"change","z":"6dc690a3.1abc88","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.target_temperature_c","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":302,"y":445,"wires":[["45f2ffe4.1fbcf8"]]}]
1 Like

#4

That is correct - strings cannot have properties. JavaScript will let you set them but it will fail silently.

Rather than copy, you can configure the Change node to move msg.payload to msg.payload.target_temperature_c and it will work.

1 Like

#5

Thanks all, that helped my understanding.

Aside: how can I post my node code here on this forum to display on a single line, as was done by @Colin?

0 Likes

#6

you need to
start with three backtic's,
then on the next line put your flow,
then on the next line three more backtic's

1 Like

#7

@zenofmud Many thanks!

0 Likes

#8

In addition, if you want it to just take one line then when you export choose Compact down at the bottom right of the export window.

0 Likes