Sharing IOS Notification Template with Example ** Improved **

Thank you for sharing. I was looking for a smarter way of setting up my notifications and this is better than what I came up with myself.

I extended it a bit to fit my needs.

Whenever someone in my family has a new phone they also get a new mobile_app<your_device_id>. I don’t like to change all nodes again, and I’ll probably forget some. So what I did:

EDIT: I found an easier way. Read my reply below this one.

In Home Assistent on a dashboard I created a input_text per person, where I enter the device id. In node red I watch for changes on that input and if it changes it will fill a global.name . In the function node ‘create IOS alert’ (I use it for android) I added an if statement that fills msg.topic depending on the name I entered in the change node for target.

Now when someone has a new device the only thing I have to do is change it in home assistant.

var title = msg.title
var message1 = msg.message1
var message2 = msg.message2
var message3 = msg.message3
var sound = msg.sound
var critical = msg.critical
var volume = msg.volume
var target = msg.target
var final_msg = `${message1}` + `${message2}` + `${message3}`
var payload = {"data":
{
    "message": `${final_msg}`,
    "title": `${title}`,
    "data": {
        "push": {
          "sound": {
            "name": `${sound}`,
            "critical": `${critical}`,
            "volume": `${volume}`,
          }
        }
    }
}
}
if (target == 'Person1'){
   msg.topic = 'mobile_app_'+global.get("person1_phone_code")
}
if (target == 'Person2'){
   msg.topic = 'mobile_app_'+global.get("person2_phone_code")
}
msg.payload = payload

return msg

So above (my previous reply) can be done easier by creating notifaction groups in your configuration.yaml.

You can name the group just like a person and just use one service for mobile_app_<your_device_id_here>. Create a group for every person and just enter this in the target field of the change node. Of course it’s also possible to make groups for your whole family and notify them all at once.

notify:
  - name: ALL_DEVICES
    platform: group
    services:
      - service: mobile_app_iphone_one
      - service: mobile_app_iphone_two
      - service: mobile_app_ipad_one
      - service: mobile_app_pixel_4_xl

Hello,

first, thanks for all this input, which helped me a lot simplifying my notifications!

I am struggling around to integrate an option to acheve

  • notification only to family members at home
  • notification to all or only dedicated persons

Tried lots of things but seems my knowledge in Node-Red is not (yet) enough for this.

Maybe s.o. can point me into the right direction?
Thanks, Ralf

I would start playing with template sensors with if statements. That would require yaml. You could do the same with a function node, but it would require the same yaml.

Scroll down the example of multi line with if

Exactly what I was looking for :smiley:
Thank you Lars

1 Like

I’m going to put this to use, as it’s been arduous to create notifications for each condition…

I have a question about syntax. I want to include the state of an entity as part of the text in the message. So far, I’ve been unsuccessful. The state of sensor.dark_sky_alt_wind right now is 78 mi/h from the north-northeast. I’d like the contents of the message to be something like:

The wind is currently blowing 78 mi/h from the north-northeast.

I’m unsure how to do that. My tests with setting the message as {{ sensor.dark_sky_alt_wind.state }} or {{ "sensor.dark_sky_alt_wind.state" }} result in a blank message on iOS.

If that is a parent sensor, and not a child attribute, you should be able to just use msg.payload

Use the debug node to see what the msg.payload says.

1 Like

Thanks–that’s exactly what I’ve tried. Here’s what I’m seeing (full flow visible in second image):

As you can see, the contents of msg.payload aren’t what I’m looking for. I’ve tried with and without mustache brackets.

quick reply due to work stuff.
You should be set flow.whatever to msg.payload

more later if not understood

More…

I can not see what is triggering your input to the windspeed. But you set message change node is definitely incorrect. You need the input to be the msg from the sensor. That will come in as msg.payload. You then use a change node and set flow.whatever to msg.payload. Then you need the function node to construct the notification.

Here is an example where if the outside temp is <50 it announces

[{"id":"094423da7cb0f64b","type":"server-state-changed","z":"45c1f86732272d89","name":"Outside Temp <50 (State Node)","server":"2a12269e.94634a","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.outside_temp","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"50","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":260,"y":500,"wires":[["64fb66f26bf0ed32"],[]]},{"id":"2a12269e.94634a","type":"server","name":"Hingham Home","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Trying out this structured setup.
Does anyone have an example with sending an image please?

Hi there.

This is exactly what I’ve been looking for so much kudos for the work. :slight_smile:

I do however have to report that I am unable to invoke the critial alerting. I have update the filed to 1 but is not getting delivered as a critical alert.

Please advise.

//Thomas

This is my working critical alert if it helps


{
    "title": "Water Leak Alert - Bathroom",
    "message": "{{states.sensor.time.state}} {{states.sensor.date.state}}",
    "data": {
        "push": {
            "sound": {
                "name": "default",
                "critical": 1,
                "volume": 1
            }
        }
    }
}

There seem to be a problem with strings vs numbers in this setup. Even when changing the variables in the change node to number (for critical and volume) they are still being parsed as text in the function.

Only when changing the code in the “create IOS Alert” function to convert to integer and float respectively was I able to get it to work.

Code included:

var title = msg.title
var message1 = msg.message1
var message2 = msg.message2
var message3 = msg.message3
var sound = msg.sound
var critical = msg.critical
var volume = msg.volume
var target = msg.target
var final_msg = `${message1}` + `${message2}` + `${message3}`
var payload = {"data":
{
    "message": `${final_msg}`,
    "title": `${title}`,
    "data": {
        "push": {
          "sound": {
            "name": `${sound}`,
            "critical": parseInt(`${critical}`),
            "volume": parseFloat(`${volume}`)
          }
        }
    }
}
}
msg.payload = payload
msg.topic = target
return msg
1 Like

Hi everyone,
I think that these Node-RED templates are extremely helpful for most notifications. I’m quite new to Node-RED (started last week), and I’m trying to migrate as many automations as I can to Node-RED from HA.

One automation I have in HA is for triggering enter/exit zone notifications. Works flawlessly in HA, but I can’t figure out how to do it in Node-RED. It works dynamically with an IF statement in the message template:

  • Uses the person’s friendly name…then
  • Knows which zone they just left/entered…then
  • Puts a notification together with all of the relevant parts and sends that to everyone.

Let’s just say, I’m not smart enough to create IF statements in function nodes yet.

Here is the yaml. Would anyone have an idea of how to migrate this to Node-Red?

alias: 'NOTIFY | ZONE: Enter/Exit Zone notification'
description: Sends notification upon entry or exit from a zone
trigger:
  - platform: state
    entity_id: person.me, person.wife
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
  - service: notify.notify
    data_template:
      title: Location update {{ states.sensor.time.state }}
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} {% if
        trigger.to_state.state == 'not_home' %}has left {{
        trigger.from_state.state }}{% endif %}{% if trigger.from_state.state ==
        'not_home' %}arrived at {{ trigger.to_state.state }}{% endif %}
mode: single

image

[{"id":"10592baf95622d19","type":"trigger-state","z":"91a80f99.6180e","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"person.me, person.wifre","entityidfiltertype":"substring","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"is_not","comparatorValueDatatype":"prevEntity","comparatorValue":"state","propertyValue":"new_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":324,"y":4080,"wires":[["173ca031c6d44d13"],[]]},{"id":"173ca031c6d44d13","type":"api-call-service","z":"91a80f99.6180e","name":"","server":"","version":3,"debugenabled":false,"service_domain":"notify","service":"notify","entityId":"","data":"{ \t    \"title\": \"Location update \" & $entities(\"sensor.time\").state,\t    \"message\": data.new_state.attributes.friendly_name & (payload = \"not_home\" ? \" has left \" & data.old_state.state) & (data.old_state.state = \"not_home\" ? \" arrivied at \" & payload)\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":592,"y":4080,"wires":[[]]}]

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/call-service.html

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/jsonata.html

Ok…I’m embarrassed.

Thank you, Kermit. That was way too easy for you I think!

Where’s the best place to start learning how to program this stuff?

Thanks again :slight_smile:

I think it’s missing the IF part? (am I wrong?) How would I include that IF in my original…I usually know how to do these things, but I don’t know how to format parts of these messages quite yet.

The “if” part is inside the call-service node data field

{ 
    "title": "Location update " & $entities("sensor.time").state,
    "message": data.new_state.attributes.friendly_name & (payload = "not_home" ? " has left " & data.old_state.state) & (data.old_state.state = "not_home" ? " arrivied at " & payload)
}

These are the two notifications I got when I left and returned home (the bottom one obviously was when I left, the top one was when I entered the zone). Any idea why the rest of the message is missing?

Whoops, forgot the new_state and old_state properties are one level down under event

like data.event.new_state.attributes.friendly_name, So you’ll need to add that.

{ 
    "title": "Location update " & $entities("sensor.time").state,
    "message": data.event.new_state.attributes.friendly_name & (payload = "not_home" ? " has left " & data.event.old_state.state) & (data.event.old_state.state = "not_home" ? " arrivied at " & payload)
}