Help with Node Red

furpile

Expert Member
Joined
Jul 14, 2014
Messages
4,419
Reaction score
1,059
Location
Richards Bay
Hi, I hope this is in the correct section as I am using Node Red as part of my Home Assistant setup. I don't have much there yet, but I am trying to figure out how to use the Android app notifications for various things.

There are some properties that you need to set for the notification, to make it work better. First is a Group, so that all similar notifications are grouped so that it does not clutter up the notification screen. Second is a priority for the message so that it will sound even if the phone is on standby.

I am trying to start with an easy test flow. Using an inject node with a timestamp, I want to add a field to the msg.payload that will set the group to a specific value, and then pass on the updated msg to a debug node.

Has anybody done something like this that can help me understand what to do please?
 
In Node-Red, like most coding environments, there are always lots of ways of achieving the same goal. Here's a sample flow that hopefully demonstrates two alternative approaches to adding your group setting to an incoming message to match the structure at https://companion.home-assistant.io...tions-basic/#thread-id-grouping-notifications :

Code:
[{"id":"6649753b.17bcc4","type":"inject","z":"39804c96.ce1be4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"title\":\"Test Title\",\"message\":\"Test Message Content\"}}","payloadType":"json","x":930,"y":440,"wires":[["97b77793.666e08","f84af1ad.6004b8","d540e0d0.17bd3"]]},{"id":"5cf51439.d59ea4","type":"api-call-service","z":"39804c96.ce1be4","name":"Notify Me","server":"8bb7448c.dc3238","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_your_phone","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1320,"y":420,"wires":[[]]},{"id":"97b77793.666e08","type":"debug","z":"39804c96.ce1be4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1030,"y":360,"wires":[]},{"id":"f84af1ad.6004b8","type":"function","z":"39804c96.ce1be4","name":"add group","func":"msg.payload.data.data = {};\nmsg.payload.data.data.group = \"test-group\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1110,"y":420,"wires":[["d77d81a.75a9","5cf51439.d59ea4"]]},{"id":"d77d81a.75a9","type":"debug","z":"39804c96.ce1be4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":380,"wires":[]},{"id":"d540e0d0.17bd3","type":"change","z":"39804c96.ce1be4","name":"add group","rules":[{"t":"set","p":"payload.data.data.group","pt":"msg","to":"test-group","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":460,"wires":[["889eca4b.3b4f7"]]},{"id":"889eca4b.3b4f7","type":"debug","z":"39804c96.ce1be4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":480,"wires":[]},{"id":"8bb7448c.dc3238","type":"server","name":"Home Assistant Parent","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]
 
In Node-Red, like most coding environments, there are always lots of ways of achieving the same goal. Here's a sample flow that hopefully demonstrates two alternative approaches to adding your group setting to an incoming message to match the structure at https://companion.home-assistant.io...tions-basic/#thread-id-grouping-notifications :

Code:
[{"id":"6649753b.17bcc4","type":"inject","z":"39804c96.ce1be4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"title\":\"Test Title\",\"message\":\"Test Message Content\"}}","payloadType":"json","x":930,"y":440,"wires":[["97b77793.666e08","f84af1ad.6004b8","d540e0d0.17bd3"]]},{"id":"5cf51439.d59ea4","type":"api-call-service","z":"39804c96.ce1be4","name":"Notify Me","server":"8bb7448c.dc3238","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_your_phone","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1320,"y":420,"wires":[[]]},{"id":"97b77793.666e08","type":"debug","z":"39804c96.ce1be4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1030,"y":360,"wires":[]},{"id":"f84af1ad.6004b8","type":"function","z":"39804c96.ce1be4","name":"add group","func":"msg.payload.data.data = {};\nmsg.payload.data.data.group = \"test-group\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1110,"y":420,"wires":[["d77d81a.75a9","5cf51439.d59ea4"]]},{"id":"d77d81a.75a9","type":"debug","z":"39804c96.ce1be4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":380,"wires":[]},{"id":"d540e0d0.17bd3","type":"change","z":"39804c96.ce1be4","name":"add group","rules":[{"t":"set","p":"payload.data.data.group","pt":"msg","to":"test-group","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":460,"wires":[["889eca4b.3b4f7"]]},{"id":"889eca4b.3b4f7","type":"debug","z":"39804c96.ce1be4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":480,"wires":[]},{"id":"8bb7448c.dc3238","type":"server","name":"Home Assistant Parent","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]
Awesome, thanks for the help. I will play around with this flow to see how it all works, and then see if I can make it work on the other flows.
 
Awesome, thanks for the help. I will play around with this flow to see how it all works, and then see if I can make it work on the other flows.
Cool. Shout if you need more help. It's just occurred to me that I may have misread your original question slightly. I had assumed you had a message coming in and wanted to add the group property to that message, so my flow shows how to do that with either a function node or a change node. It now occurs to me that you might have been asking what to (manually) add to your sample message to include the group property. ie: How to include that in the original inject (and not how to add it later). If that's actually what you wanted, and you can't get there from my examples, let me know and I'll provide some more inject examples.
 
OK. The error you were seeing was because I had assumed you already had a properly formed message structurearriving at the notification node, but in reality both your MQTT input and your timestamp inject node just had a simple string or integer in msg.payload. That meant that msg.payload wasn't an object that could be extended either of the sample nodes I had supplied. You were relying on the templating functionality built into the Notify node to build a complete message. You could just extend that structure to include the bits you want, like group & priority, but it's marginally easier to do that with a template node because you can then see what your final message structure looks like by attaching a debug node to the output of the template node.

Hopefully this works better for you:

Code:
[{"id":"333f6679.247ff2","type":"template","z":"fc7e6078.7212e","name":"build message","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n    \"data\": {\n        \"title\": \"This is the Title\",\n        \"message\": \"This is the body. {{topic}} said {{payload}}\",\n        \"data\": {\n            \"attachment\": {\n                \"hide-thumbnail\": false,\n                \"content-type\": \"jpeg\"\n            },\n            \"push\": {\n                \"category\": \"camera\"\n            },\n            \"entity_id\": \"camera.your_camera_entity_id\",\n            \"url\": \"/lovelace/whatever_path\",\n            \"group\": \"Motion\",\n            \"priority\": \"high\"\n        }\n    }\n}","output":"json","x":480,"y":760,"wires":[["f96acbc.49a0038","88aff2db.b3ac6"]]},{"id":"617f10a3.9b7f88","type":"mqtt in","z":"fc7e6078.7212e","name":"","topic":"ai/Inrit/motion","qos":"2","datatype":"auto","broker":"f49da766.508f58","x":250,"y":800,"wires":[["333f6679.247ff2"]]},{"id":"60164d9c.8511bc","type":"inject","z":"fc7e6078.7212e","name":"Push to Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test_button","payload":"","payloadType":"date","x":270,"y":720,"wires":[["333f6679.247ff2"]]},{"id":"f96acbc.49a0038","type":"debug","z":"fc7e6078.7212e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":720,"wires":[]},{"id":"88aff2db.b3ac6","type":"api-call-service","z":"fc7e6078.7212e","name":"Notify","server":"9d419342.643f8","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_huawei_vns_l31","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":650,"y":800,"wires":[[]]},{"id":"f49da766.508f58","type":"mqtt-broker","name":"HABroker","broker":"http://localhost","port":"1883","clientid":"HAClient","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9d419342.643f8","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Note that I've emptied the Data field in the notify field because the whole thing gets built up in the template node before it which makes everything a lot clearer as far as I'm concerned. You can just tweak the template node to add or remove bits of the template. Since I'm assuming you'd like a snapshot of the camera included in the notification I've included the attachment, push and entity_id sections documented at https://companion.home-assistant.io/docs/notifications/dynamic-content and also the url field which allows you to control which of your Home Assistant views you'll land on if you click through from the notification. The last two fields are your group and priority.
 
Top
Sign up to the MyBroadband newsletter
X