Activate a switch/Relay at a specific time

Hi Guys,

i need again your help. Im trying to figure out how to activate a Relay daily at a specific time then have it running X seconds and then turn off.

Relais3 is the item in question here.
Now i need the starting condition (here Time) but i dont know if this works or if a cronjob has to be done here.

My code so far is:

rule “Relais off”
import Time

when

    Time(16:31)

     then
        Relais3.sendCommand(ON)
        createTimer(now.plusMillis(10000), [|
        Relais3.sendCommand(OFF)
        ])

end

Hope you can help me out here.

You should use a cronjob to switch it on, and another rule with a cronjob to switch it off, or you use a timer. Maybe also the expire binding could help you.
Read the docs and good luck!

@Soron Rule to run every day at 16:31

rule “Relais off”
when
    Time cron "0 31 16 ? * *"
then
        Relais3.sendCommand(ON)
        createTimer(now.plusMillis(10000), [|
        Relais3.sendCommand(OFF)
        ])

end

Please

See:

Read the docs before asking questions, please!!

After reading the doc’s if your still not sure about the rule with a timer here is a working example. You needed to create the var Timer like below.

var Timer startTimer = null
rule “Relais off”
when
    Time cron "0 31 16 ? * *"
then
        Relais3.sendCommand(ON)
        startTimer = createTimer(now.plusMillis(10000), [|
        Relais3.sendCommand(OFF)
        startTimer = null
        ])

end

Also, here’s a link that will help with writing rules. Lots of examples and explanations.:+1:

Thanks H102 so far that works.
Now to the real problems begin.

1.) I need to make the time variable so I can set it myself in openhabpanel
2.) and also adjust the timer how long it will stay on.

Any suggestion will be greatly appreciated.

Search the forum to see if this has been done before. If you can’t find what your looking for then start a new topic.