Using msg property in node red notification

I’m building a garage door reminder to remind me if the garage door has been open for more than 30 minutes and continue a looping reminder every 30 minutes until closed. If it is left open, send a push notification to my phone with actions to close the door or ignore loop.

I have everything working correctly in node-red but I want to pull the time the door was opened into the notification. I’m capturing the time the door was opened by getting current state of sensor.time and passing it along as msg.open_since. I then set the payload with the JSON below and pass it to a call service node that notifies my phone app. I can’t seem to pull the open_since value into the string and have tried various different syntax, including sending the time as payload and using that in the string.

I’ve added debug nodes and can confirm that msg.open_since does contain a string of the time value. The JSON passed to the notify call service contains the curly brackets within the object but I assume that’s expected until the notification hits my phone. Any suggestions?

{
    "data": {
        "title": "Garage door left open",
        "message": "Garage door has been open since {{open_since}}.",
        "data": {
            "actions": [
                {
                    "action": "close_garage",
                    "title": "Close Door"
                },
                {
                    "action": "stop_garage_loop",
                    "title": "Stop Alerts"
                }
            ],
            "tag": "garage_reminder",
            "group": "garage_reminder"
        }
    }
}

Not sure if there is a better way but I just came across this post where they are using a function to setup the message and then passing to the call service node. I have not tried it but thought I would share the link.

https://community.home-assistant.io/t/sharing-ios-notification-template-with-example-improved/176838

1 Like

Thanks for pointing me to this. I was planning something similar to consolidate my notifications to link to one standard flow for push notifications based on the passed data. It looks like I may move that up my list of priorities. It seems they solved this by building the object within a Javascript function as opposed to what I was trying to do with building it directly as a JSON file.

Still curious if others solve for this within the change node JSON.

Got back to this and found my problem, so updating for those looking at this later.

I think the problem is that the change node I was using to set the payload data wasn’t able to access the msg attribute since I was reseting the payload data. My solution was to remove this change node and add the notification data directly in the call service. This allowed me to call my msg data by the property. Below is the format used in the data field of the notify call service node.

{
    "title": "Garage door left open",
    "message": "Garage door has been open since {{open_since}}.",
    "data": {
        "actions": [
            {
                "action": "close_garage",
                "title": "Close Door"
            },
            {
                "action": "stop_garage_loop",
                "title": "Stop Alerts"
            }
        ],
        "tag": "garage_reminder",
        "group": "garage_reminder"
    }
}