Can I debug why a script was run?

I have a recurring weird behaviour where I trigger a script, and it gets run again 15-30 minutes later. I trigger it via the apple watch app, then I leave my phone behind and go running, and when I get back the script was run twice. I’ve checked the trace, but I can’t see any information in the trace on what actually triggered the script. Can I check that in the logs somewhere? Or is there an option to enable more detailed logging and then review it after it happens again?

1 Like

Not the logs. The logbook, it should have some context of what ran the script.

@tom_l Thanks. I’m looking at the logbook now, and I started 2 scripts, one from my watch, one from my phone. For both I see “started by action Scripts:” then the script name. So it lets me know it was from a script run rather than an automation maybe?

Is there a way to get more detailed info? Like which device ran the script, which user was logged in to that device, what the IP address was, and so on?

What you can do is add additional fields/parameters to the script and populate these from the different trigger points. So for example add a field called “source”, populate it with “my watch” when run from the watch, etc. This data will then be available in the trace.

Typically within the script/automation I also use the action system.log_write to create an entry in home-assistant.log and/or set an input_text which I then display in a dashboard. As these both provide an easy to access “log” of what the automation is doing.

@PeteRage That’s a very interesting idea, thanks for sharing. How do you set the source parameter? I tried adding a field to an example script, but I couldn’t find a way to set it on the watch.

I don’t see a way to do that either. I suppose you could create a script specific for the watch that then calls the script that has the source parameter. It’s also possible that the context object has the device id of the watch in it, and you could extract that in the script.

That’s an interesting idea. It would let me distinguish runs that I initiated via different methods.

I’ve looked into changed variables section of the traces, and I see some context IDs, but I’ve no idea if I can look those up somehow or figure out what they’re linked to.

It does not happen too frequently but since this issue is affecting my door lock it is pretty annoying to say the least. Luckily I use that script only when returning home and not when leaving so I should be at home when the door get opened unintentionaly.

I just experienced it now again.

  • I called the script from my watch at time T
    • Companion app on my watch visually confirmed the action (wheel stopped spinning and icon returned to normal
    • Lock did not open
  • I called the script from my watch again at time T+00:05
    • Lock did open almost immediately

Now looking at the script traces I have three logs

To me it seems like the issue is on the companion app delaying to call the script and is related to the companion app on my watch/phone reconnecting to my WiFi router at the time.

I also experience that it’s similar. I added a 15 minute wait on the end on my script and still I get multiple executions of the script. Sometimes they’re delayed by up to an hour.

I am often further away from my phone with the watch when I experience this. So I assumed it was linked to an unreliable connection between the watch and phone, but it sounds like your experience suggests it’s different.

hi everyone,
i am investigating the exact same problem (when i trigger a script from my apple watch companion app, the same script gets triggered a second time after some minutes).
i was wondering if you were able to find the root cause of the problem…?

Does this happen when your phone and watch are directly connected, or are you connected over WiFi? I only experience it when I’m connected over WiFi, and I only see this with scripts not with other actions.

Welcome to the club. This is a known issue in the companion app. Check out this GitHub issue tracker. No solution yet but, know it exists. For me, it happens in CarPlay, the companion app and from my watch.

https://github.com/home-assistant/iOS/issues/3690#issuecomment-3198769107

That seems like a slightly different issue to me, where the script is run without any input. While in this case the script is run multiple times after being triggered once.

If other people also have the same problem I would create a GitHub issue to track it.

Both occur. For me, they rerun multiple times after being run 1x. They also will run themselves days later without anyone running them themselves.

You issue could be different, but the main issue is the companion app running scripts more then requested.

This just happened to me again. This time, the lock was unlocked and the door was open 15 minutes after I left home. Pretty frustrating.

I reported this issue a while ago, but unfortunately, I haven’t received a response. Please upvote if you’re experiencing the same problem.