I’m trying to have “wake-up alarm being turned off” as a trigger that makes a wake-up script in HA run. I’ve been trying all kinds of things and despite the script running about 99% of the time, my iPhone often gives an error about timing out or the kCFErrorDomainCFNet error.
I’ve created an automation of “when my wake-up alarm is turned off”
This automation triggers the shortcut ‘Run Wekker uit’ (Run alarm off) which uses ‘Get contents from url’, using the URL ‘http://homeassistant.local:8123/api/services/script/routine_wekker_uit’. The authorization is ‘Bearer [long-lived access token]’, content-type is application/json. No further content such as entity_id because the URL already accesses the script directly (though I DID try that, but it gave the same result).
This then fires the script correctly (meaning, it runs all the stuff in the HA script)
However in shortcuts it can be seen that the shortcut keeps running, seeing as the ‘get contents from url’ command stays green for a long time. Eventually it times out.
I’ve tried a lot of things: My first foray into the connection was by this script: https://community.home-assistant.io/t/sync-ios-17-sleep-alarm-to-ha/622297/68
I then wanted something more flexible/adaptive so started with an HA automation which was triggered within the shortcut with ‘trigger automation’. This gave an ‘IntentsErrorDomain’ error from shortcuts and the error from the HA companion app that rendering the action took too long.
Then I switched to a script, after reading that that might be a better fit. Using ‘Call service’ with script.turn_on, combined with the dictionary input of entity_id with script.routine_wekker_uit. Gave the same errors.
Finally after reading about it online, decided to use the ‘Get contents from URL’ command as it fires it directly. Which produced the results seen above.
What am I doing wrong? How can I prevent the time-out of the request? Or have HA give a “yes it’s done” command back to shortcut or something? It seems like it’s waiting for a response (which it never has to do, it just has to fire the command) and erroring on that.
The script runs fine by itself, so it seems a shortcut/ HA-companion app-specific issue.
Yes, it waits a few seconds for the script to finish and times out if it doesn’t. The solution is to run the script via the script.turn_on service/action (i.e.: “fire and forget”)
change the url so that it ends with .../services/script/turn_on
add a request body (JSON) with the key entity_id and the value script.routine_wekker_uit
You can achieve the same thing with a “normal” application, but again you’ll need to use script.turn_on instead of running the script directly. The action is called “call service” or something similar and looks like this :
Sorry, I somehow missed that sentence All I can say is that the timeout wasn’t caused by waiting for the script to finish (when you called script.turn_on). So I hope bypassing the application will help.
Besides, the shortcut is an intermediate step - perhaps calling it is causing these issues. Try invoking the HA script directly from the iPhone automation (I think that’s what bazfum meant).
No problem! It IS a big OP haha. About the change in URL, I’ll know tomorrow morning. Despite my HA automation itself working through the different options discussed here, I’m very annoyed by the errors since they’ve been popping up a long while now, they shouldn’t have to pop up imo. I just want them gone!
About bazsfum’s comment: I don’t see any options to call the HA Companion app directly from the Personal Automation tab? The only option I have is to create a shortcut where I CAN add a command through the HA Companion app (or the get contents from url one and bypass the app) and have a personal automation call that shortcut based on a trigger (alarm off, in this case). There’s a bunch of other app options available, but no HA.
Yes, that’s what I meant. When you select “Create Shortcut” within a personal automation, it “unlocks” more actions for that automation and no (named) shortcut is created. I don’t know if this will make a difference, but it’s worth a try.
Just to test, my older version (prior to get contents from url) does work. Both in and out of a personal automation. But this gives the errors I mentioned in the OP too.
By the way, the errors can’t always be replicated.
My alarm snooze automation works after testing it a few times this morning. Both when manually activated and through an alarm. But the alarm off automation (exactly the same, except for the script entity_id) and being called in the personal automation with alarm off instead of snooze -though manually it times out too. It has the correct entity_id too, I’ve checked over 10 times now.
Ugh I really don’t know why this isn’t working! I only need one long lived access token right, just for the shortcut app and not for each shortcut individually?
Edit: just to be clear, I have two scripts: script.routine_wekker_uit for when my alarm is turned off and script.routine_wekker_af for when it is snoozed. So I have two essentially separate shortcuts / automations that I’m trying to get working.
The alarm off script:
alias: Routine || Wekker uit - Script
mode: single
sequence:
- choose:
- conditions:
- condition: time
before: "11:30:00"
- condition: template
value_template: >
{% set last = states('input_datetime.wekker_lisa_laatste_snooze')
%} {{ last in ['unknown','unavailable','']
or last[:10] != now().strftime('%Y-%m-%d') }}
sequence:
- target:
entity_id: input_datetime.wekker_lisa_laatste_snooze
data:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
action: input_datetime.set_datetime
- action: script.turn_off
metadata: {}
target:
entity_id: script.sunrise_simulator
data: {}
- action: scene.turn_on
metadata: {}
target:
entity_id: scene.lisa_opkomen
data:
transition: 30
- action: notify.mobile_app_iphone_lisa
metadata: {}
data:
message: De wekker is uitgezet.
enabled: false
- action: media_player.volume_set
metadata: {}
target:
entity_id: media_player.googlehome8249
data:
volume_level: 0.3
- action: google_assistant_sdk.send_text_command
metadata: {}
data:
media_player: media_player.googlehome8249
command: Wat voor weer is het vandaag in PLACEHOLDER?
- delay: "00:10:00"
- type: turn_off
device_id: 8d2bdca4191aeea74c17047b5bdb00f8
entity_id: 38757b82dd75f183aaf0e21311dfdfd6
domain: light
default:
- action: notify.mobile_app_iphone_lisa
data:
title: Wekker UIT script niet gestart
message: >
Niet voldaan: Tijd: {{ now().strftime('%H:%M') }} Laatste snooze: {{
states('input_datetime.wekker_lisa_laatste_snooze') }}
And the snooze script:
alias: Routine || Wekker gaat af - Script
description: Start ochtendroutine max. 1x per dag
mode: single
sequence:
- choose:
- conditions:
- condition: template
value_template: >
{% set last = states('input_datetime.wekker_lisa_laatste_run') %}
{{ last in ['unknown','unavailable','']
or as_timestamp(last) < as_timestamp(now().replace(hour=0, minute=0, second=0)) }}
- condition: time
before: "11:30:00"
sequence:
- action: input_datetime.set_datetime
target:
entity_id: input_datetime.wekker_lisa_laatste_run
data:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
- action: script.sunrise_simulator
data:
duration: 30
target:
- light.lisa
- light.nachtlamp_lisa
max_brightness: 100
default:
- action: notify.mobile_app_iphone_lisa
data:
title: Ochtendroutine NIET gestart
message: >
Niet voldaan: Tijd: {{ now().strftime('%H:%M') }} Laatste run: {{
states('input_datetime.wekker_lisa_laatste_run') }}
I understand. I admit it’s too complicated for my limited knowledge. I ran the following test:
an HA script that waits 15 seconds and then displays a notification in the sidebar
an iPhone automation, enabled after the alarm goes off, that invokes this script directly (no shortcut) via script.turn_on and the companion app
it works correctly on both unlocked and locked iPhone screens
The other difference between our configurations is that I’m connecting via https. But I have no idea if that matters, and adding a certificate to HA isn’t trivial.
I just tried the alarm off shortcut manually again just now, it works without timing out… only difference is the time between. Pffff.
Hmm I’ll try that again, we’ve got an external link that is HTTPS but that gave the error that there was no network connection this morning (in the shortcut). I’ll go tinker later today.
Edit: just tested it quickly manually (so not in the personal automation yet). External link with HTTPS works for the snooze shortcut works. The alarm off shortcut times out…
The disabling delay doesn’t make a difference (just wanted to try). However, after editing the automation in HA, manually triggering the alarm off shortcut does make it run. Reverting to the old version in HA (so no edits) doesn’t break it again. So it’s REALLY inconsistent in when it works and when not.
Yes, well, maybe it’s one of those things that just doesn’t work properly, and I’d experience the same if I used it daily. A well-known example of an unreliable action is the “render template,” which times out ~60% of the time (and using a web call actually solves that case).
On a different note: do you experience network issues? Do you use other shortcuts, e.g. as voice commands, and if so, do you experience any issues with them?
Never any network issues! Our entire house is basically smart and we’ve put a lot of work in a stable network in-house. So while I don’t know for sure if that might me an issue, there’s no other use cases within or outside of HA where we notice network issues.
The render template with web call, would that be something to consider here? I’m okay with changing up the HA side if that helps. I’m not familiar with that option and what it does
No, I don’t think so. This action simply evaluates a template like “{{ states(‘sensor.blah_blah’) }}” and cannot replace a script.
I’ve generally never had any issues running HA scripts from the iPhone, aside from a timeout issue, which script.turn_on completely resolves for me. So what you’re experiencing may be very specific.
Ah, there’s another way to execute an HA automation/script from iPhone automation: using a custom HA event. I don’t expect it to work any better but if you want to try it:
Create HA automation that’s triggered by an event (you can use any event name you like; I’m using iphone_alarm_off and iphone_alarm_snooze).
mode: parallel
max: 5
triggers:
- trigger: event
event_type: iphone_alarm_off
id: alarm_off
- trigger: event
event_type: iphone_alarm_snooze
id: alarm_snooze
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id:
- alarm_off
sequence: [] # your appropriate script/actions go here
- conditions:
- condition: trigger
id:
- alarm_snooze
sequence: [] # your appropriate script/actions go here
the action name you should now be using in the iphone automation/shortcut is “run event” (or similar), and just put the event name there (iphone_alarm_off/iphone_alarm_snooze)
Note: the automation mode is set to parallel, just to rule out the potential issue of simultaneous execution.
It ran without issues this morning with this event based automation. I’ll wait until tomorrow to see if it runs again without issue, but this may be the solution!
I’m glad it worked! I’d forgotten I was using this method (to open and close the gate after 30 seconds) before I learned about script.turn_on. So I think since the event method doesn’t raise an error, the app must be handling it in some other way.