Light on between time

Hi guys, i need little help. I don’t know how to write rule.

I want turn on light, filter etc in fish tank in specific time.
I have item akva_ch1 and i need turn on at 7:00 and turn off 20:00. Time between must be ON even after restarting the system. Can you help me ?.


You can use cron rules, and persistence to restore item state on reboot.

What about something like that:

rule "fish tank"
    Time cron "0 0/1 * * * ?"
    if (now.getHourOfDay() >= 7 && now.getHourOfDay() < 20) {
        if (light.state != ON) sendCommand(light, ON)
        if (filter.state != ON) sendCommand(filter, ON)
    else {
        if (light.state != OFF) sendCommand(light, OFF)
        if (filter.state != OFF) sendCommand(filter, OFF)

Perfect, spy0r solution works perfect.

Stephan’s solution is a good one but given that the life in your tank potentially relies on this to work correctly some questions you should answer:

  • Does the light Item ALWAYS match the actual state of the physical light? For example, if you manually turn the light on and off does light reflect the change?

  • What happens if you unplug and plug the light back in or, if it is hard wired, flip the circuit breaker? The same for the filter?

  • What happens if you unplug the light (or remove power from the circuit breaker) and then restart openHAB?

Fully understanding all the failure cases is important because with the simple rule provided, if at any point the light and filter Items state do not match the actual state of the devices then OH will potentially think they are ON when they are not ON and fail to turn them ON.

For most purposes I wouldn’t care. And even for something like plants I wouldn’t care. But I do know that it doesn’t take long to wreck a fish tank. So you need to make this Rule robust which means understnading how your devices behave in failure cases.

rlkoshak thanks for you question.

Stephan code is only template for my how to work with time. I will add additional variables. For example manual/automatic switch, LDR sensor for light and some protect.

Hardware part was modify old APC ups with sonoff 4CH module and espeasy.
If is power failure, I get notification sms from alarm.

And last thing, i have only basic freshwater aquarium. my fish need only water and sometimes food, :smiley: all others is only for my pleasure. I love graphs :slight_smile:

Sorry for my bad english language and thank you for your comment.