Cron jobs, mqtt and network outage

Hi everybody,

I’m still pretty new to openhab, but so far thanks to a lot of tutorials and examples I’ve been able to get everything working like I wanted and the setup has been running fine for over two months now. But yesterday I stumbled upon an issue where it behaved unexpectedly.

The setup:

  • raspberry pi running openhab and the mqtt server
  • multiple esp8266 reporting temperatures
  • esp8266 controlling a 4x relay (sonoff 4ch)

Everything is in the same wifi (Fritz.box). The ESPs get triggered and read via mqtt

What behaved unexpected is a simple cron timer:

  • switch a certain port of the relay to on in the morning and to off in the evening

The situation that I found yesterday evening after the light didn’t go out:
raspberry has disappeared from the WLAN
ESPs reading temperatures disappeared from the WLAN
ESP with the relay is still connected or reconnected

Nothing found in the fritz.box … not sure what happened? Anyway, that’s not the point of the post. So I got some connection problems. What did I do? Reboot the raspberry, power cycle the ESPs.

Everything connected back to the WLAN.

What I expected now:
The light should be switched off after the raspberry reboot.

But this didn’t happen. However, in openhab, the switch object was set to “off”

So I read about the cron job -> it’s apparently a one time thing. It gets triggered one time, it’s not like the cron job is checking up on the object later, if it’s still off.

My question here: Is there any simple way to set something to “off” or “on” during a defined period of time, so that the script checks periodically if it is really switched off? I thought about doing an every 5 minute cron job and check for whether it’s night or day in the executing part of the script. but that would mean sending a trigger every 5 minutes. There sure is a more elegant way?

The other question: … apparently openhab executed the job, that’s why the “local” switch was set to off. So mqtt should have received the off command as well, but couldn’t send it to the client due to not having network.
Why didn’t this get cached locally and sent to the ESP relay once network was back on?
Is this a configuration setting somewhere? Like thtat the mqtt message should be resent? I wouldn’t want it on the temp readings, but for the commands I’d like if they aren’t “fire and forget”.

Do you have persistence with restoreOnStartup?
If yes, the switch would have been restored to OFF from NULL after a reboot BUT that’s only a state update, NOT a command. Therefore not sent through the binding.
You could add a rule:

rule "restore mqtt relays"
when
    MQTTItem changed
then
    if (previousState == NULL) {
        MQTTItem.sendCommand(MQTTItem.state.toString)
    }
end

Also if you are using sonoffs and tasmota, you can set-up tasmota to restore the relay state after a reboot

1 Like

To answer your second question, I know that Mosquitto (a mqtt broker or ‘server’) is able to cache the latest messfor each topic and send it whenever a client (re-)connects. I can’t recall the exact name of the setting right now, sorry.

1 Like

I think retain is what you are thinking about. It can be set in mqtt.conf: <broker>.retain=true. it will make the broker keep the last message on every topic and send to all newly connected clients. Note that this is a global setting, so will be set for all messages sent by openhab.

2 Likes

Thanks guys, that’s all really helpful! I think I have at least restoreOnStartup already enabled - also the setting in tasmota, I’ll check my installation on the weekend for this and the rest.