Momentary Button state

Hello, everyone.

I try to make a push button whose state changes when pressed.
The problem is that I press the push button for only 200 milseconds and then go back to the off state.
This to do a real push button.
So my push button is always off. I want him to change status every time it’s pushd
Here are my config files

Items
Group Verlichting

Switch keuken1 “keuken1” { gpio=“pin:17 debounce:10 activelow:no” }

Rule
rule "keuken1 is a switch"
when
Item keuken1 received command
then
if (receivedCommand == ON)
createTimer(now.plusMillis(200))[
sendCommand(keuken1 ,OFF)
]
end

Sitemap
Frame label=“keuken” {
Switch item=keuken1 mappings=[ON=“AAN”]

Is there anyone who can help me with this?

One way would be to separate button and function

  rule "toggle something"
when
  Item pushbutton changed to ON
then
  if (outputitem.state == ON) 
     sendCommand(outputitem ,OFF)
  } else { 
     sendCommand(outputitem ,ON)
  }
end

The code does not seem to recognize outputitem. Do I have to create an outputitem somewhere else?

outputitem is the device you want to steer with the toggle function

Something like this? but this does not work eclipse smarthome designer is is not exepting this

rule "toggle something"
when
Item keuken1 changed to ON
then
if (keuken1.state == ON)
sendCommand(keuken1 ,OFF)
} else {
sendCommand(keuken1 ,ON)
}
end

Try keuken1.sendCommand(ON)

Do you use the 200ms timer to get visual feedback of actually pressing the button, or is it only used to automatically switch to off again? In case of the latter you should check out the autoupdate=“false” parameter for the Switch item.

Stefaan keuken1.sendCommand (ON) does not work either.

Marcel I use the 200 milliseconds because I work with pulse relays in the power cabinet that only need a short pulse to switch the lights, I also have light dimmers in the power cabinet that I allow to dimming through a pulse of certain time. My problem is just that the switch status after the pulse always goes back off. I tried the autoupdate = “false” but this does not seem to give the desired result.

It’s not that clear what you are trying to do.
I use impulse relays too.
I represent the On/Off state with a Switch Item.
I represent the pulse output with a completely separate Switch Item, and a rule that turns off the pulse after a short while.

In my case I have hardware to read the state of the real light circuit, if you do not have that you will have to use a toggle rule instead like I suggested before. Problem with that is that it may or may not start up the right way round.

I really do not think you can sort this out with only one item.

Ok, this should do it:

var Timer keuken1Timer = null;

rule "keuken1 is a switch"
when
	Item keuken1 changed from OFF to ON
then
	logInfo("rules", "Rule: keuken1 is a switch")

	if (keuken1Timer == null)
	{
		logInfo("keuken1Timer", "Schedule keuken1Timer")
		keuken1Timer = createTimer(now.plusMillis(200))
		[|
			logInfo("keuken1Timer", "Switch button keuken1 off")
			keuken1.sendCommand(OFF)
			keuken1Timer = null
		]
	}
	else
	{
		logInfo("keuken1Timer", "keuken1Timer already scheduled")
	}	
end

In your openhab.log file you should see the log events.

Updated:

In the Habdroid client it seems that status is not always properly updated. For testing I used a regular switch in the sitemap (no mappings parameter) and I most of the time need to scroll the switch out of display and back to see the updated OFF state.

I have all push buttons in my house for the lights. I want to control true openhab. The pulse relays only need a short pulse so now I use a relay board on the gpio output of the raspberry pi this relayboard I turn’s off after a 200millis pulse .So the status of my lights is always off in openhab.What hardware do you use to read the status of the lights? Because I’m looking for something else to change the status of the lights when the wall push buttons or used.

I tried it and the status of my switch is not staying on.
The log:
2017-05-02 19:38:14.484 [INFO ] [.smarthome.model.script.keuken1Timer] - Switch button keuken1 off
2017-05-02 19:38:16.369 [INFO ] [eclipse.smarthome.model.script.rules] - Rule: keuken1 is a switch
2017-05-02 19:38:16.374 [INFO ] [.smarthome.model.script.keuken1Timer] - Schedule keuken1Timer
2017-05-02 19:38:16.578 [INFO ] [.smarthome.model.script.keuken1Timer] - Switch button keuken1 off

No, the button does not stay on because it is switched off after 200 ms…

So basically what you are trying to achieve is the following:

  1. Press push button.
  2. GUI updates to ON.
  3. Start pulse on the GPIO pin.
  4. After 200 ms stop pulse on the GPIO pin.
  5. GUI remains ON
  6. Press push button.
  7. GUI updates to OFF.
  8. Start pulse on the GPIO pin.
  9. After 200 ms stop the pulse on the GPIO pin.
  10. GUI remains OFF.

Is there any way you can determine the current state of the relay board (or in other words that the light is on or off)?

a simple cheap Chinese relay, 240V coil wired across the end lighting circuit. You do need some contact type binary input to OpenHAB for each channel - a spare GPIO pin, or in my case Modbus discrete inputs.
Some impulse relays have a spare pole that could be used.

If you don’t provide some kind of feedback like this, OpenHAB can never be sure if the lights are on or off to begin with.

A bonus is that I can detect when an impulse relay has been activated by a wall button, rather than by Openhab. So OH can detect when a user is making manual changes and use that in rules i.e. set occupancy (someone is there, because they are pressing buttons), perhaps turning off automatic light for a while (the user has made a choice with the button, and probably does not want OH ‘help’).

Items

Switch myLight "Lighting" { bound to circuit monitor hardware, autoupdate=false }
Switch myPulse "Pulse output" { bound to output pin for impulse relay }

Only put myLight on your Sitemap, as a Switch

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
2 Likes

That’s exactly what I want to do. No i have nothing to reed the real state of the lights.

I was planning something like you.I ordered the https://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20170124013428&SearchText=wellpro. The relay output module and the digital inputs module. These came on today.

But I thought so by using the pulse to switch the status of the lights. The pulse of the wall buttons and the relay module is the same.

The relay you put on the lights circuit is only possible with pulse relays, and I have also a lot of light dimmers in the power cabinet and the output current depends on how much you dim the lights so i can not use a relay for this.I think if it is possibel with only the puls i only have to make sure the raspberry never lose power because in that case it will forget the state of the lights.

In the weekend if I have some more time I will try to replace my relay board on the gpio’s with the modbus output relays and digital inputs.And do some more testing
If I do not struggle too much with the modbus modules.

Yes. If your dimmers do not provide feedback, and they can be altered independently of OpenHAB, then OH can never be sure what state they are in. There is no magic !!

In real life, a small relay will probably work reasonably well across a dimmed circuit. But it might buzz at low power, and a “Mostly ON / OFF” indication probably isn’t very useful. Should also be possible to cook up some simple circuit with optocoupler, if you (or a friend) are into electronics.

But much better is a dimmer type that you can “read back” what it is doing.