Jython script loads and trigger twice sometimes

Hi there!

I’m using Steve Bates lovely Jython scripting repository for openHAB 2.x

I have noticed when using the StartupTrigger that 50% of the times the script executes twice when I save the py-file. When that happens and it runs for the first time the code below crashes. (See log) When it’s only running a single time, it works perfect.

from org.eclipse.smarthome.model.persistence.extensions import PersistenceExtensions
from openhab.rules import rule, addRule
from openhab.triggers import StartupTrigger

@rule
class MyTestRule(object):
    def getEventTriggers(self):
        return [
            StartupTrigger()
        ]
    def execute(self, modules, inputs):
        self.log.info('Running')
        self.log.info(PersistenceExtensions.lastUpdate(ir.getItem('AmbientLight_Backyard')).toDateTime())

addRule(MyTestRule())

Log:

2018-02-06 13:45:48.298 [INFO ] [ort.shared.ScriptedAutomationManager] - removeAll added handlers
2018-02-06 13:45:48.300 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'testa.py'
2018-02-06 13:45:48.309 [INFO ] [ort.shared.ScriptedAutomationManager] - removeAll added handlers
2018-02-06 13:45:48.309 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'testa.py'
2018-02-06 13:45:48.310 [INFO ] [sboh2j.MyTestRule                   ] - Running
2018-02-06 13:45:48.311 [ERROR] [.automation.core.internal.RuleEngine] - Failed to execute rule 'MyTestRule-9affd619-b564-4533-ac38-b0c2fe88474f': Fail to execute action: 1
2018-02-06 13:45:48.317 [INFO ] [sboh2j.MyTestRule                   ] - Running
2018-02-06 13:45:48.320 [INFO ] [sboh2j.MyTestRule                   ] - 2018-02-06T13:45:25.124+01:00

I can’t figure out how to solve this. Any ideas someone?

Just for context, are you editing the files locally or using a network file share (samba, sshfs, nfs, etc.)?

Thanks @steve1
Scripts reside on my Ubuntu server and the folders are mounted on my Ubuntu Desktop PC over SSH using sshfs.

The symptoms sound like an issue with OH file changes being detected before the file is completely written. I’ve seen this issue in the past with other OH configuration files (I edit locally now). The first read breaks because of a partial file write and the second read succeeds because the file has been completely written.

Can you reproduce the issue by “touching” the file on the Ubuntu system to just update the timestamp? If not, then I think it’s related to the remote file writes.

I believe you’re right. A touch issued on local disk won’t cause loading to occur twice. I’ll have to think about how solve this. I’m using VSC for editing scripts.

Thanks a lot for your kind help @steve1

:star_struck: