Node-Red and YAML Data Template?

Hi all,

Just getting into Node-Red this week. Trying to figure out how to pass the following into a call-service node. I have this in existing HA automations and it works well (randomly selects a color for RGB lights. Any help is appreciated!

  - data_template:
      hs_color:
      - '{{ range(360)|random }}'
      - '{{ range(80,101)|random }}'

You can try this… I don’t have an RGB bulb to test it though.

[{"id":"63b32970.fec118","type":"inject","z":"25063fae.4f85f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1838.5717468261719,"y":2025.714111328125,"wires":[["446104b3.393c0c"]]},{"id":"446104b3.393c0c","type":"template","z":"25063fae.4f85f","name":"Data Template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{ \n\"data\": {\n    \"hs_color\": {\n    range(360)|random ,\n    range(80,101)|random\n                }\n        }\n}\n","output":"str","x":2048.571533203125,"y":2024.2857666015625,"wires":[["8cfb193f.fd7b48","7d8f2c1e.096b04"]]},{"id":"8cfb193f.fd7b48","type":"debug","z":"25063fae.4f85f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2262.8569984436035,"y":2107.1428575515747,"wires":[]},{"id":"7d8f2c1e.096b04","type":"api-call-service","z":"25063fae.4f85f","name":"Set light w/ Template","server":"8a3231e1.3f0e1","version":1,"debugenabled":false,"service_domain":"light","service":"toggle","entityId":"light.entrance_porch_light","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":true,"x":2304.2856369018555,"y":2024.285732269287,"wires":[["c9a2a6ad.e494d8"]]},{"id":"c9a2a6ad.e494d8","type":"debug","z":"25063fae.4f85f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2540,"y":2024.2857666015625,"wires":[]},{"id":"8a3231e1.3f0e1","type":"server","name":"Hassio","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

Thank you, @bastero. No luck yet, but I’ll keep tinkering with what you supplied.

[{"id":"3cfbed30.826ac2","type":"inject","z":"f61766ce.f85398","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":172,"y":3184,"wires":[["d49399ae.0af8f8"]]},{"id":"d49399ae.0af8f8","type":"api-render-template","z":"f61766ce.f85398","name":"","server":"","template":"{\"hsColor\": [{{range(360)|random}},{{range(80,101)|random}}]}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":518,"y":3184,"wires":[["915e1aef.0eae18"]]},{"id":"c8c74a45.dd6308","type":"inject","z":"f61766ce.f85398","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":172,"y":3232,"wires":[["a790b534.03e6b8"]]},{"id":"a790b534.03e6b8","type":"function","z":"f61766ce.f85398","name":"","func":"msg.payload = {\n    data: {\n        \"hs_color\": [\n            randomNumber(0,360),\n            randomNumber(80,101)\n            ]\n    }\n}\nreturn msg;\n\nfunction randomNumber(min, max) {  \n    return Math.floor(Math.random() * (max - min) + min);\n}  ","outputs":1,"noerr":0,"initialize":"","finalize":"","x":316,"y":3232,"wires":[["b8c884d3.84ed58"]]},{"id":"915e1aef.0eae18","type":"api-call-service","z":"f61766ce.f85398","name":"","server":"","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.light1","data":"{\"hs_color\": payload.hsColor}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":890,"y":3184,"wires":[[]]},{"id":"b8c884d3.84ed58","type":"api-call-service","z":"f61766ce.f85398","name":"","server":"","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.light1","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":490,"y":3232,"wires":[[]]},{"id":"9b0e0940.2bd5a8","type":"inject","z":"f61766ce.f85398","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":172,"y":3280,"wires":[["7aa4a500.1258dc"]]},{"id":"7aa4a500.1258dc","type":"api-call-service","z":"f61766ce.f85398","name":"","server":"","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.light1","data":"(\t$randomNumber := function($low, $high) {( \t    $floor($random() * ($high - $low) + $low)\t)};\t\t{\"hs_color\": [$randomNumber(0,360), $randomNumber(3, 400)]}\t)","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":346,"y":3280,"wires":[[]]}]
3 Likes

image

[{"id":"482a89bb.2d8de8","type":"inject","z":"f61766ce.f85398","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":412,"y":1488,"wires":[["d20c03b4.b68f3"]]},{"id":"d20c03b4.b68f3","type":"api-call-service","z":"f61766ce.f85398","name":"","server":"","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.light1","data":"{\t\"hs_color\": [$shuffle([0..360])[0], $shuffle([80..101])[0]]}\t","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":586,"y":1488,"wires":[[]]}]
5 Likes

That worked! Thank you :grinning:

I have been looking for exactly the same thing (my search words didn’t pick this up), so thank you from me too!