How to add a physical switch

Hi to all… i m trying to control a light with openhab and a physical push button using two gpio from my ESP8266.

my idea is to switch on the light via mqtt.

i created 2 object

Switch  switch "Switch" {mqtt=">[localbroker:/Prova/gpio/4:command:ON:1],>[localbroker:/Prova/gpio/4:command:OFF:0],"}
Contact hardwareswitch "Hardware Switch [MAP(]" {mqtt="<[localbroker:/Prova/state/switch:state:MAP(],"}

the first switch controls the relay
the contact is connected to the push button wired from GND to GPIO13 (in my case) input of my ESP


i can see in openhab the changes of state of my pushbutton… this means that the signal enter correctly in OH. and also i can set the light ON and OFF by my switch…

i am in trouble with a rule that when the pushbutton change the state from on to off …
if the light is ON then switch it OFF and change state.
if the light is OFF then switch it ON and change state…

any advice?

thank you

Create a rule that triggers when the hardwareswitch changes and send a command to toggle the state on the switch. NOTE: “switch” is a terrible name for an Item. It is too close to a reserved word. I recommend using a more descriptive name.

switch.sendCommand(if(switch.state == ON) OFF else ON)
1 Like

Thank you seems to work but now i noticed 2 Things

Every time i change a the state in the log windows appears
016-11-02 01:38:45.645 [INFO ] [runtime.busevents ] - Luce_Studio state updated to OFF
2016-11-02 01:38:46.080 [WARN ] [t.i.s.MapTransformationService] - Could not find a mapping for ‘-’ in the file ‘’.

But no map is set.

this is my item

Switch	Luce_Scrivania	"Scrivania"	(gZN_Studio,lights)                         {mqtt=">[localbroker:/Prova/gpio/4:command:ON:1],>[localbroker:/Prova/gpio/4:command:OFF:0], <[localbroker:/Prova/relay/relay:state:ON:1], <[localbroker:/Prova/relay/relay:state:OFF:0],"}

and this is my item in the sitemap

Switch item=Luce_Scrivania 	


also i noticed (ask to you how to initialize all lights on startup)
all my lights can receive the state via mqtt. but i am confused to use the postupdate command

something similar to this

rule "Initialize light states"
		System started


The problem with mapping was solved. I removed unused rules in the file…

Now remain to retrieve the state information of my swirches…

Tryng to write

rule "Initialize light states"
		System started

No success… still not receive the update on the startup

Any suggestion?

can i recall the mqtt topics directly at startup?

Your Item is not initialized. I’m not sure what the mapping error is referring to though. I don’t see any map. Do you have any other uninitialized Items that have a Map? The timing might be a coincidence.

And this is where the choice of “switch” for an Item name is causing problems. “switch” is a reserved word. Rename your Item to something else, something more descriptive and not a reserved word and, assuming that whatever you rename “switch” to is not undefined the above should work.

However, a more “correct” approach might be to use persistence and restoreOnStartup.

Actually yes you can if you use the MQTT Action you can publish messages to the topics which could cause the devices to publish their states. But you can’t get the last state the devices published because once read the message goes away.

Hi… i changed names before posting my preview message.

I wrote switch because i was trying update the switch state. Now the item has another name.

For the mqtt i prefeer the items publish their state at startup because i can usa the switches also by phisical button. So when oh coming up the state can be different.

There is a way to publish all states?

It depends on your remote devices. I have my system set up with a System started rule that publishes a message to a topic all my remote devices subscribe to. When the devices see a message posted to that topic they publish their current state to their respective topics.

However, this requires both OH (the System started rule) and the devices to support this. There is nothing built into the MQTT binding or anything else that does this for you.

yes… I’m still bricked at this point

i can send an MQTT message to all my esp with openhab (setting a rule) but how can set ESPeasy to receive this and reply with the status of the device? a dummy device?

You must edit the code running on the ESP to subscribe to the messages and respond accordingly.

I don’t know much about Arduino coding so can’t help with how to do this.

CIao Damiano,
se puoi aiutarmi il mio progetto consiste nel far arrivare i contatti puliti di ogni pulsante fisico in casa alle gpio di raspberry,
Alla raspberry collegare poi una o due schede a 8ch a relè tipo quelle della Sainsmart, dove poi verranno collegati i carichi (luci).

Per cui si avrebbe la possibilità di controllare quale carico o carichi azionare alla pressione di quel pulsante fisico o pulsante web.
Piuttosto che attivare degli scenari alla pressione prolungata ecc ecc.

E’ una cosa fattibile da fare con openhab2?, se si quale protocollo devo utilizzare?,