17DEC2020 Updated and improved flow credit to @dinomite and @lars_hassio
I’ve been using node red for about nine months now. I’ve taught myself a lot by reading the examples in the community. Now I am giving back.
I had been writing a ton of function nodes with different standards to set flow attributes and then create the payload to send to the notify service. I needed a standard that was easier to trouble shoot and create new notifications.
I created a template for creating IOS notifications for various state changes of my devices (EG. Garage Door, Person/Device Tracker, Low Temp, High Humidity etc.) using the change node to set attributes in the flow and then a function node to create the payload (see image:)
The change node is way easier to prevent errors often found when creating a function… almost like a config screen rather than script. (see image:) (note: use debug to find attributes)
A user can just change these to achieve the full alert. Much faster than in a function.
The function node create a full output including the target ios device captured in the change node.
[{"id":"b0037499.3da95","type":"tab","label":"Send IOS notification","disabled":false,"info":""},{"id":"25f6a3c4.a9529c","type":"api-call-service","z":"b0037499.3da95","name":"Notify IOS with Message","server":"67813a04.5b8c44","version":1,"debugenabled":false,"service_domain":"notify","service":"{{topic}}","entityId":"","data":"{}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":570,"y":80,"wires":[[]]},{"id":"bc58bd96.f3c118","type":"function","z":"b0037499.3da95","name":"Create IOS Alert","func":"var title = msg.title\nvar message1 = msg.message1\nvar message2 = msg.message2\nvar message3 = msg.message3\nvar sound = msg.sound\nvar critical = msg.critical\nvar volume = msg.volume\nvar target = msg.target\nvar final_msg = `${message1}` + `${message2}` + `${message3}`\nvar payload = {\"data\":\n{\n \"message\": `${final_msg}`,\n \"title\": `${title}`,\n \"data\": {\n \"push\": {\n \"sound\": {\n \"name\": `${sound}`,\n \"critical\": `${critical}`,\n \"volume\": `${volume}`,\n }\n }\n }\n}\n}\nmsg.payload = payload\nmsg.topic = target\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":80,"wires":[["25f6a3c4.a9529c","c0fca00f.9cc418"]]},{"id":"b6171fec.b478f","type":"link in","z":"b0037499.3da95","name":"Notify IOS device","links":["d7251e74.62a8","4228d7d2.9665a","5e35c380.002684","ec0ac1b8.f6e2c","122218d1.a45127","6e18dd1c.d1749c","ab90d39c.05bdb","e1b05caa.b9c398"],"x":205,"y":80,"wires":[["bc58bd96.f3c118"]]},{"id":"c0fca00f.9cc418","type":"debug","z":"b0037499.3da95","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":140,"wires":[]},{"id":"1698a445.48925c","type":"change","z":"b0037499.3da95","name":"Set notification attributes","rules":[{"t":"set","p":"title","pt":"msg","to":"string, payload or whatever input","tot":"str"},{"t":"set","p":"message1","pt":"msg","to":"string, payload or whatever input","tot":"str"},{"t":"set","p":"message2","pt":"msg","to":"string, payload or whatever input","tot":"msg"},{"t":"set","p":"message3","pt":"msg","to":"string, payload or whatever input","tot":"str"},{"t":"set","p":"critical","pt":"msg","to":"0 or 1","tot":"str"},{"t":"set","p":"sound","pt":"msg","to":"default (critical notifications has different default sound than non critical) or wav: https://companion.home-assistant.io/docs/notifications/notification-sounds/#pre-installed-notification-sounds","tot":"str"},{"t":"set","p":"volume","pt":"msg","to":"1.0 (100%) or 0.n (n %)","tot":"str"},{"t":"set","p":"target","pt":"msg","to":"mobile_app_<your_device_id_here>","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":280,"wires":[["5e35c380.002684"]]},{"id":"5e35c380.002684","type":"link out","z":"b0037499.3da95","name":"","links":["b6171fec.b478f"],"x":475,"y":280,"wires":[]},{"id":"e261ebe9.72552","type":"debug","z":"b0037499.3da95","name":"Full debug to find attributes","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":340,"y":320,"wires":[]},{"id":"c4ed5703.3e243","type":"comment","z":"b0037499.3da95","name":"Template. Copy to flow to trigger notification","info":"","x":390,"y":220,"wires":[]},{"id":"774817b3.4b9e38","type":"inject","z":"b0037499.3da95","name":"Timestamp","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":480,"wires":[["7703e780.30409"]]},{"id":"307cb987.3a7da6","type":"change","z":"b0037499.3da95","name":"Set notification attributes","rules":[{"t":"set","p":"title","pt":"msg","to":"Time notification","tot":"str"},{"t":"set","p":"message1","pt":"msg","to":"The time is ","tot":"str"},{"t":"set","p":"message2","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"message3","pt":"msg","to":". I think you need more coffee!","tot":"str"},{"t":"set","p":"sound","pt":"msg","to":"default","tot":"str"},{"t":"set","p":"critical","pt":"msg","to":"0","tot":"str"},{"t":"set","p":"volume","pt":"msg","to":"0.5","tot":"str"},{"t":"set","p":"target","pt":"msg","to":"mobile_app_<your_device_id_here>","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":480,"wires":[["e1b05caa.b9c398"]]},{"id":"e1b05caa.b9c398","type":"link out","z":"b0037499.3da95","name":"","links":["b6171fec.b478f"],"x":695,"y":480,"wires":[]},{"id":"7703e780.30409","type":"moment","z":"b0037499.3da95","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Oslo","adjAmount":"1","adjType":"hours","adjDir":"add","format":"LLL","locale":"","output":"payload","outputType":"msg","outTz":"Europe/Oslo","x":320,"y":480,"wires":[["307cb987.3a7da6"]]},{"id":"e1226738.36c868","type":"comment","z":"b0037499.3da95","name":"Example. Insert you app id into \"set notification attributes\" node and trigger timestamp to send notification","info":"","x":420,"y":420,"wires":[]},{"id":"291fa529.9849ca","type":"comment","z":"b0037499.3da95","name":"Nodes to create messages and send notifications triggered from other flows","info":"","x":450,"y":20,"wires":[]},{"id":"6431169a.33c2d8","type":"comment","z":"b0037499.3da95","name":"Select correct HA server before using","info":"","x":690,"y":100,"wires":[]},{"id":"67813a04.5b8c44","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
Hope this helps; also open to constructive feedback!.
Next onto same for Alexa notify!
PaulText