Can OH2 execute a command at startup, after its initialization?

I want OH2 to execute a command (mosquitto_pub.exe) at startup, after it is fully loaded and running. Adding the command to the openhab.bat will not do the right thing - the command will be executed to early, before OH is fully initialized. Is there a proper way to do it? Perhaps a special rule?


This kind of thing?

except you would use exec action in the delayed code.

Thanks, I will look into that.

For now I found my own workaround which seems to work ok: I added one line (starting with “start”) in openhab.bat, before the karaf.bat line:

@echo off

echo Launching the openHAB runtime...

set DIRNAME=%~dp0%
start /b C:\openHAB2\mqtt_start.bat >nul
"%DIRNAME%runtime\bin\karaf.bat" %*

My mqtt_start.bat looks as follows:

timeout /t 20 /nobreak
C:\mosquitto\mosquitto_pub.exe -h -t "openhab/start" -m "1"
C:\mosquitto\mosquitto_pub.exe -h -t "openhab/start" -m "0"

The /t argument of timeout specifies the delay in seconds for running my commands after starting openhab.

I am watching this with mosquitto_sub, and indeed 20 seconds after restarting the OpenHab Windows service I get the correct sequence of messages on openhab/start topic.

Now I can implement dynamic state update for my clients (smart switch etc.) when openhab restarts.