I’m trying to see if I can trigger different focus modes on iOS from home assistant.
The recent update added a sensor for detecting if a focus mode is set, but it seems it’s status only.
I made a shortcut to turn on sleep focus mode, but can’t figure out how to trigger it from home assistant. This may be a restriction by apple, but maybe someone knows how? Any help is greatly appreciated.
This only triggers when you click the notification tho. That will work for most focus modes but I would like to trigger sleep mode without any interaction if possible.
True. Some personal automations can run automatically. The only hands-free ones that could be triggered by HA are Email/Message but these cannot run automatically. The option with the least friction I’ve found is NFC, as these can run automatically, although they do require a fairly recent iPhone.
Create a switch in HA and make it available in iOS Home, create an automation in iOS Home that turns the Focus on and off when the switch is turned on and off
Create an automation in HA to turn the switch in and off as necessary
But I don’t think homekit allows you to trigger focus mode from toggling an input Boolean. I may be wrong, but when I tried this before, it wasn’t an option.
My scenario is time based and my janky solution works for my situation
In Home Assistant I have created input_boolean.working
I have also created binary_sensor.working that uses a template to monitor the status of input_boolean.working
In my HomeKit integration I have included binary_sensor.working and it appears in my Apple Home as an occupancy sensor called “Working”
I have one Home Assistant automation that runs at 08:50 that turns on my office lights and turns on input_boolean.working and one automation that runs at 18:10 that turns off my office lights and turns off input_boolean.working
In IOS Shortcuts I created a shortcut that checks the status of the “Working” occupancy sensor and it turns on my Working Focus if Occupancy is detected and turns my Working Focus off if not
I then created two Personal Automations in Shortcuts that run at 08:55 and 18:15 that call the above shortcut
This works perfectly for me. My next task is to add some logic to my Home Assistant automations so that they do not run on public holidays and vacations
I don’t see a way to do this in iOS 15. There’s no way to enable Focus mode when something from Homekit happens. The only action frok the Settings section is “Get device details”.
Coming here late, but I have done this - not that it’s pretty, and requires a MacOS device.
MacOS can run a shortcut (eg. shortcuts run 'Sleep on'), and that shortcut can turn focus modes on or off. I installed Node Red on my Macbook, and then send an MQTT message to it from HA. Node Red couldn’t run the shortcut directly, and it was taking too long to find an answer, so I gave up and had it write a file. An Automator folder action workflow then ‘sees’ the file and runs the shortcut. If it starts proving fragile I might re-investigate, but for now it’s doing what I need - when I tell the Google Home “I’m going to sleep”, it turns on sleep focus, and off again when I say “I’m awake” (along with other automations).
Obviously this requires focus to be shared across devices for it then to update your phone.
Since I’m running on a Macbook, I also used Amphetamine to stop it going to sleep when the lid is closed.
Interesting to see how others handled it.
In my case, I’m doing something very similar.
I run webhook in the background which runs shell scripts to execute the shortcuts command.
The overall outcome is the same as yours, but with a slightly different implementation as I trigger the action by a http call.
To be honest, the NodeRED approach with MQTT might be better, especially as I have two MacOS devices that I use and MQTT would allow them to receive a message with QOS so that the message is delivered only once. The broker could be connected to over the internet and therefore would eliminate the need for the device to be on LAN.
I came here to check if anything changed on this front, but it’s sort of ridiculous that there’s no API to control DND via Apple’s cloud.
And just in case it’s useful for anybody else, I found that running a shortcut on the MacBook to turn on sleep mode did not start sleep tracking on my watch. No idea why, and since I found a solution I never looked into it further. Instead I had the shortcut turn on DND, and then a shortcut automation that runs when DND is turned on, to turn on sleep mode. If you already use DND, you could create a new mode (eg. pre-sleep) and automate based off that.
Once every 3 months I come back to this task and I think I’m getting close, but still not there yet…
My current status:
I have a device/entity in HA
when it changes status to something specific, I call the service: notify.mobile_app_my_iPhoneName with the data: shortcut: shortcut name as described by OP
on my iOS device I have defined a shortcut that basically says “Turn Do Not Disturb On until Turned Off”, I believe it should work for any focus mode
when the service is triggered I do receive the notification and if (and only if) I click on the notification status, by focus mode changes
Now browsing on stackexchange I found that Apple have documented how to trigger personal automations without asking for confirmation/user input.
Unfortunately I can’t make this work for Shortcuts and I don’t know how to create an iOS Automation that changes the iOS Focus Mode.
I hope some good soul will take this further.
Create an Input_text helper called something like “Iphone Focus”.
Use Apple Shortcuts and create a new automation.
Use any of the available focuses as triggers. For example, “Work”. The trigger can be the focus is turned on or turned off.
4.Set the shortcut up to when triggered send call input_text.set with the name of the Focus you want.
This can be done on all available focuses in IOS or even any custom ones you build.
Sure, you can call any Home Assistant service from an iOS Shortcut when an iPhone Focus is manually set or unset using that method. I’ve been doing that for at least a year or two.
But what this thread is about is going the other way. Automatically setting the iPhone Focus from a Home Assistant automation/action (without directly handling your phone). Apparently, that still can’t be done.
This might be a possible solution but it is very cumbersome I must say and also still a workaround since not everything is handled in HA. First off my usecase is as follows: I want my vacuum cleaner to start immediately when both me and my girlfriend are not in the house, and only if it is between 10.00 and 14.00. Now I walk the dog a lot and I don’t always leave the gps zone, but I do want the vacuum to start automatically. So I thought, when my phone disconnects from wifi that would be a nice time to start the vacuum.
This gave me a problem because somehow my ssid does not update immediately if my wifi is disconnected. This can take up to half an hour, so I want to trigger an “update sensors” on my phone. So my usecase is a bit different then stated before because “update sensors” is an option in shortcuts. Hence it is easier to achieve then the following manual.
So my solution (though I do not know the impact on my battery yet):
In your iOS device make a personal automation for a specific time
Put in the time that is definitely before the time you want your automation to trigger (for me that was 10.00) every day
Fill in the task “Repeat” as often as you’d like (for me 840 times)
Search in tasks for Home assistant and fill in task “Assist”
Go to the home assistant app and then to your variables section
Find the variable you’d like to use (can also create a new one)
Go to it’s settings
Then go to Voice Assistant
Make your variable recognizable.
Fill in a keyword
In step 4 you filled in the task “Assist”, fill in your keyword there.
Now you will receive the value of your variable in the automation.
Do an action based on your variable. F.i. if true set focus and if false wait for a few seconds, because otherwise the loop will finish fast (for me 20 seconds).
FYI. I don’t know why exactly but I could not just put the if statement after my “Assist” task. I first had to copy the result to my clipboard but maybe that is just me.
You can then proceed to switch your Focus depending on your variable. So if you have an HA automation which results to setting the variable, you should be good.
This can be done using an iOS app called PushCut, which needs to be perpetually running on an extra iPhone.
In Home Assistant, you can then call a URL: https://api.pushcut.io/<your personal PushCut code>/execute?shortcut=Focus%20Set%20Sleep
This URL triggers the Pushcut app to run the requested shortcut on the phone running the PushCut server (without a prompt or user interaction), and that focus change will update on all connected iOS devices and change the focus on your active iPhone.
It was pretty complicated to setup. Let me know if you’d like to know more.
My question is a little different, but along the same lines.
I have been wondering if an automation could be created that when my wife is talking on her iPhone it will trigger mute on the TV or turn down the volume. Has anybody ever heard of such an automation?