Skip to content

Instantly share code, notes, and snippets.

@sstratoti
Last active April 15, 2024 00:49
Show Gist options
  • Save sstratoti/8021c5a4ee8e34313c3f59ba20c4a83a to your computer and use it in GitHub Desktop.
Save sstratoti/8021c5a4ee8e34313c3f59ba20c4a83a to your computer and use it in GitHub Desktop.
iOS Subflow for HomeAssistant Companion notifications
@sstratoti
Copy link
Author

Options Description Documentation
Group Name You can edit this in the subflow directly and add in the values for your groups. Grouping combines notifications together visually. link
Tag (for replacing messages - optional) Replace an existing notification by using a tag for the notification. All subsequent notifications will take the place of a notification with the same tag. If a tag is not provided, one will be auto-generated. link
Notify Service Can take multiple services as a comma delimited list e.g.: mobile_app_username, mobile_app_username2
Title Top line of text link
Subtitle (optional) Second line of text link
Message Message Text
Notification URL (optional) lovelace dashboard, https:// or app://. link
Pre-installed Custom Sound These are the sounds that are pre-installed with the companion app. link
Custom Sound (Optional - will override pre-installed selection) Please see the companion documentation. link
Critical Notification? iOS gives special priority to this type of notification. Critical alerts always appear at the top of your lock screen above all other notifications, and play a sound even if Do Not Disturb is enabled or the iPhone is muted. link
Populate User Information Will resolve user info in Node-RED and place it in msg.userData
Clear notifications on Action Received? Will clear all notifications related to this if an action is recieved from any of the devices notified. link
Action [x] Title iOS notifications can have ufrom ZERO to 4 actions. If you do not populate any of the action titles, the notification sent will just be a non-actionable notification. This is the title that is displayed on the action button. link
Action [x] URL (optional) Optional. The URL to open when tapped
Action [x] Activation Mode Set to foreground to launch the app when tapped. Defaults to background which just fires the event. This is automatically set to foreground when providing a uri. link
Action [x] App Authentication Required? true to require entering a passcode to use the action. link
Action [x] Destructive? true to color the action's title red, indicating a destructive action. link
Action [x] Behavior textInput to prompt for text to return with the event. This also occurs when setting the action to REPLY. link
Action [x] Text Input Button Title (Required if behavior is Text Input) Title to use for text input for actions that prompt. link
Action [x] Text Input Placeholder (Optional - only if behavior is Text Input) Placeholder to use for text input for actions that prompt. link
Latitude for Pin 1 Will show a centered map with a red pin at the given coordinates. link
Longitude for Pin 1 Will show a centered map with a red pin at the given coordinates. link
Latitude for Pin 2 The latitude of the second pin. link
Longitude for Pin 2 The longitude of the second pin. link
Show a line between points? Displays a line connecting the first and second pin. link
Show a compass on the map? Displays a compass control on the map. link
Show points of interest? Displays point-of-interest (POI) information on the map. link
Show scale information on the map? Shows scale information on the map. link
Show Traffic? Displays traffic information on the map. link
Show User Location? Attempts to display user's location on the map. link
Camera Entity The preview thumbnail of the notification will display a still image from the camera. When expanded, the notification content displays a real time MJPEG stream if the camera supports it. link
Image Path (10MB limit) An attachment is an image, video, or audio file which is downloaded to the device when a notification is received and shown alongside the notification. A thumbnail is shown when the notification is not expanded. The full size attachment is shown when the notification is expanded. link
Video Path (50MB limit) link
Audio Path (5MB limit) link
Content URL (overrides image/video/audio) Optional. The URL of content to use as the attachment. This URL must be accessible from the Internet, or the receiving device must be on the same network as the hosted content. This overrides any image, video or audio values. link
Load Media in Notification Lazily? Optional.If set to true the attachment will not be downloaded immediately and will only be loaded when viewing the notification. Use this to avoid downloading obviously-too-large attachments, but if they are only occasionally too large, you shouldn't provide this key as the app can attempt both. link
Hide thumbnail? Optional. If set to true the thumbnail will not show on the notification. The content will only be viewable by expanding. link

@thebaoster
Copy link

thebaoster commented Jul 23, 2021

Thanks for this awesome code. I'm keep getting error not sure how would one fix it. Also want to mention that it go straight to action 1 when I haven't selected anything yet.

7/23/2021, 1:42:02 PMnode: create CLEAR service call
function : (error)
"TypeError: Cannot read property 'replace' of undefined"

@sstratoti
Copy link
Author

Try this latest version, I've been testing it internally and have corrected a few issues. I've updated the gist with the latest.

@sstratoti
Copy link
Author

Also, I'm still tweaking this, and eventually plan to do a write up of it. Just not sure when!

@thebaoster
Copy link

Try this latest version, I've been testing it internally and have corrected a few issues. I've updated the gist with the latest.

Thanks! for your time and help! This node has been doing awesome for me. I've place the new node in but still getting error.
Capture

7/26/2021, 3:52:02 PMnode: create service call
function : (error)
"ReferenceError: randomId is not defined (line 50, col 81)"

@thebaoster
Copy link

Try this latest version, I've been testing it internally and have corrected a few issues. I've updated the gist with the latest.

Thanks! for your time and help! This node has been doing awesome for me. I've place the new node in but still getting error.
Capture

7/26/2021, 3:52:02 PMnode: create service call
function : (error)
"ReferenceError: randomId is not defined (line 50, col 81)"

Hmm I think I found the issue. I do not have the random-id node. trying to find it in the pallet but couldn't. https://www.npmjs.com/package/random-id

@sstratoti
Copy link
Author

So in the "on start", do you have

const randomId = () => Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);

@sstratoti
Copy link
Author

The "on start" of "create service call" I mean to say.

@sstratoti
Copy link
Author

image

@thebaoster
Copy link

thebaoster commented Jul 27, 2021

The "on start" of "create service call" I mean to say.

Yes I've copied the code and created a new node.

I've also add it to the top of the On Message just in case and it actually ran but I started to have all kind of issue. Like action 1 and 4 doesn't flow after I click it on the phone. The error also cam back

7/26/2021, 8:31:56 PMnode: create CLEAR service call
function : (error)
"TypeError: Cannot read property 'replace' of undefined"

Capture

@Automot360
Copy link

Very useful node, thank you.
I played around with and have the same error:
"ReferenceError: randomId is not defined (line 50, col 81)".
Is there a solution for that?
Thanks again for the work, will be nice to used it.

@sstratoti
Copy link
Author

Figured it out. The issue is that you weren't setting a "tag" in the settings, whereas in my testing - I always had.

Should be fixed in the version thats up there now.

In addition, you can now send in some overrides for the communications.

If you create a message object with the following structure (note, any of these are optional):

msg.notificationOverride.title = "dynamic title!";
msg.notificationOverride.subtitle = "dynamic subtitle";
msg.notificationOverride.message = "Some new message like Battery is at " + msg.payload.attributes.battery + "%";
msg.notificationOverride.services = "my_phone, partners_phone, kids_phone"; //could override this depending on who is "home".
msg.notificationOverride.tag = "xyzTag" // could be that you want to send the same message, but have it appear multiple times on the device rather than overwritten.
msg.notificationOverride.clear = true; // will attempt to clear all messages to all specified devices in the setup or in msg.notificationOverride.services if overridden. I believe this currently isn't working because apple isn't allowing them to do this with the app. But they've petitioned for it... 

and push it into the input of the node, it'll override any settings you have set at are above.

@Automot360
Copy link

Thanks, now everything works, great Node, now I can change all my IOS based automations.
One thing I recognized, maybe not related to your Flow, I am able to send only 2 Messages in the row for about 1 Minute.
All other Messages are lost.
Again great Sub flow thanks a lot.

@sstratoti
Copy link
Author

That might have to do with iOS and rate limiting. If you go into the subflow (double click it in the palette) and then drag a connection from the API call (blue node called "send notifications") to one of the outputs - say output 4. Then place a debug from that output in your flow, and you'll see the response from the API call in the debug window.

@Automot360
Copy link

Thank for your answer, sorry its a different problem with the Node Red Plugin, its losing the web socket connection to Home Assistant
"[server:Home Assistant] Connected to undefined)", discussed in other Treads. I found out that Node Red crashes after sending the first and second IOS Message.
Thanks again for this nice Sub flow, will use it a lot.

@Post404
Copy link

Post404 commented Aug 18, 2021

This is amazing and works almost perfectly for me.
The only thing is that when I press any button, it always opens the app even though I have "activation mode" set to background.
There is also no URL provided anywhere.
Am I doing something wrong?

@sstratoti
Copy link
Author

Hmm, I'll check into that! I think it does the same for me.

@sstratoti
Copy link
Author

OK I've updated the flow. Even though URI wasn't populated, it was sending in an empty string, which was causing the default behavior of "foreground" when it is populated.
I also fixed an issue where if msg.notificationOverride wasn't populated, it would send a message twice! Weird bug with my settings on the switch node. Give this update a try? I've edited the gist with the latest.

@sstratoti
Copy link
Author

sstratoti commented Aug 19, 2021

Oh wait - needed to update once more to remove a debug. Should be good....now!

@Post404
Copy link

Post404 commented Aug 19, 2021

I've tested it and it works perfectly!

@Post404
Copy link

Post404 commented Oct 3, 2021

I am using this subflow for a lot of things now and it is working perfect, but while fixing some errors from the log I noticed that this subflow is generating errors (i just cleared it so there is only 1 occurrence):

Logger: homeassistant.components.websocket_api.http.connection
Source: components/websocket_api/connection.py:89
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 22:40:40 (1 occurrences)
Last logged: 22:40:40

[547425187728] Received invalid command: mobile_app/push_notification_channel

I don't think this is a problem, but i'm thinking it might cause one when updates happen. Do you have this error aswell?

@sstratoti
Copy link
Author

Subscribe to this issue:
home-assistant/iOS#1700

From the looks of it, I don't believe it hurts anything.

@Post404
Copy link

Post404 commented Oct 4, 2021

Thanks, that explains it, i've subscribed to it.

@fireone23
Copy link

fireone23 commented Oct 18, 2021

Feel silly asking this question, but I have read through this thread a few times now, I keep getting a 'no connection' error message on the node. Was expecting at least getting a notification as I was getting using the android version of the same subflow. I figure I'm doing something wrong with the config, I have tried both using both notify and mobile_app_eddie as the notify service, neither worked. Can anyone please assist?


Screen Shot 2021-10-18 at 3 50 43 PM

Screen Shot 2021-10-18 at 3 51 08 PM

@pistak23
Copy link

Hello, thank you for this node, but I have a problem with Custom Sound (Optional - Will Override Pre-Installed Selection). When I write own sound, from system sound (I imported them, already) still play only Pre-Installed Custom Sound... Have you the same problem? Or I have some mistake? thank you very much

Snímka obrazovky 2021-10-26 o 8 22 15
.

@sstratoti
Copy link
Author

Initial thought - never heard of a .caf file before. I think the delivered sounds are all wavs?

You might want to check the iOS app documentation to see what is supported?

If supported let me know and I’ll take a deeper look?

@pistak23
Copy link

pistak23 commented Oct 26, 2021

I used this sound before, and works well... Now with this node they dont work. look at this, please. It is screen from Home assistant website.
This is a part of code, and play .caf sound now. Only this node don't play custom sound:
"sound": {
"name": "3rd_Party_Critical_Haptic.caf",
"critical": 0,
"volume": 1
}

And the .waw format I also have, and no works too :(

Snímka obrazovky 2021-10-26 o 13 12 30
Snímka obrazovky 2021-10-26 o 13 11 46

@pistak23
Copy link

Hello again,
I tried to repair this mistake, and I think I done it. In subflow in node function "create service call" I replaced "customSoundPreInstalled" and "customSound" and now it works. Please check it and if is correct, update this node for other people. Thank You

Snímka obrazovky 2021-10-27 o 0 25 53

@fireone23
Copy link

Feel silly asking this question, but I have read through this thread a few times now, I keep getting a 'no connection' error message on the node. Was expecting at least getting a notification as I was getting using the android version of the same subflow. I figure I'm doing something wrong with the config, I have tried both using both notify and mobile_app_eddie as the notify service, neither worked. Can anyone please assist?

Screen Shot 2021-10-18 at 3 50 43 PM

Screen Shot 2021-10-18 at 3 51 08 PM

I figured out the issue. For the call-service nodes in the subflow, I have multiple instances of the Home Assistant server listed, changing that around and found one that allowed the connection.

Screen Shot 2021-10-26 at 3 38 51 PM

@sstratoti
Copy link
Author

@pistak23 - thanks for catching that. The default value is set to "default" which is obviously > 0, so the custom sound would never hit in the logic. Swapping those variables will have it check the custom first, then the default sounds - so that looks good to me. I'm making a correction and will edit the gist to update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment