Dynamic templates

Hey,

I’d like to create a flow where the template is defined by a node and later parsed by “get template”.

I’ve created a flow that doesn’t work correctly, the {{payload}} is not replaced as I’d like. Any ideas how can this be solved?

[{"id":"a015a60b.86345","type":"inject","z":"3b4ed2a4.b47fee","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello there","payloadType":"str","x":190,"y":680,"wires":[["5c0868f8.60f27"]]},{"id":"8d9e511c.a0bd5","type":"debug","z":"3b4ed2a4.b47fee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":680,"wires":[]},{"id":"5c0868f8.60f27","type":"change","z":"3b4ed2a4.b47fee","name":"","rules":[{"t":"set","p":"template","pt":"msg","to":"Kenobi: {{payload}}!","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":680,"wires":[["ea1a36db.e478a8"]]},{"id":"ea1a36db.e478a8","type":"api-render-template","z":"3b4ed2a4.b47fee","name":"","server":"2b96683c.936a98","template":"","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":470,"y":760,"wires":[["8d9e511c.a0bd5"]]},{"id":"2b96683c.936a98","type":"server","name":"Home Assistant","addon":true}]

Thank you!

what exactly are you trying to achieve?

Template node just runs the home assistant template thru HA and brings the response. something like this:

image

[{"id":"a015a60b.86345","type":"inject","z":"110b2c7b.9890f4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello there","payloadType":"str","x":160,"y":1560,"wires":[["5c0868f8.60f27"]]},{"id":"8d9e511c.a0bd5","type":"debug","z":"110b2c7b.9890f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":1560,"wires":[]},{"id":"5c0868f8.60f27","type":"change","z":"110b2c7b.9890f4","name":"","rules":[{"t":"set","p":"template","pt":"msg","to":"{{ states('input_boolean.alarm_clock')}}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":1560,"wires":[["ea1a36db.e478a8"]]},{"id":"ea1a36db.e478a8","type":"api-render-template","z":"110b2c7b.9890f4","name":"","server":"e447d17a.16a64","template":"","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":600,"y":1560,"wires":[["8d9e511c.a0bd5"]]},{"id":"e447d17a.16a64","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

if you want to use the payload of a message in something else, like a service call, then construct the service call node like this:

image

[{"id":"56ba63fd.ba46bc","type":"api-call-service","z":"110b2c7b.9890f4","name":"turn the light green","server":"e447d17a.16a64","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.living_room_middle","data":"{\"color_name\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":350,"y":1640,"wires":[[]]},{"id":"a492adfa.cb841","type":"inject","z":"110b2c7b.9890f4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"red","payloadType":"str","x":170,"y":1640,"wires":[["56ba63fd.ba46bc"]]},{"id":"e447d17a.16a64","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

I’ve created a subflow for Telegram notification. It has two inputs, which are passed to the “call service” node.

The input I’d like to pass is a template (it should inject some of the values from previous nodes).
Roughly, something like this:

[{"id":"6bf0d2a9.2bc004","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":50,"y":30,"wires":[{"id":"c4ea0265.c5a0b8"}]}],"out":[],"env":[{"name":"message","type":"str","value":""}],"color":"#DDAA99"},{"id":"c4ea0265.c5a0b8","type":"api-call-service","z":"6bf0d2a9.2bc004","name":"","server":"2b96683c.936a98","version":1,"debugenabled":false,"service_domain":"notify","service":"radmen_telegram","entityId":"","data":"{ \"message\": $env('message') }","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":340,"y":40,"wires":[[]]},{"id":"2b96683c.936a98","type":"server","name":"Home Assistant","addon":true},{"id":"52caca7e.83f144","type":"inject","z":"7dfefb8b.3673ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":300,"y":280,"wires":[["62c4e19d.5f92e"]]},{"id":"62c4e19d.5f92e","type":"subflow:6bf0d2a9.2bc004","z":"7dfefb8b.3673ec","name":"","env":[{"name":"message","value":"Hello there: {{payload}}","type":"str"}],"x":600,"y":260,"wires":[]}]

The string which is passed as input is treated literally as value, not template. I’d like to somehow parse it as a template and then pass to the “call service” node.

Does it make sense?

i am afraid not. maybe send the entire flow.
again, if you want to send part of the message to service call node, then just do it like I said. Templates are useful in HA automation but not required much in node red.

@serkank well, the flow I sent is basically what I want to have.

The inject node is used only to set a value in payload, then I want to trigger a notification using the template passed in the input.

I’d like the subflow to treat the input as a template and replace {{payload}} with the actual value taken from the message object.

I am not using telegram so I can’t create the flow but did you check my previous message? the logic will be the same. you want to send this message to telegram? “Kenobi: Hello There”

[{"id":"52caca7e.83f144","type":"inject","z":"110b2c7b.9890f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":170,"y":1940,"wires":[["8ead3c7b.40847"]]},{"id":"ac0dca62.d0a1e8","type":"api-call-service","z":"110b2c7b.9890f4","name":"","server":"e447d17a.16a64","version":1,"debugenabled":false,"service_domain":"notify","service":"persistent_notification","entityId":"","data":"msg.data","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":true,"x":620,"y":1940,"wires":[[]]},{"id":"8ead3c7b.40847","type":"change","z":"110b2c7b.9890f4","name":"","rules":[{"t":"set","p":"data.title","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"data.message","pt":"msg","to":"msg.payload & \": Hello there!\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":1940,"wires":[["ac0dca62.d0a1e8"]]},{"id":"e447d17a.16a64","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

I did see your previous message and your latest response it gave me some hints how I can solve the issue. Thank you! :slight_smile: