I am working with some messages related to notifications. I currently have the following working just fine (with the exception of the Function nodes, which is what I need help with):
[{"id":"72f054d31130bd9a","type":"change","z":"647eafda.c7cfa","name":"Set Message","rules":[{"t":"set","p":"person","pt":"msg","to":"payload.data.event.old_state.attributes.friendly_name","tot":"msg"},{"t":"set","p":"zone","pt":"msg","to":"payload.data.event.old_state.state","tot":"msg"},{"t":"set","p":"direction","pt":"msg","to":"left","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1850,"y":700,"wires":[["8018062759696e7e","93adb26a8fbc6b9f"]]},{"id":"6ede69fea6cb4679","type":"trigger-state","z":"647eafda.c7cfa","name":"Person Left a Zone","server":"296c0678.b5f9ca","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":"^person\\..*$","entityIdType":"regex","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"not_home"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"not_home"}],"customOutputs":[],"outputInitially":true,"stateType":"str","enableInput":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":1430,"y":700,"wires":[["6e6e1be63b852e38"],[]]},{"id":"8c4e62426ed94bce","type":"trigger-state","z":"647eafda.c7cfa","name":"Person Arrived at a Zone","server":"296c0678.b5f9ca","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":"^person\\..*$","entityIdType":"regex","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"not_home"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"not_home"}],"customOutputs":[],"outputInitially":true,"stateType":"str","enableInput":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":1450,"y":820,"wires":[["ccfbbde4f1209703"],[]]},{"id":"99a2450d0a1fe749","type":"change","z":"647eafda.c7cfa","name":"Set Message","rules":[{"t":"set","p":"person","pt":"msg","to":"data.event.new_state.attributes.friendly_name","tot":"msg"},{"t":"set","p":"zone","pt":"msg","to":"data.event.new_state.state","tot":"msg"},{"t":"set","p":"direction","pt":"msg","to":"arrived at","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1850,"y":820,"wires":[["8018062759696e7e","65d8af957668f71b"]]},{"id":"6e6e1be63b852e38","type":"function","z":"647eafda.c7cfa","name":"Set Topic","func":"var oldstate = msg.payload.data.event.old_state.state;\nvar newstate = msg.payload.data.event.new_state.state;\nvar id = msg.data.event.new_state.attributes.id;\n\nif (oldstate == \"home\")\n msg.topic = 'input_datetime.time_left_home_${id}';\nif (newstate == \"home\")\n msg.topic = 'input_datetime.time_arrived_home${id}';\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1680,"y":700,"wires":[["72f054d31130bd9a"]]},{"id":"ccfbbde4f1209703","type":"function","z":"647eafda.c7cfa","name":"Set Topic","func":"var oldstate = msg.payload.data.event.old_state.state;\nvar newstate = msg.payload.data.event.new_state.state;\nvar id = msg.data.event.new_state.attributes.id;\n\nif (oldstate == \"home\")\n msg.topic = 'input_datetime.time_left_home_${id}';\nif (newstate == \"home\")\n msg.topic = 'input_datetime.time_arrived_home${id}';\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1680,"y":820,"wires":[["99a2450d0a1fe749"]]},{"id":"8018062759696e7e","type":"template","z":"647eafda.c7cfa","name":"Format Message","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{person}}} has just {{{direction}}} {{{zone}}}.","output":"str","x":2110,"y":760,"wires":[["d002dcb3716f8a90"]]},{"id":"296c0678.b5f9ca","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
I’m sure there’s a way I can do this with a single state trigger node, but it’s taken me a bit to get here…
The “Set Topic” function nodes are meant to conditionally add a msg.topic based on some criteria:
- if the value of
msg.payload.data.event.old_state.state
is home, then setmsg.topic
to input_datetime.time_left_home_${id}, where “id” ismsg.data.event.new_state.attributes.id
. - if the value of
msg.payload.data.event.new_state.state
is home, then setmsg.topic
to input_datetime.time_left_home_${id}, where “id” is alsomsg.data.event.new_state.attributes.id
.
So, adding this msg.topic to the payload is failing, and I’m sure it’s because my syntax is bad in “input_datetime.time_left_home_${id},” or maybe some assignment thereof. Can anyone provide me with a solution?
TIA!