[SOLVED] Connect a lamp with step by step (latching) relay and Raspberry

Hello everyone, I want to make the circuit attached but I’m a beginner of OpenHub.
I use Raspberry Pi 3 model B+ with openHAB 2.3.0-1 image
I have already connected openHAb to Google Home.

The main features are:

  • The lamp can be switched on and off with a normal button that trigger a double step by step relay.
  • The lamp can also be used when the Raspberry or Google Home are offline.

I set up an open hub with an example (without much understanding) and at the moment I can set or reset a pins of the Raspberry Pi and read the status of anothers pin. What I have in part is OK, but I have to understand how to implement the logic that I need.
I need that logic state of the lamp is read by a input pin and an output pin must trigger the step by step relay for short time (ex.750ms)

Where can I start?

// home.items
// Relays for lamps
 // I need that the state of lamp is read by another pin for ex. StepRelay pin 24
Switch LED "LED" ["Lighting"]  { gpio="pin:21 activelow:yes initialValue:high"}
Switch LED2 "LED2" ["Lighting"]  { gpio="pin:12 activelow:yes initialValue:high"}

//Generic output
Switch channel3 "Channel 3" { gpio="pin:26 activelow:yes initialValue:high" }
Switch channel4 "Channel 4" { gpio="pin:19 activelow:yes initialValue:high" }
Switch channel5 "Channel 5" { gpio="pin:13 activelow:yes initialValue:high" }
Switch channel6 "Channel 6" { gpio="pin:6 activelow:no"}
// Input
Contact DoorSensor "Door Sensor [%s]" { gpio="pin:16 debounce:10 activelow:yes" }
Contact Button "StepRelay [%s]" { gpio="pin:24 activelow:yes" }

Thank you very much
S.Fredella

You might find this helpful. No timer needed for a simulated pushbutton control when you have feedback

Yes I found the relay Finder with two contact, part number in my attachment 26.02.8.230.0000.MMM.
I do not know how to program OpenHub to use this circuit, as I wrote in the post.
The circuit certainly works but what must be the script to work as described?
Thank You

Click the post and read it, two sample rules there.

Thank rossko57, it seems to me the right way, but I tried and something does not work, in fact it does not work even what previously worked. The LED no longer activates the output and the inputs no longer have the right icon in the sitemap. The three files I edited are:

//home.items

Switch LED "LED" ["Lighting"]  { gpio="pin:21 activelow:yes initialValue:high"}
Switch LED2 "LED2" ["Lighting"]  { gpio="pin:12 activelow:yes initialValue:high"}

// Relays
Switch channel3 "Channel 3" { gpio="pin:26 activelow:yes initialValue:high" }
Switch channel4 "Channel 4" { gpio="pin:19 activelow:yes initialValue:high" }
Switch channel5 "Channel 5" { gpio="pin:13 activelow:yes initialValue:high" }
Switch channel6 "Channel 6" { gpio="pin:6 activelow:no"}
// Input
Contact DoorSensor "Door Sensor [%s]" { gpio="pin:16 debounce:10 activelow:yes" }
Contact Button "Button [%s]" { gpio="pin:24 activelow:yes" }

Switch myLight "Lighting" { bound to circuit monitor hardware, autoupdate=false }
Switch myPulse "Pulse output" { bound to output pin for impulse relay }
// home.rules
rule "Master light control"
when
   Item myLight received command     // from a Rule or the UI
then
   if(receivedCommand == ON && mylight.state == OFF) {
      myPulse.sendCommand(ON)
   } else if (receivedCommand == OFF && mylight.state == ON) {
      myPulse.sendCommand(ON)
   }        // else already in requested state
end

rule "light feedback monitor"
when
   Item myLight received update     // from hardware monitor
then
   if(myPulse.state == ON) {    // this was an automated change
      myPulse.sendCommand(OFF)    // we don't time the pulse, simply stop when it has worked
      logInfo("lights", "auto change complete")
   } else {
      // this was a manual change by wallswitch, we can do something else
      logInfo("lights", "user pressing buttons")
   }
end
// home.sitemaps
sitemap home label="casa"
{
Switch item=LED
Switch item=LED2
Switch item=channel3
Switch item=channel4
Switch item=channel5
Switch item=channel6
Switch item=myLight

Text item=Button
Text item=DoorSensor
}

I have a question, where and how should I define the myPulse.sendCommand (ON) myPulse.sendCommand (OFF) functions to specify the output an imput pin?

Um, look - these are examples, you see. You’re supposed to substitute your own Items.
And then you’d need to put your Items in the rules.

sorry it to late to use the brain…

I change the two line, can it work?

Switch myLight “Lighting” { gpio=“pin:24 activelow:no autoupdate=false” }
Switch myPulse “Pulse output” { gpio=“pin:12 activelow:yes initialValue:high” }

I’m not familiar with GPIO configs. The additional autoupdate parameter needs a comma to separate. Plus my example does have an error with quotes.

Switch myLight "Lighting" { gpio="pin:24 activelow:no" ,  autoupdate="false" }

Now with your suggestion the behavior has changed. Something in the rule does not work: in fact if I set or reset the input pin the switch icon is always green (on enabled). in addition The output never activates the relay. The switch selector moves to on but if I update the browser page it returns to the left (inactive) it seems that only the second rule and not the first one work.

As I said, I don’t know GPIO. Neither of your channels look like an input channel though.

Using my example’s names, myLight is supposed to be bound to the input channel that tells openHAB if the relay is on or off.
myPulse is supposed to be bound to the output channel that controls the relay that pretends to be a pushbutton.

I really appreciate your help.
In the scenario that I described in my last post I connected a wire between the input to be used by openhub as sense and a third pin, so I can test the rules. I would expect the pin connected to the relay to follow the rule but this does not happen.
At the moment the file items are as follows:

Switch LED "LED" ["Lighting"]  { gpio="pin:21 activelow:yes initialValue:high"} // Ok

// Relays
Switch channel3 "Channel 3" { gpio="pin:26 activelow:yes initialValue:high" } //Ok
Switch channel4 "Channel 4" { gpio="pin:19 activelow:yes initialValue:high" } //Ok
Switch channel5 "Channel 5" { gpio="pin:13 activelow:yes initialValue:high" } //Ok
Switch channel6 "Channel 6" { gpio="pin:6 activelow:no"}  //ok
// Input
Contact DoorSensor1 "Door Sensor [%s]" { gpio="pin:16 debounce:10 activelow:yes" } // ok it follow pin 6


Switch myLight "Lighting" { gpio="pin:16 activelow:yes" ,  autoupdate="false" } // This pin is connected to pin 6 (Channel 6) to test the rule
Switch myPulse "Pulse output" { gpio="pin:12 activelow:yes initialValue:high" }

What I expect is that:
if the pin 6 is low and I click to switch on myLight the relay must switch on than switch off after pin 6 go low;
if the pin 6 is high and I click to switch on myLight nothing must change
if the pin 6 is low and I click to switch off myLight nothing must change
if the pin 6 is high and I click to switch off myLight the relay must switch on than switch off after pin 6 go high

In addition there is a way to debug the rule?

Thank You

I don’t get what connecting pin 6 to pin 16 is about.

If pin 6 or 16 is an input, how about adding some debounce like what I guess is a door sensor input.

Test your Items first.
Take the rules out.
Put both your Items on your sitemap as Switch.

If you use a real pushbutton to activate the step relay on and off, does myLight show the changing state? You may need to refresh the UI.

Once that is working, try switching myPulse. If you turn it ON then your step relay should change. When you turn it OFF nothing much should happen.

Rules won’t work until that works reliably.

Hello, I found the solution!!

Contact Sensor1 "Sensor [%s]" { gpio="pin:16 debounce:10 activelow:yes" }
Switch myLight "Lighting" { autoupdate="false" }
Switch myPulse "Pulse output" { gpio="pin:12 activelow:yes initialValue:high" }
rule "Master light control"

when
   Item myLight received command     // from a Rule or the UI
then
   if(receivedCommand == ON && Sensor1.getStateAs(OpenClosedType)  == OPEN)
   {
    myPulse.sendCommand(ON)
   }
   else
   {
    if (receivedCommand == OFF && (Sensor1.getStateAs(OpenClosedType) == CLOSED ))
    {
     myPulse.sendCommand(ON)
    }
   }
end


rule "light fb"
when
    Item Sensor1 changed
then
	myPulse.sendCommand(OFF)
    if(Sensor1.getStateAs(OpenClosedType) == OPEN)
    {
     myLight.postUpdate(OFF)
    }
    else
    {
     myLight.postUpdate(ON)
    }	
end
sitemap home label="casa"
{
 Switch item=myLight icon="light"
}

Thank You roossko57 for help.

P.S. It is necessary add a pull-up resistor between vcc and sensor pin 16 because it is not possible use Raspberry internal pull-up with open hub (I think).

I had a similar scenario. I was not confortable with mixing mains and low voltage wires in a relay, so I decided to go for this solution instead: GitHub - crgarcia12/electronics-homeassistant-lightscontroll

I built a cheap PCB myself and bought an mains-approved case (will upload photos later).
Seems to work pretty well and I think it’s safer because it’s full of insulation, fuses, etc :rofl: