GPIO - Event listener not working

That isn’t how the GUI works.

Think of the GUI as reflecting the state of your light, not the state of your button. When you light is on, Kitchen_Light should be ON. When your light is off, Kitchen_Light should be OFF.

If you try to make the GUI act like your spring loaded button it will never show anything.

That being said, to achieve what you are trying to do you need the following (in generic terms):

  1. A way to trigger the light from openHAB. I assume you have this already.
  2. A way to communicate to openHAB that the physical button was pressed. This probably takes the form of another Switch Item.
  3. A rule to update the Item from 1 when 2 is triggered
  4. A MAPPING on your sitemap so it only shows a button instead of a toggle switch

Items:

Switch Kitchen_Light "Kitchen Light" <light> { ... }
Switch Kitchen_Light_Button {...} // we don't need a label or icon because we don't show this on the sitemap

Rule:

rule "Update light when button pressed"
when
    Item Kitchen_Light_Button received command
then
    if(Kitchen_Light.state == ON) Kitchen_Light.postUpdate(ON) else Kitchen_Light.postUpdate(OFF) // this will change its state without triggering the switch and sending the command to the light
end

Sitemap:

Switch item=Kitchen_Light mappings=[ON=Toggle]

When you press the button on the sitemap it will cause the Kitchen_Light switch to send the ON command to the light. This will need to be configured so any command received toggles the light. If you need to you might need to create a new rule to handle that. When you press the hardware button a message/signal gets sent to openHAB and the rule updates Kitchen_Light.

Thanks for your reply:)

It still doesn’t work somehow, i am a bit unsure with :
I read up about the debounce, and it seems like 20ms is fine and since I use pullup.

I use activelow, so my button is defined as

Contact Contact_LivingRoom1 {gpio="pin:18 debounce:20 activelow:yes"}

Then to the rule:
Item received command []
Item received update []
Item changed [from ] [to ]

So detect my button I could use either: (Case sensitive i guess, upper case right?)
changed from OPEN to CLOSED
received update CLOSED
received command ?? (can leave it out as you suggested)

so my code looks like this

rule "Update kitchen light when button pressed"
when
    Item Contact_LivingRoom1 received CLOSED
then
	logInfo("PersistenceDemo2", "You did not press this button during the last 5 seconds! "+ Light_Kitchen.state)
	
    if(Light_Kitchen.state == ON) {
		//sendCommand(Light_Kitchen, OFF)
		logInfo("PersistenceDemo2", "ON")
		Light_Kitchen.postUpdate(OFF)
	}
	else {
		//sendCommand(Light_Kitchen, ON)
		Light_Kitchen.postUpdate(ON) 
		logInfo("PersistenceDemo2", "OFF")
	}// this will change its state without triggering the switch and sending the command to the light
end

However i would like my sitemap to display on off button so I skipped the toggle button, so here my item is defined as:

	Switch Light_Kitchen 	"Ceiling" (Group_Kitchen,Lights)	{ gpio="pin:6" } //Relay 4

So what am i doing wrong, how do I find the correct debounce value and which action should i use? postupdate or send command as here?

I am trying to document all pretty well in case someone else needs it.

From openHAB’s rule’s perspective the debounce doesn’t matter. Just experiment with the debounce value in your Contact_LivingRoom1 binding config until you only receive one CLOSED per button press.

Change your trigger to received command CLOSED

Now that I understand how the items work, change the “postUpdate(” calls to “sendCommand(” because you really are commanding the kitchen light to turn on.

I think that will work. You will probably have to play with the debounce value though until the rule only executes once per button press.

My working rule:

rule "Update kitchen light when button pressed"
when
    Item Contact_LivingRoom1 changed from OPEN to CLOSED
then
	
	
if(Light_Kitchen.state == ON){
	sendCommand(Light_Kitchen,OFF)
		//LogInfo("Kitchen", "Switching to OFF")
	}
	else {
		sendCommand(Light_Kitchen,ON)
		//LogInfo("Kitchen", "Switching to ON")
	}
end




rule "Update bathroom light when button pressed"
when
    Item Contact_Bathroom changed from OPEN to CLOSED
then
	
	
if(Light_Bathroom.state == ON){
	sendCommand(Light_Bathroom,OFF)
		//LogInfo("Kitchen", "Switching to OFF")
	}
	else {
		sendCommand(Light_Bathroom,ON)
		//LogInfo("Kitchen", "Switching to ON")
	}
end

rule "Update living room light when button pressed"
when
    Item Contact_LivingRoom2 changed from OPEN to CLOSED
then
	
	//and Light_Module2.state == ON and Light_Module3.state == ON and Light_Module4.state == ON 
    if(Light_Module1.state == ON ) {
		sendCommand(Light_Module1, OFF)
		sendCommand(Light_Module2, OFF)
		sendCommand(Light_Module3, OFF)
		sendCommand(Light_Module4, OFF)
		sendCommand(Light_Hallway, OFF)
		//LogInfo("LivingRoom", "Switching to OFF")
	}
	else {
		sendCommand(Light_Module1, ON)
		sendCommand(Light_Module2, ON)
		sendCommand(Light_Module3, ON)
		sendCommand(Light_Module4, ON)
		sendCommand(Light_Hallway, ON)
		//LogInfo("Kitchen", "Switching to ON")
	}
end

My working items:

/* Lights */
	
	
	Switch Light_Balcony 		"Outlet" 		(Group_Balcony) 						{ gpio="pin:10" } //relay  3
	
	Switch Light_Module1 		"Module 1" 		(GF_Living,Lights) 		{ gpio="pin:12" }  //Relay 16
	Switch Light_Module2 		"Module 2" 		(GF_Living,Lights) 							{ gpio="pin:19" }   
	Switch Light_Module3		"Module 3" 		(GF_Living,Lights) 							{ gpio="pin:16" } //Relay 14
	Switch Light_Module4 		"Module 4" 		(GF_Living,Lights) 							{ gpio="pin:26" }
	//Switch Light_GF_Elefant 	"Elefant" 		(GF_Living,Lights) 							{ gpio="pin:6" }
	//Switch Light_GF_Glass 		"Glass" 		(GF_Living,Lights) 						{ gpio="pin:13" }
	//Switch Light_GF_WallWasher 	"Wall washer" 	(GF_Living,Lights) 						{ gpio="pin:10" }
	
	/*Bathroom*/
	Switch Light_Bathroom 		"Ceiling" 		(FF_Bath,Lights) 								{ gpio="pin:9" }
	
	/*Kitchen*/
	Switch Light_Kitchen 		"Ceiling"  					(Group_Kitchen,Lights)				{ gpio="pin:6" } //Relay 4
	
	/*Group_Hallway*/
	Switch Light_Hallway 	"Ceiling"  						(Group_Hallway,Lights)				{ gpio="pin:20" } //Relay 12
	Switch Light_DoorOpener "Door Opener" 		<heating> 	(Group_Hallway,Lights)				{ gpio="pin:22" } //Relay 5
	Switch Heating_Hallway 	"Hallway Heating" 	<heating>	(Group_Hallway, Heating) 	{ gpio="pin:17" } //Relay 9
	
	
	/* Heating */
	
	
	Switch Heating_GF_Living 	"Livingroom" 		<heating>	(GF_Living, Heating)	{ gpio="pin:27" }
	Switch Heating_GF_Kitchen 	"Kitchen" 			<heating>	(GF_Kitchen, Heating)	{ gpio="pin:11" }
	Switch Heating_FF_Bath 		"Bath" 				<heating>	(FF_Bath, Heating)		{ gpio="pin:5" }
	Switch Heating_FF_Office 	"Master bedroom" 	<heating>	(FF_MasterBedroom	, Heating)	{ gpio="pin:21" }
	Switch Heating_FF_Bed 		"Bedroom" 			<heating>	(FF_Bed, Heating)				{ gpio="pin:4" }
	
	
	/*Contact*/
	
	Contact Contact_LivingRoom1 {gpio="pin:18 debounce:30 activelow:yes"}
	Contact Contact_LivingRoom2 {gpio="pin:23 debounce:30 activelow:yes"}
	Contact Contact_Bathroom {gpio="pin:24 debounce:30 activelow:yes"}

	Color rgb_strip_living_room "RGB Ledstrip Living Room" (rgb) {dmx="CHANNEL[22,23,24:1000]"}

Thanks guys, sometimes i need to press the bathroom and living room switch several times, no idea why but thats liveable… No I need to get my motion sensor to work, myopenhab and dmx…

Hi Guys,
I am making a nice Pullup Din rail shield these days. I will post some pictures when I get it all soldered up and installed. For now I just have a wire nightmare:)

So to my followup question: If my GPIO does not detect any change, does that then mean that the debounce is set to high? Or is it some other issue, and why do I only have the problem with some switches and not all that it misses detection?

Have you measured the voltage at the GPIO port when switch is open?
And probably you should use a 1k pullup resistor for the GPIO

yupp, its then 3.217V or so, I use a 10K Pullup, I am working on a nice pullup din rail mounted shield. I will let you know how it goes:)

I changed the debounce to 0 on two of my switches now, and it works better now. Almost 0 fail detect rate.

Cheers

Just a side note, My GPIO stopped working again. I figured out the problem was actually the persistence, you need to save the state of the items otherwise it will not work… No matter if you have reset the GPIO or not.

can you explain clearly what is to be done.yesterday i installed openhab an i was able to control my gpio pins. and today when i was trying to control them after installing lirc the message of error comes that resource is busy and i even now delete the lirc it is still showing same error message .

Hi @skatun

i am using gpio bindings 1.9.0 and openhab distribution-1.7.0.
when openhab is starting everything goes well , gpio pin get exported but after that , i got state

PIR state updated to undefined

i am getting an error:

2017-01-02 07:37:04.280 [ERROR] [.o.b.gpio.internal.GPIOBinding] - Error occured while creating backend object for item PIR, exception: Invalid argument 

i am not getting what is happening …?

Hi guys . i’m new on the openhab and i have some problems. I tried everything but it;s not working :)) . i wrote the code but is not working .

this is my items tab :


//Relays
Switch channela "Bec Interior" { gpio="pin:26 activelow:yes initialValue:high" }
Switch channel2 "Bec Exterior" { gpio="pin:19 activelow:yes initialValue:high" }
Switch channel3 "Channel 3" { gpio="pin:13 activelow:yes initialValue:high"}
Switch channel4 "Channel 4" { gpio="pin:6 activelow:yes initialValue:high"}

Contact DoorSensor "Door Sensor [%s]" { gpio="pin:16 debounce:10 activelow:yes" }
Contact contact_interior  { gpio="pin:24 debounce:30 activelow:yes" }

and this is my rules tab:

rule "Update light when button pressed"
when
    Item contact_interior changed from OPEN to CLOSED
then
    if(channela.state == ON){sendCommand(channela,OFF)
//LogInfo("interior","Switching to OFF")
}
 else{ sendCommand(channela,OFF)
//LogInfo("interior","Switching to OFF")
}
end

and this is my sitemap tab:

sitemap home label="Andy's House"
{

       Switch item=channela
       Switch item=channel2
       Switch item=channel3
       Switch item=channel4

       Text item=contact_interior
       Text item=DoorSensor

idk what i’m doing wrong . when the button is pressed . nothing happens