[SOLVED] Automatically turn off a Relais/GPIO after x seconds

Hello everyone.

I need a bit of help with a problem.
I have successfully connected a Relais Board in Openhabian2 and can switch the Relais on and off.
However one Relais should only give a short Impuls (switched on for 0,5 Seconds or less) in order to activate a device begind it.
The Relais are defined as follows within the “Relais.Items” File

Switch Relais1 "RELAIS1" { gpio="pin:5" }
Switch Relais2 "RELAIS2" { gpio="pin:6" }
Switch Relais3 "RELAIS3" { gpio="pin:13" }
Switch Relais4 "RELAIS4" { gpio="pin:19" }
Switch Relais5 "RELAIS5" { gpio="pin:26" }
Switch Relais6 "RELAIS6" { gpio="pin:12" }
Switch Relais7 "RELAIS7" { gpio="pin:20" }
Switch Relais8 "RELAIS8" { gpio="pin:21" }

The Rule is so far set as follow. (Test with 2 Seconds)

import time
var timeout = 2 // sec

Rule "RELAIS2 auto aus"

when
        item RELAIS2.state == "ON"
then
        createTimer(now.plusSeconds(timeout))
        [| RELAIS2.state == "OFF"]
end

I have tried before with the rule

when
Item Relais2 received update
then

if (Relais2.state == "ON")
createTimer(now.plusSeconds(timeout)) [|
    Relais2.postUpdate("OFF")
    sendCommand (Relais2.state == "OFF"

Im not sure if the naming is incorrect as iam totally new to this and the Naming and if the rulke is applied successfully to the Relais2 in my case.

Hope someone can help me out here.

Hey @Soron

let me recap this quickly: you have a device that should be triggered by a relais. It is triggered when the relais is turned on for a short interval (for example 0.5 seconds). Your problem right now is, that you only have commands to turn the relais on or off. Is this correct?

In case it is: Have you considered using a trigger channel to send a short impuls? I guess you can send the “on” - signal (maybe adress another channel for this. I don’t know your device nor your channels or items) when the trigger is pressed, and the “off” - signal, when the trigger is released. If I’m correct it should act like a virtual switch…

Your first rule could not work, because the when part is a state. It must be an event.
Your first rule could not work, because Relais2 and RELAIS2 are different Items. OpenHAB is case sensitive.

Second rule getting closer - it’s triggered by an event.
But if you want to send a command to a GPIO pin, then you need to send a command. A postUpdate does not send a command to the GPIO.

The sendCommand part you do have is mangled, unwanted ==, missing bracket etc.

Have another try, it’s nearly there.

I’d recommend to use the Expire binding on this ‘impulse’ item.

The expire binding minimum “pulse” is 1 second and the OP wants 500ms even though there is a 2s timer in the rule.

NO IMPORTS NEEDED

val relay2timeout = 500 // 500 ms
rule "RELAIS2 auto aus"
when
        Item RELAIS2 changed to ON
then
    createTimer(now.plusMillis(relay2timeout), | [
        RELAIS2.sendCommand(OFF)
    ])
end

Thank you all for the suggestions.
@TheTmirror
I have read about virtualk switches there but never found an example. I started this whole experiment 1 week ago as a total newbie.
I have now 8 Relais 2 Onewire Temperature sensors with a Graphana graph etc. So i made good progress and was happy.

But at this im stuck

The Expire binding is installed.

I tried now the Rule from vzorglub and still no success, the Relais stays on.
In the future i want to user othger relays to automativcally be activated in events so the actual timer in this case is not that important. (except if there are problems below 1 sec)

The problem here might be that there is Relais2 and RELAIS2 and im not sure which one had to be used as name in the rule.

The item name is relais2
The item label is "RELAIS2"

val relay2timeout = 500 // 500 ms
rule "RELAIS2 auto aus"
when
    Item Relais2 changed to ON
then
    createTimer(now.plusMillis(relay2timeout), | [
        Relais2.sendCommand(OFF)
    ])
end

Ok thanks, finally got it working.

Good, can you tick the solution, please?

Hi Vincent.
I think I am missing something.
I don’t want to high jack the topic?

My rule :

val relay2timeout = 500 // 500 ms
rule "RELAIS2 auto aus"
when
    Item Arm_away  changed to ON
then
    createTimer(now.plusMillis(relay2timeout), | ([
       Arm_away.sendCommand(OFF)
    ])
end

I tried to get the log. as I am not sure what is wrong. it keeps on saying expecting a ) or end ?
and then just ignores the rule
what am I doing wrong

There IS a “)” missing. You opened 4 brackets but closed only 3 of them…

Thank you. I totally missed that one.

Removed the bracket at beginning of timer lambda
Forced the relay2timeout type to int as the .plusMills() methad only accept int as argument (Just in case)

val int relay2timeout = 500 // 500 ms
rule "RELAIS2 auto aus"
when
    Item Arm_away  changed to ON
then
    createTimer(now.plusMillis(relay2timeout), | [
       Arm_away.sendCommand(OFF)
    ])
end

can I have multiple

val int relay2timeout = 500 // 500 ms

With Different timing attributes in them ?
This Rule Looks after 4 Relays that activates Contacts on a modified Alarm Remote.
and some of the contacts might need a shorter or longer press simulation.

Yes of course create 4 int constants
and in the rule create 4 timers