I am currently runing OH3.2. I need to have a script, running at sytem start, which collects some IP addresses. Even when set the trigger in the rule to system level 100 it looks like, that it does not work 100%. So tried to create a timer, to delay the execution for 30s after the trigger.
When this is part of a rule, it gives an error:
Script execution of rule with UID ‘Startup’ failed: :23:35 Expected an operand but found |
createTimer(now.plusSeconds(30), [ |
^ in at line number 23 at column number 35
The same code copied into a script runs perfectly. Any idea, what I am doing wrong?
No it’s not. You selected ECMAScript 5.1 when you created the Action. You either need to change that to Rules DSL or you need to change your code to be JSR223 JavaScript.
I finally found the little difference, after setting up the rule again and then comparing the code. “application/javascript” and not “application/vnd.openhab.dsl.rule”.
As I have never worked with ECMAScript before, I must have just not clicked exactly on the selection. As the editor looks the same, I have never noticed that. Thanks for your help.
one additional question: what is the appropriate time to wait after startup, until the script can run safely?
I have extended the time to 2 mins, however, after startup the items with _IP have still the value NULL. I then startet the startup script manually abt. 50 mins after startup, now the values are correct.
That is going to be totally based on your system. No one can give you a general answer to that. It depends on the hardware, other stuff that’s running at the same time, RAM, installed bindings, etc.
It depends on what you want to do in such a rule, and whether that rule depends on certain Things being online.
It might be the case that something is wrong (e.g. the MQTT broker is down, Zwave controller is unplugged, etc) and no amount of waiting will be enough.
However, if the system started rule doesn’t depend on anything external or even Items being in a certain state, it will work every time.
If your rule depends on a Thing’s status, consider triggering the rule when the Thing comes online instead of a System started role. Things generally come online after the rule engine has started.
You don’t necessarily need one rule out Thing. Again it depends on what your rule does and what it’s means when a Thing comes online for that rule.
The danger goes for the Item’s. It depends on what the rule does with/to the items and whether there’s any external dependencies involved. For example, if the rule expects the items to restoreOnStartup using InfluxDB but InfluxDB is down, the Items will not have been restored when the rule runs.
Again, there is no universal answer here. You have to know and understand what the dependencies are to answer that question.
What I want to achieve with the startup script is the following: I have integrated several Tasmota devices via MQTT. As these devices have an internal browser, sometime the ip address is important. Unfortunaley, OH sees the ip address only, when the Tasmota devices is bootet after OH. I have learned, that the “Status5” reads the IP address back, so the goal is, to get the address shortly after an OH reboot (which I do every night, after a system backup).