Repeat timer - on - off -

  • Platform information:
    • Hardware: computer
    • OS:unbuntu
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 2

Hello, I would like for one of my TP link hs105 to be on a repeat timer.

On for 3 minutes 30 seconds
Off for 1 minute 15 seconds

What do i need to know and to to arrive at this?

Thank you very much for any help :slight_smile:

You need to know the basics of how to write Rules. Then you can adapt Design Pattern: Looping Timers.

Thank you very much for comming back to me on this. I have worked hard to get to this point but i think i might have made a code that can work. If any of you can have a look at it, maybe you can spot somthing i might not yet kno about programming. However, i did seem to work the first fiew minutes i tested it. Thank you again :slight_smile:

var Timer timer = null

rule "demo rule LOOP1"

when
    Item Wallplug_greenhouse_Humidity received command
then
    if (receivedCommand == ON) {
       
            // first ON command, so create a timer to turn the light off again
            timer = createTimer(now.plusSeconds(30)) [|
                sendCommand(Wallplug_greenhouse_Humidity, OFF)
            ]
         
    } else if (receivedCommand == OFF) {
            // first off command, so create a timer to turn the light ON again
            timer = createTimer(now.plusSeconds(30)) [|
                sendCommand(Wallplug_greenhouse_Humidity, ON)
            ]
        
    }
end 
1 Like

Nice!!
Good going
Couple of points
Try to be consistent with you indents. It make code reading easier
Use the item.sendCommand(XX) syntax intead of sendCommand(item, command) because off: https://www.openhab.org/docs/configuration/rules-dsl.html#myitem-sendcommand-new-state-versus-sendcommand-myitem-new-state

You need to make sure that NO other rule or UI will send a command to this item or you’ll end up with interloping timers all over the place

var Timer timer = null

rule "demo rule LOOP1"
when
    Item Wallplug_greenhouse_Humidity received command
then
    if (receivedCommand == ON) {
        // first ON command, so create a timer to turn the light off again
        timer = createTimer(now.plusSeconds(30)) [|
            Wallplug_greenhouse_Humidity.sendCommand(OFF)
        ]
    } else if (receivedCommand == OFF) {
        // first off command, so create a timer to turn the light ON again
        timer = createTimer(now.plusSeconds(30)) [|
            Wallplug_greenhouse_Humidity.sendCommand(ON)
        ]
    }
end 
1 Like

Oh thank you, i took your script and put it on my system! works great! Read your link and kinda understood. Im probably still to new to all of this. What i did take out of it is to change the syntax. I pretty much just copy past some code i found, entered my own variables tweaked a little and made it work. However, i am not a programmer so i basically do not know what im doing! Haha

Wondering, should i be killing the timer at the end of the script? Would there be a benefit in having 2 diffeent scripts for each state (on/off)?

Again, very big thank you!

There is no end of the script so no need

No

1 Like

Wow, so im basically done!

Thank you for your help!

For anyone interested, i grow mushrooms. oster mushrooms. I already got some mechanical controlers on it but would love more options to play with. so im going to try and use openhab to control the flowering chamber :slight_smile:

This veers just a little off topic, but in Scripted Automation you can define a scriptUnloaded function that does get called when the file is unloaded. This is a great place to cancel any running Timers you may have.

There is no equivalent to this in Rules DSL and therefore, when you reload a .rules file that has a Timer active, that Timer will become orphaned and when it finally does expire you will see an error in the logs (it will have “Procedures$Procedure” in the long complicated message. You can ignore this error.

1 Like

Wonderring…

I have noticed that sometimes my rooter wifi stops and reboots. I have also noticed that sometimes this makes my script stop. Is there some way that i could use a second triger here maybe:

when
    Item Wallplug_greenhouse_Humidity received command

Thank you for any insight!

Sorry for not responding before @rlkoshak not really sure of hat you here saying and how it applied to my case… :slight_smile:

There is a way to cancel the running Timers if you use Scripted Automation instead of Rules DSL. If you use Rules DSL, you have to expect and accept that you will see errors in the logs when your .rules file reloads.

If all your Rules are stopping you have a lock or Thread::sleep somewhere that blocks a Rule from exiting when your router goes offline. You can only run five rules at the same time so when you have five Rules blocked, none of your Rules can run.