Timed GPIO Output

Hi, I an trying to write a program using a simple button switch in the UI to output to a GPIO and hold it on for 30 sec and then turn off. i trid a number of things, but i can;t get this switch to work.


these 2 pics are my code so far. Help me please. Top pic is the items file and the bottom is the sitemap

Please, don’t post code as picture but as text. There are code fences to make it more readable.

If you want to automatically switch off an item which was switched on, simply use the expire binding.

Thanks Udo Hartmann for the advise. I have been trying to get the expire binding working, but so far no luck. This code works for a simple switch that turns a GPIO on and off. I have put the
{expire=“45s,OFF” } at the end of switch one and it doesn’t work. In the brackets, nothing. i just want an on, 45 seconds and then off, then a second Gpio on, r45 seconds off. then reset. Am i better off with a rule?


Switch Switch1   "Button 1"      { gpio="pin:23 activelow:yes initialValue:high" }
Switch Switch2   "Button 2"     {  gpio="pin:23 activelow:yes initialValue:high" }

Expire binding needs to be installed. Furthermore, it is a version 1.x binding and you need to “enable legacy bindings” to be offered it in the list.

1 Like

i have already enabled the expire binding in the paper UI. I and i can confirm that the legacy binding is already on .

The format is like this

Switch Switch1 "Button 1" { gpio="pin:23 activelow:yes initialValue:high", expire="10s,command=OFF" }

Thanks mate, but it still doesn’t work. I made a small rule the times the relay for 5 seconds instead. Can you possibly give me some pointers tho on how to trigger relays sequentially?

So, Iwrote this short rule,. it works for a sinle relay on its own, but not as the are together. the logs say that there is a problem with the second “when”

your code goes here
```rule "Relay time"
when
      Item Switch1  received command "ON"
then
     createTimer(now.plusSeconds(5)) [|sendCommand(Switch1 ,OFF)]
when
         Item Switch1  received command "OFF"
then
        Item Switch2  [|sendCommand(Switch2 ,ON)]
then
        createTimer(now.plusSeconds(5)) [|sendCommand(Switch2 ,OFF)]

end

Finally after just trying different things, i got it.


rule "Relay time"
when
      Item Switch1  received command "ON"
then
     createTimer(now.plusSeconds(5)) [|sendCommand(Switch1 ,OFF)sendCommand(Switch2 ,ON)]

end

rule "relay"
when
         Item Switch2  received command "ON"

then
        createTimer(now.plusSeconds(5)) [|sendCommand(Switch2 ,OFF)]

end



Where have you seen rules like this? never, ever anywhere… :wink:

A rule i this:

rule "name" // keyword rule is start of rule named "name", where "name" has to be unique
when        // keyword for the triggers part 
            // Triggers
then        // keyword for the execute part
            // Execute part
end         // keyword for end of the rule

You must use every keyword exactly as written above (so once per rule)

when is not another if, and then is not part of if either.

A rule needs at least one trigger, this could be a change of a status, a received command or a received update. There are more triggers, like Time cron, Thing status, Channel events or system start. all of them can trigger a rule, alone or in combination, but each trigger will trigger, there is no such thing as an boolean and for rule triggers, as it’s extremely unlikely, that two triggers will ever happen to happen at the very same moment.

So, a correct rule to automatically switch off an item which was switched on would be

rule "Relay time"
when
    Item Switch1  received command ON
then
    createTimer(now.plusSeconds(5)) [|sendCommand(Switch1 ,OFF)sendCommand(Switch2 ,ON)]
end

rule "relay"
when
    Item Switch2  received command ON
then
    createTimer(now.plusSeconds(5)) [|sendCommand(Switch2 ,OFF)]
end

But this code is not as nice as it can be. Presumpting that every time, the 1st relay is switched on, the 2nd relay should follow and the 2nd relay sould never switch alone, this rule would do better:

var Timer tRelay = null

rule "Relay auto off and follow"
when
    Item Switch1 received command ON              // whenever relay 1 is switched ON
then
    if(tRelay !== null) return;                   // cancel rule, if existing timer
    tRelay = createTimer(now.plusSeconds(5), [|   // create timer
        if(Switch1.state == ON) {                 // relay 1 on, so 1st execution
            Switch1.sendCommand(OFF)              // switch off relay 1
            Switch2.sendCommand(ON)               // switch on relay 2
            tRelay.reschedule(now.plusSeconds(5)) // reschedule timer
        } else {                                  // relay 1 off, so 2nd execution
            Switch2.sendCommand(OFF)              // switch off relay 2
            tRelay = null
        }
    ])
end

Please be aware that nothing prevents you from switching both relays to ON. The timer might reset the 1st relay if doing that while relay 2 is ON, but only after timer expired!

As you can see, I used a slightly different notation.

  1. a command is ON or OFF (for switches), not “ON” or “OFF” (these are strings, openHAB will change them silently to switch commands)
  2. whenever possible, please use the method instead of the action. Item.sendCommand(command) is the method, sendCommand(Item, command) is the action. The difference is that the action needs two strings, where the method is often capable to handle different types of input.
  3. the lambda (part in brackets [] ) is a parameter of the createTimer(), it’s more clear to write it a part of parameter list, although your code is also correct.

Now you are just showing off. lOL. t Thank you ever so much for all your help. That rule looks so complex that it would have taken me months to build my skill level enough to understand it let alone write it. All this effort just to lift a hidden panel up at the end of my pool and then motorize a panel out to access my pool cover. I so cant wait for the warmer weather to hit so i can put the pieces together.
[Udo_Hartmann. Do you have any experience with water PH sensors? I have been looking around the forum, and there isn’t much here on it.