API: Check when sync is "finished"? LocalIndexUpdated false positive?

I have a shell script using the events API and am struggling to check whether sync is “finished” more prior to system shutdown and also on system startup. Technically a sync is never finished because it’s syncing continuously, but for the purposes above, the best definition probably means that “within say a minute of timeout, at least a file was synced (indicating at least any remaining files should be synced at that moment as well) and then no more files are synced”.

To be clear, LocalIndexUpdated/RemoteIndexUpdated is emitted when a file is synced (i.e. changed)? Brief test shows that this seems to be working as expected except this: a LocalIndexUpdated emitted for 2 particular directories (one a parent of the other) that were never touched (including any of its files) and I’m not sure why. To confirm, the GUI lists “recent changes” and it labeled these directories as “modified dirs” while the rest of the recently changed files listed do not involve this path. Its modified time according to ls is older, i.e. it does not reflect what is reported by Syncthing.

Any ideas? Is simply checking for LocalIndexUpdated/RemoteIndexUpdated within a timeout period the best way to check for syncing completion? I was also considering StateChanged but its .data.from and .data.to=idle but .data.from seems to have more states besides what’s mentioned in the docs, e.g. sync-waiting, sync-waiting, and sync-preparing so doesn’t be so straightforward.

This is indeed surprisingly tricky. I would suggest looking at state changes and folder summary events. Folders being idle (no “syncing” state within recent memory, perhaps) and folder summary saying 100% completion is probably as good as it gets right now.

It’s trickier if you also want to worry about peers having synced all changes that might have been made locally.

I also noticed that this is tricky when developing Syncthing Tray. There I also ended up reading folder summary events like @calmh has already mentioned. For remote devices one could query db/completion on RemoteIndexUpdated-events (if my memory serves me well).

Note that syncthingctl might already do what you want, see Shutdown computer when sync is done - #3 by Martchus. It only covers the local completion, though.