Like the title says, I am trying to pre-format a notification using a function node in Node-RED, but formatting it based upon what variables I have declared.
For this situation, possible messages are:
- msg.title (will always be declared)
- msg.message (will always be declared)
- msg.image
- msg. actionable_data_title_1
- msg.actionable_data_title_2
This is the code I currently have, that tries to paste values in there, whether I declare them or not. It works, it just adds some whitespace in the notification if the other 3 msg.whatever are not declared:
{
"title": "{{title}}",
"message": "{{message}}",
"data": {
"image": "{{image}}",
"actions": [
{
"action": "{{actionable_data_action_1}}",
"title": "{{actionable_data_title_1}}"
},
{
"action": "{{actionable_data_action_2}}",
"title": "{{actionable_data_title_2}}"
}
]
}
}
So I thought to use a function node to look through all of the messages to see what has and hasnt been declared.
My first thought is to make an empty array, and then check to see what msg.whatever have been declared, and add them to the array, and then format the notification based on the items in the array. This works, however, I cannot figure out how to pass it onto the ‘call service’ node ‘data’ field.
elements = []
if (msg.image != null){
elements.push(msg.image)
}
else if (msg.actionable_data_title_1 != null){
elements.push(msg.actionable_data_title_1, msg.actionable_data_action_1)
}
else if (msg.actionable_data_title_2 != null){
elements.push(msg.actionable_data_title_2, msg.actionable_data_action_2);
}
//check to see if elements contains msg.image AND msg.actionable_data_title_1
if (elements.includes(msg.image) && elements.includes(msg.actionable_data_title_1)){
msg.notification = "{\"title\":\"{{title}}\",\"message\":\"{{message}}\",\"data\":{\"image\":\"{{image}}\",\"actions\":[{\"action\":\"{{actionable_data_action_1}}\",\"title\":\"{{actionable_data_title_1}}\"},{\"action\":\"{{actionable_data_action_2}}\",\"title\":\"{{actionable_data_title_2}}\"}]}}"
}
//check to see if elements contains msg.whatever but DOES NOT contain msg.image
else if (!elements.includes(msg.image) && elements.includes(msg.actionable_data_title_1)){
msg.notification = "{\"title\":\"{{title}}\",\"message\":\"{{message}}\",\"data\":{\"actions\":[{\"action\":\"{{actionable_data_action_1}}\",\"title\":\"{{actionable_data_title_1}}\"},{\"action\":\"{{actionable_data_action_2}}\",\"title\":\"{{actionable_data_title_2}}\"}]}}"
}
if (elements.includes(msg.image) && !elements.includes(msg.actionable_data_title_1)){
msg.notification = "{\"title\":\"{{title}}\",\"message\":\"{{message}}\",\"data\":{\"image\":\"{{image}}\"}}"
}
else {
msg.notification = "{\"title\": \"{{title}}\",\"message\": \"{{message}}\"}";
}
return msg;
If I enter {{notification}} into the data field, it gives me an API error. If I enter {}, I just get blank notifications. If I enter {“message”:"{{notification}}"}, I just get blank notifications.
Here is the flow
[{"id":"8158ab0b731cfa04","type":"inject","z":"9003fcc3.08f48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":955,"y":280,"wires":[["8a8b1a3d6d5ff2f5"]],"l":false},{"id":"4d64f59ce4eaf3d1","type":"inject","z":"9003fcc3.08f48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":955,"y":320,"wires":[["1f72036f5402d9f8"]],"l":false},{"id":"4d84c50e9eeb2cca","type":"inject","z":"9003fcc3.08f48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":955,"y":360,"wires":[["c6a6cfe3c25c81c8"]],"l":false},{"id":"c6a6cfe3c25c81c8","type":"change","z":"9003fcc3.08f48","name":"Set Notification msg","rules":[{"t":"set","p":"title","pt":"msg","to":"Actionable With Image","tot":"str"},{"t":"set","p":"message","pt":"msg","to":"This is a test","tot":"str"},{"t":"set","p":"actionable_data_title_1","pt":"msg","to":"Test 1!","tot":"str"},{"t":"set","p":"actionable_data_action_1","pt":"msg","to":"restart_coffee_timer","tot":"str"},{"t":"set","p":"actionable_data_title_2","pt":"msg","to":"Test 2!","tot":"str"},{"t":"set","p":"actionable_data_action_2","pt":"msg","to":"dont_restart_coffee_timer","tot":"str"},{"t":"set","p":"image","pt":"msg","to":"/media/local/abstract.jpg","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1015,"y":360,"wires":[["3d677e1e9a53d4cf"]],"l":false,"info":"Changes the person entity to a simple name\n\nexample: person.joe -> Joe"},{"id":"1f72036f5402d9f8","type":"change","z":"9003fcc3.08f48","name":"Set Notification msg","rules":[{"t":"set","p":"title","pt":"msg","to":"Just Image","tot":"str"},{"t":"set","p":"message","pt":"msg","to":"This is a test","tot":"str"},{"t":"set","p":"image","pt":"msg","to":"/media/local/abstract.jpg","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1015,"y":320,"wires":[["3d677e1e9a53d4cf"]],"l":false,"info":"Changes the person entity to a simple name\n\nexample: person.joe -> Joe"},{"id":"8a8b1a3d6d5ff2f5","type":"change","z":"9003fcc3.08f48","name":"Set Notification msg","rules":[{"t":"set","p":"title","pt":"msg","to":"Actionable No Image","tot":"str"},{"t":"set","p":"message","pt":"msg","to":"This is a test","tot":"str"},{"t":"set","p":"actionable_data_title_1","pt":"msg","to":"Test 1!","tot":"str"},{"t":"set","p":"actionable_data_action_1","pt":"msg","to":"restart_coffee_timer","tot":"str"},{"t":"set","p":"actionable_data_title_2","pt":"msg","to":"Test 2!","tot":"str"},{"t":"set","p":"actionable_data_action_2","pt":"msg","to":"dont_restart_coffee_timer","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1015,"y":280,"wires":[["3d677e1e9a53d4cf"]],"l":false,"info":"Changes the person entity to a simple name\n\nexample: person.joe -> Joe"},{"id":"3d677e1e9a53d4cf","type":"function","z":"9003fcc3.08f48","name":"Check Speaker Selection","func":"elements = []\n\nif (msg.image != null){\n elements.push(msg.image)\n}\nelse if (msg.actionable_data_title_1 != null){\n elements.push(msg.actionable_data_title_1, msg.actionable_data_action_1)\n}\nelse if (msg.actionable_data_title_2 != null){\n elements.push(msg.actionable_data_title_2, msg.actionable_data_action_2);\n}\n//check to see if elements contains msg.image AND msg.actionable_data_title_1\nif (elements.includes(msg.image) && elements.includes(msg.actionable_data_title_1)){\n msg.notification = \"{\\\"title\\\":\\\"{{title}}\\\",\\\"message\\\":\\\"{{message}}\\\",\\\"data\\\":{\\\"image\\\":\\\"{{image}}\\\",\\\"actions\\\":[{\\\"action\\\":\\\"{{actionable_data_action_1}}\\\",\\\"title\\\":\\\"{{actionable_data_title_1}}\\\"},{\\\"action\\\":\\\"{{actionable_data_action_2}}\\\",\\\"title\\\":\\\"{{actionable_data_title_2}}\\\"}]}}\"\n}\n//check to see if elements contains msg.whatever but DOES NOT contain msg.image\nelse if (!elements.includes(msg.image) && elements.includes(msg.actionable_data_title_1)){\n msg.notification = \"{\\\"title\\\":\\\"{{title}}\\\",\\\"message\\\":\\\"{{message}}\\\",\\\"data\\\":{\\\"actions\\\":[{\\\"action\\\":\\\"{{actionable_data_action_1}}\\\",\\\"title\\\":\\\"{{actionable_data_title_1}}\\\"},{\\\"action\\\":\\\"{{actionable_data_action_2}}\\\",\\\"title\\\":\\\"{{actionable_data_title_2}}\\\"}]}}\"\n}\nif (elements.includes(msg.image) && !elements.includes(msg.actionable_data_title_1)){\n msg.notification = \"{\\\"title\\\":\\\"{{title}}\\\",\\\"message\\\":\\\"{{message}}\\\",\\\"data\\\":{\\\"image\\\":\\\"{{image}}\\\"}}\"\n}\nelse {\n msg.notification = \"{\\\"title\\\": \\\"{{title}}\\\",\\\"message\\\": \\\"{{message}}\\\"}\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1095,"y":320,"wires":[["b9ef32fc182a4942"]],"l":false},{"id":"b9ef32fc182a4942","type":"api-call-service","z":"9003fcc3.08f48","name":"Send Notification","server":"","version":3,"debugenabled":true,"service_domain":"notify","service":"","entityId":"","data":"{\"message\":\"{{notification}}\"}","dataType":"jsonata","mergecontext":"","mustacheAltTags":true,"outputProperties":[],"queue":"none","x":1230,"y":320,"wires":[[]]}]