Need help to insert msg.payload into json data

Hi everyone,
I have a simple problem but unfortunately, I am even more so. I have a flow that I want to use to retrieve the set temperature of my climate system, and then set the new temperature of the furnace to 2 degrees less. I have already learned how to retrieve the ‘set temperature’ and placed it into the flow as “msg.payload”.

var setTemp = msg.data.attributes.temperature-2;
msg.payload=setTemp;
return msg;

As you can see above, I have retrieved the set temperature of the climate control and I have subtracted 2 degrees C from this value and placed it into the msg.payload. This part works fine

Then, I know how to use a ‘Call Service’ node to set the temperature with data like {“temperature”:22};
However, I do not know how to insert the msg.payload in place of the number (in this case 22)
Here is the complete flow. Just the last node ‘Set Temp’ is the problem.

[{"id":"94492e5d.e42928","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"e56ba6e.21bfad8","type":"debug","z":"94492e5d.e42928","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":100,"wires":[]},{"id":"a79705ba.3185d8","type":"inject","z":"94492e5d.e42928","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":160,"wires":[["865e6bcc.52643"]]},{"id":"d257d30b.6fb71","type":"function","z":"94492e5d.e42928","name":"","func":"var setTemp = msg.data.attributes.temperature-2;\nmsg.payload=setTemp;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":160,"wires":[["74bc2b60.be6bbc"]]},{"id":"865e6bcc.52643","type":"api-current-state","z":"94492e5d.e42928","name":"test","server":"1d707c49.07a204","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"climate.thermostat","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":110,"y":100,"wires":[["20092d4f.3f4682"]]},{"id":"20092d4f.3f4682","type":"switch","z":"94492e5d.e42928","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"heat","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":100,"wires":[["d257d30b.6fb71"],[]]},{"id":"74bc2b60.be6bbc","type":"api-call-service","z":"94492e5d.e42928","name":"Set Temp","server":"fee4c7b2.b5e188","version":1,"debugenabled":false,"service_domain":"climate","service":"set_temperature","entityId":"climate.thermostat","data":"{\"temperature\":20}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":480,"y":120,"wires":[["e56ba6e.21bfad8"]]},{"id":"1d707c49.07a204","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"fee4c7b2.b5e188","type":"server","name":"Home Assistant","addon":true}]

Thanks

Set to something like {“temperature”:22-{payload}}

Sorry thats 22 minus leave that out 22-

Thanks for trying, but it does not work. I get an error:

"Call-service API error.  Error Message: must contain at least one of temperature, target_temp_high, target_temp_low."

I did remove the 22- and I also tried msg.payload but same error message with both {payload} and {msg.payload}

Sam

Hey Sam,

would you mind trying this:

Set the data to {"temperature":payload}
Set the Drop-down left of the data field to J: expression

Set the data field in your service node to:

{"temperature":"{{payload}}"}

Just copy and paste as is. Don’t lose any of the quotes or curly brackets. Leave “Use alternative template tags for the Data field” unchecked as well.

4 Likes

Thank you so much! That did the trick.
Sam

Sorry, typed on mobile… missed 2 {} :frowning:

Hi!
Thanks for the answers. My scenario was similar, but in my case setting the payload as the proposed one between double quotation marks gave me an error saying brightness_pct is expecting a float, not a string.
I just wanted to set the PWM controlled fan speed depending on the CPU.temperature.
The solution for me was -given that the fan is controlled via light.turn_on service- to pass
data (J expression):
{ "brightness_pct": fanSpeedPerc }
being fanSpeedPerc a msg attribute (eg. 'fanSpeedPerc=30`) set in a previous node