<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul Wheeler on Medium]]></title>
        <description><![CDATA[Stories by Paul Wheeler on Medium]]></description>
        <link>https://medium.com/@paul.william.wheeler?source=rss-32c989ab3660------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tlrYm2o_W62g71QQ.</url>
            <title>Stories by Paul Wheeler on Medium</title>
            <link>https://medium.com/@paul.william.wheeler?source=rss-32c989ab3660------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:31:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paul.william.wheeler/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Syncing iOS wake up alarm to Home Assistant]]></title>
            <link>https://medium.com/@paul.william.wheeler/syncing-ios-wake-up-alarm-to-home-assistant-eaf33d683c66?source=rss-32c989ab3660------2</link>
            <guid isPermaLink="false">https://medium.com/p/eaf33d683c66</guid>
            <category><![CDATA[home-assistant]]></category>
            <category><![CDATA[home-automation]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Paul Wheeler]]></dc:creator>
            <pubDate>Mon, 09 Sep 2024 07:17:59 GMT</pubDate>
            <atom:updated>2024-09-09T07:17:59.418Z</atom:updated>
            <content:encoded><![CDATA[<p>Having had <a href="https://www.home-assistant.io/">Home Assistant</a> for a while, I wanted to explore whether it could help me leap out of bed in the morning.</p><p>I felt sure there’d be some prior art that I could use and sure enough found some:</p><ul><li>Home Assistant forum question —<a href="https://community.home-assistant.io/t/get-wake-up-alarm-from-a-phone"> Get wake up alarm from a phone</a>; which links to</li><li>A super-useful post on <a href="https://wltd.org/posts/how-to-automatically-sync-ios-alarms-with-home-assistant">How to automatically sync iOS alarms with Home Assistant</a></li></ul><p>Of course I started to follow the guide whilst trying to understand what it is doing. The overall concept is very cool — using the iOS Shortcuts app to send information to Home Assistant. More specifically, the flow is like this:</p><ol><li>Trigger the iOS Shortcut during the night</li><li>Get all the alarms</li><li>Find the alarm labelled “bedtime”</li><li>Send the time as the state of a sensor called “sensor.bedtime” to Home Assistant.</li><li>In Home Assistant, configure an automation using the time sensor such that the automation will run at “sensor.bedtime”. Add any other conditional logic (e.g. to suppress an automation) as you desire.</li></ol><p>However, there’s one big limitation with this solution:</p><p><strong>iOS Shortcuts does not expose whether an alarm is enabled or not.</strong></p><p>There’s therefore no way to guarantee that the sensor in Home Assistant will have the right value. Perhaps the alarm is off on weekends; perhaps I have a weekday off and I’ve disabled it. One can build predictable rules in to the automation in Home Assistant (e.g. don’t fire on a weekend) but you can never guarantee 100% accuracy.</p><p><strong>But there is another way.</strong></p><p>If you’re using the Sleep Schedule in iOS, then you have access to two very relevant <a href="https://support.apple.com/en-gb/guide/shortcuts/apd932ff833f/ios">event triggers in Shortcuts</a>:</p><ol><li>Alarms / Wake-up — trigger when your wake-up alarm goes off</li><li>Sleep / Waking up — trigger when your device comes out of Sleep mode (e.g. when your Wake-up alarm is switched off)</li></ol><p>We can use either or both of these notifications to send an <em>event</em> to Home Assistant, instead of maintaining a sensor. As long as you use the Sleep Schedule for your alarm, it will always be in sync.</p><p>You can then build some <a href="https://www.home-assistant.io/docs/automation/">automations</a> in Home Assistant to respond to this event and perform some action. In my case, I have some Somfy blinds and a Phillips Hue light strip so my automation in Home Assistant opens the blinds a little bit and turns on the light strip (if it’s dark outside) when the alarm goes off.</p><h3>So what does the iOS Shortcut look like?</h3><p>You can <a href="https://www.icloud.com/shortcuts/719f152431b547d4a4243538ecd2f616">download it here</a>! It is setup with Import Questions so it should be very easy to import and get going.</p><p>To import it you will need:</p><ol><li>The name of your home WiFi network. Why? I don’t want the event to fire if I’m not at home; being on the home WiFi network is a reasonable proxy for being at home. Note: it would normally be impossible for the event to be successfully sent to Home Assistant if I’m not on the home WiFi network… but if I VPN’d to my home network for some reason whilst away in a different timezone for work… just not worth the risk of an upset partner 😉.</li><li>The URL of your Home Assistant instance, which is defaulted to <a href="http://homeassistant.local:8123/"><em>http://homeassistant.local:8123</em></a></li><li>A <em>Long-Lived Access Token</em> to authenticate with Home Assistant. You can obtain this as described in the <a href="https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token">documentation</a>.</li></ol><h4>Validating the shortcut</h4><p>To validate the shortcut you can run it and check the response from Home Assistant as follows:</p><ol><li>Click the 3 dots for the menu for the shortcut</li><li>Hit the play icon at the bottom right</li><li>It will prompt for input, just enter test</li><li>Scroll down to the bottom and check the output is showing as {“message”:”Event sleep_event fired.”}.</li><li>If you don’t see this, refer to the <em>Troubleshooting</em> section below.</li></ol><h4>Triggering the shortcut using Sleep Schedule</h4><p>There are two automations that you might want to use on your phone to trigger the shortcut:</p><ol><li>Your Wake-Up alarm goes off. This does what it says on the tin.</li><li>Sleep status goes in to ‘Waking Up’. This is when the Wake-Up alarm is Stopped, or if no alarm is set then this will be according to how the Sleep Schedule has been set up.</li></ol><p>I send both to Home Assistant as different events, but I have configured Home Assistant to only respond to the first one as that suits my needs.</p><p><strong>To configure the automation to fire on your Wake-Up alarm:</strong></p><ol><li>Open up the Shortcuts app</li><li>Select <em>Automation</em></li><li>Hit the + icon to create a new automation</li><li>Select <em>Alarm</em></li><li>Select When = Goes Off; Alarms = Wake-Up; Run Immediately</li><li>Select <em>Next. </em>That’s the trigger set up, now it’s to configure firing the shortcut.</li><li>Search for <em>Text</em>. If you’re only seeing a limited list of actions, then hit the <em>New Blank Automation</em> button. You should then see the full list of actions and be able to search for <em>Text</em>.</li><li>Enter <em>wake_up_alarm</em> as the text. This will be the name of the sleep event sent to Home Assistant and will be passed to the shortcut.</li><li>Then search for <em>Run Shortcut</em> and select it.</li><li>Tap <em>Shortcut</em> and select <em>Send Sleep Event To Home Assistant.</em></li><li>Select <em>Done.</em></li></ol><p>That’s it! You now have an automation set up to trigger when your Wake-Up alarm goes off and fire the <em>Send Sleep Event To Home Assistant</em> shortcut which sends a <em>sleep_event</em> to Home Assistant with <em>type=wake_up_alarm.</em></p><p><strong>To configure the automation to fire when waking up:</strong></p><ol><li>The process is very similar, it’s just that the trigger is different. <em>Waking up</em> is after the Wake-Up alarm is turned off or if there’s no alarm then according to the Sleep schedule.</li><li>Select <em>Automation.</em></li><li>Hit the + icon to create a new automation</li><li>Select <em>Sleep</em></li><li>Select When = Waking Up; Run Immediately</li><li>Select <em>Next.</em></li><li>Configure the shortcut as above in steps 7–11, but for the <em>Text</em> in step 8 set this as <em>waking_up.</em></li></ol><p>You now have an automation set up to trigger when iOS enters the <em>Waking up </em>state, i.e. when it comes out of sleep mode.</p><h4>Configuring Home Assistant to respond to the event</h4><p>Getting the events in to Home Assistant is half the battle; the next step is using it in Home Assistant.</p><p>In Home Assistant, head to <em>Settings / Automation &amp; Scenes. </em>Or follow the link below if you have the <em>my </em>integration enabled (you probably do).</p><p><a href="https://my.home-assistant.io/redirect/automations/">Link to Automations - My Home Assistant</a></p><ol><li>Select <em>Create Automation</em> at the bottom right of the screen.</li><li>Select <em>Create New Automation.</em></li><li>Under <em>When</em> select <em>Add Trigger</em> and select <em>Manual event.</em></li><li>Fill in the <em>Manual event </em>information: <em>Event type = sleep_event; Event data = </em>type: wake_up_alarm (or use type: waking_up if you prefer)</li><li>Under <em>And if (optional)</em> add any optional conditions you might want. I have a <em>Time </em>condition to check it’s a weekday and for the Phillips Hue lightstrip automation a <em>Sun </em>condition to check it’s dark outside (i.e. it’s 30 mins before sunrise; roughly dawn) as turning on the light when it’s already light outside is just annoying.</li><li>Under <em>Then do</em> is where you configure the action to be taken. Select <em>Add Action. </em>You are likely to want to do something with a Device so select <em>Device action; </em>but the limit is your imagination!</li></ol><p>If you want to perform multiple actions or automations, you can decide whether to add them all as actions on the same Automation, or create independent automations for them, following the same steps as above. I prefer to have separate ones as it means I can test them and enable/disable them independently and have different <em>And if</em> conditions. (Example — always open the blinds, but only turn on the Phillips Hue lightstrip when it’s dark outside).</p><p>Your automation might look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tYPwdHn6sL9OVkmH9ZgdBQ.png" /><figcaption>Example automation for turning on the Hue strip when the wake up alarm goes off and it’s dark outside</figcaption></figure><p>I hope you’ve found this guide useful, and happy automating! Below is some troubleshooting and more in-depth explanations.</p><h3>Troubleshooting</h3><p>There’s a number of things that could go wrong getting this all setup and working. This is unlikely to be exhaustive, but is hopefully useful.</p><p>Some familiarity with command-line tools like curl is assumed.</p><ol><li>Check that your Home Assistant has the REST API running. It almost certainly does unless you’re running it with no UI/dashboard. You can add the http integration to enable the REST API — refer to <a href="https://www.home-assistant.io/integrations/http/#apis">https://www.home-assistant.io/integrations/http/#apis</a> or <a href="https://www.home-assistant.io/integrations/api/">https://www.home-assistant.io/integrations/api/</a>. Execute a curl to validate you get a response TODO: curl statement with response</li><li>Validate that the bearer token you obtained from Home Assistant is working by making a request to the API. Execute the following curl request: curl -X GET -H &quot;Authorization: Bearer REPLACE_WITH_YOUR_TOKEN&quot; REPLACE_WITH_YOUR_HA_URL/api for example curl -X GET -H &quot;Authorization: Bearer blah-blah-blah&quot; http://homeassistant.local:8123/api . You should get back a JSON response like this: {“message”:”API running.”} . If you don’t either your bearer token is wrong or your Home Assistant URL is wrong. You’ll have to debug the error you got to figure it out.</li><li>Test sending the sleep event to Home Assistant using curl. curl -X POST -H &quot;Authorization: Bearer REPLACE_WITH_YOUR_TOKEN&quot; -H &quot;Content-Type: application/json&quot; --data &#39;{&quot;type&quot;:&quot;test&quot;}&#39; REPLACE_WITH_YOUR_HA_URL/api/events/sleep_event . You should get back {“message”:”Event sleep_event fired.”} . You should be able to see this event in your Home Assistant logs (TODO: confirm how to do this). To send a wake_up_alarm event as would be done from the iOS Shortcut automation when your Wake Up Alarm goes off, use curl -X POST -H &quot;Authorization: Bearer REPLACE_WITH_YOUR_TOKEN&quot; -H &quot;Content-Type: application/json&quot; --data &#39;{&quot;type&quot;:&quot;wake_up_alarm&quot;}&#39; REPLACE_WITH_YOUR_HA_URL/api/events/sleep_event . Any automation you have built off this event should then fire in Home Assistant, subject to any conditions you have built on the event.</li><li>The sleep events are being received by Home Assistant correctly, but your automation in Home Assistant is not working. First do the above steps to validate that the sleep events are being received correctly. Recommendation would then be to: execute the automation manually in Home Assistant; view the Traces for the automation in Home Assistant; disable any <em>And If</em> conditions on the automation in Home Assistant to make it as simple as possible and then start building the complexity back up again.</li><li>To execute the iOS Shortcut manually, just click it and enter the type of sleep event you want to test in the dialog, e.g. wake_up_alarm .</li><li>To debug the iOS Shortcut itself, the best approach is to add a <em>Show Alert</em> step in the shortcut and add some debug logging to the alert text. There is no better way unfortunately.</li></ol><h3>Deep-diving in to the shortcut</h3><p>For the curious, let’s go step-by-step in to what the shortcut is doing.</p><ol><li><em>Receive Text input from Nowhere</em>: this allows the shortcut to receive some input text, which will be the type of the sleep event sent by the iOS Automation; for example in response to the Wake Up Alarm going off. If there is no input text, then prompt for it. This is helpful for debugging so you can manually execute the shortcut and add the event type, e.g. wake_up_alarm .</li><li><em>Get Wi-Fi network’s Network Name</em>: obtain the current Wi-Fi network name and check it matches your home network name. This is a proxy for <em>are you at home?</em> in case your Home Assistant instance is accessible from your current location but you’re not at home. You probably don’t want your house to act like you’re at home when you’re not. An alternative would be to build <em>And If</em> conditions in to your automation to validate that Home Assistance thinks you are home if you have proximity/location working.</li><li><em>Set variable ha_url to text</em>: set the variable ha_url to your Home Assistant URL, as you entered in the Import Questions. This variable will be used later.</li><li><em>Set variable ha_token to text</em>: set the variable ha_token to your Bearer token, as you entered in the Import Questions.</li><li><em>Combine Bearer ha_token with Spaces</em>: combines the text Bearer with the ha_token variable using spaces. This is building the Authorization header value needed for HTTP requests to Home Assistant.</li><li><em>Combine ha_url url_path with Custom Text</em>: does a string concatenation of the ha_url variable with the url_path which is /api/events/sleep_event . The Text in the <em>Custom Text</em> is left blank so that they are just concatenated together (technically combined together using an empty string)</li><li><em>Get contents of ha_full_url</em>: makes the HTTP POST request to Home Assistant to send the sleep event. A little weird as it’s a <em>Get Contents of URL</em> action, but this action can perform HTTP POST as well as GET. This is the equivalent of the curl requests in the <em>Troubleshooting</em> section. The Method is set to POST , the Authorization header is set to the bearer token that was built in step 5, the Content-Type is set to application/json and the Request Body is set to JSON with an attribute of type set to the input of the shortcut in step 1, e.g. wake_up_alarm .</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eaf33d683c66" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>