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.