Sync Software switch State with Physical Switch

  • Platform information:

    • Rpi3
    • openHABianPi 7.05.170509
    • openHAB version: 2.1.0-1
  • Issue of the topic:
    I’m hoping I can have the software switch in the sitemap update when a physical switch is pressed.
    I’m using the latest version of ESP_Easy I have a ESP12f controlling a relay using http commands through openhab.
    Through the ESP_Easy interface I have a physical push button connected to toggle the relay manually and send the state of the relay to Openhab via MQTT. Everything is working except when I push the physical switch the software state is not updated on the sitemap.

  • Please post configurations (if applicable):

    • Items configuration related to the issue
      I believe I need to setup a MQTT subscribe in my item bindingconfig on my item but since I’m using http binding config can I still use MQTT on the same item?
Switch  Lamp1   "Living Room Lamp" <poweroutlet_us> (SmartItems) {http=">[ON:POST:,5,1:on] >[OFF:POST:,5,0:off]"}
  • Sitemap configuration related to the issue
 Frame label="SmartPlugs"{
           Switch item=Garage_Door mappings=[ON="GO!"]
           Switch item=Lamp1
  • Rules code related to the issue
    I don’t currently have any Rules for this item.

You need to add an inbound config to your existing item config:

Hi @sihui

Thanks for the quick responce. I attempted to do that but I guess I messed up the code.
When I add the MQTT inbound config to the item config it breaks even the ability to switch the relay with OH. Here is what the item code looks like when I add the MQTT inbound config.

Switch  Lamp1   "Living Room Lamp" <poweroutlet_us> (SmartItems) {http=">[ON:POST:,5,1:on] >[OFF:POST:,5,0:off]" mqtt="<[broker:ESP_377E2F/Relay/Switch:state:default]"}

I’m guessing I just need something separating the different binding config parts I’ve just not seen any examples showing someone using more than one binding in an items config.
Thanks again for your quick response and help.

You need a comma between binding configs. You should be seeing an error in the logs saying that it can’t parse the binding config.

Hi @rlkoshak

I figured it was something simple like that.
But that didn’t fix my problem. Here is my new item.

Switch Lamp1 "Living Room Lamp" <poweroutlet_us> (SmartItems) {http=">[ON:POST:,5,1:on] >[OFF:POST:,5,0:off]",mqtt="<[<broker>:ESP_377E2F/Relay/Switch:state:default]" }

When I hit the pushbutton on the ESP it toggles the relay and sends the MQTT status update because I get it on MQTT fx but OH doesn’t seem to get it cause it doesn’t change the item state.
The item state does change when I hit the switch on openhab basic ui.
Any ideas why that would be?

And are you seeing anything in the logs?

Before you had broker and here you have <broker> which is absolutely incorrect and would show up as an error in the logs.

I suggest a review of MQTT Binding (v1.11) Getting Started 101

Sorry I didn’t catch that I think that was a oversight from me testing other people’s examples.
After I corrected that I am getting a Warning message in the logs. Here is the message I’m seeing when I switch the relay on and back off using the physical switch.

2017-12-08 15:30:26.078 [WARN ] [] - given new state is NULL, couldn’t post update for ‘Lamp1’

2017-12-08 15:34:30.619 [WARN ] [] - given new state is NULL, couldn’t post update for ‘Lamp1’

And here is what is shows using the OH switch.
2017-12-08 15:35:28.575 [ItemCommandEvent ] - Item ‘Lamp1’ received command ON

2017-12-08 15:35:28.601 [ItemStateChangedEvent ] - Lamp1 changed from OFF to ON
2017-12-08 15:35:28.833 [WARN ] [] - given new state is NULL, couldn’t post update for 'Lamp1’
2017-12-08 15:35:29.946 [ItemCommandEvent ] - Item ‘Lamp1’ received command OFF

2017-12-08 15:35:29.970 [ItemStateChangedEvent ] - Lamp1 changed from ON to OFF
2017-12-08 15:35:30.185 [WARN ] [] - given new state is NULL, couldn’t post update for ‘Lamp1’

Is this an indication of a problem with

If you are using ESPEasy and did not change the default topics via web config you need a leading slash for the topic:


To be sure try with and without that slash (or subscribe via mqtt.fx to the generic # topic to see the correct topic syntax)

Is there a specific reason why you are using the http call to switch the relay? I can also be done via mqtt:


But if you feel more comfortable with http and it’s already working, don’t “change a running system”.

No. because you are not using map transformation in your item setup …

Hi @sihui

Thanks for the info I did as you mentioned and found the problem after looking a little closer.
It seems the problem is that the ESP Easy is sending the MQTT message with no caps. Or if it sends the message with the correct Caps then it only sends the content 1 or 0 and it seems the only way OH will update the state is if the message contains ON or OFF.
I’m not sure how to fix this because I have several other ESP’s running ESPEasy and they don’t have this problem but it does seem to be a problem in the ESP Easy firmware.

NOW you need a map transformation.




Hi @sihui
Thanks again for all your help.
So I created a as seen below.


But now I get this error in the logs.

2017-12-11 14:25:30.513 [WARN ] [rm.AbstractFileTransformationService] - Could not transform '0' with the file '' : Target value not found in map for '0'

2017-12-11 14:25:30.517 [WARN ] [] - given new state is NULL, couldn't post update for 'Lamp1'

I’ve restarted everything after making the change but that hasn’t helped either.
Any other ideas I can try?

Complete explanation can be found here:

Hi @sihui

I got it working thanks so much for all your help it looks like it was a mix between having the wrong spelling and/or missing capital letters and possibly a poorly named map file. After I changed the name from to it started working.

As far as ESP Easy and I think I’ve read this somewhere too but it seems you need the device that’s sending the MQTT status message as the first device on the ESP devices page.

So anyone out there having the same trouble syncing a physical switch with the Openhab software switch here is my working setup.
Here is my OH Item Don’t forget to set the IP to the IP of your ESP.

Switch  Lamp1   "Living Room Lamp" <poweroutlet_us>  {http=">[ON:POST:http://XXX.XXX.XXX.XX/control?cmd=GPIO,5,1:on] >[OFF:POST:http://XXX.XXX.XXX.XXX/control?cmd=GPIO,5,0:off]", mqtt="<[broker:ESP_377E2F/Relay/Switch:state:MAP(]" }

Here is my Sitemap nothing special here.

 Frame label="SmartPlugs"{
         Switch item=Lamp1

Here is my 1st device on the ESP Easy setup.

Here is my 2nd device on the ESP Easy setup.

And here are the rules I’m using on the ESP Easy firmware.

On SwitchIn#Switch=0 do
     if [Relay#state]=1

Here is the MQTT Controllers setting on ESP Easy. (Again be sure to change the IP to your controller.)

And here is the mqtt.cfg I’m using on OpenHab. I only made one small change to the default generated at the install. (Be sure to change the IP to your MQTT server.)


I hope this will help anyone else looking to control something with a relay using a ESP 12 running ESP Easy and still have a Physical Switch option and have it show the correct state of the device in the OpenHab user interfaces.

Hi @ Ryan,
Can you please share your mqtt config also?

Glad you got it working :grinning:

Nope, works with devices at any place:

You are doing things very complicated, though. You could achieve the same result without any http call, just with mqtt alone …:sunglasses:

Please note when editing the controller subscribe and publish topics the way you did it you can only subscribe and use one device to that esp8266 via mqtt. If you leave it general you can connect more devices:

To elaborate a little bit on this one:

Controller Publish: /%sysname%/%tskname%/%valname%

%sysname% = name of the esp, in my case esp8266one
%tskname% = name of one or more devices, in my case dht22 and light
%valname% = value choosen for specific device channel, in my case temperature and humidity for one device, intensity for the other

That makes a mqtt publish topic (don’t forget the slashes):


In the process of attempting to get the relay switch working I found it easier to use http calls.
But that was before I wanted the software switch to update. Once that came into play it does make more sense to use MQTT and I attempted to set it up with MQTT per your example but it wasn’t working for me so I just left it with http.
I’m new to MQTT and programming in general so the less code I have to do the easier it is for me.
As for the Controller Publish and Subscribe I did have the publish as /%sysname%/tskname% as in your example but that’s when I was seeing the published MQTT message without the capital letters.

As for the leading slash is there a benefit to using that?
Now that I have this working and I’ve learned a few things about MQTT I think I’ll try another using only MQTT to control and send status. Thanks again for all the info and help.

Never change a running system, if it works leave it that way.

No, not at all. You could easily change

That is the reason I replied on your working config: if you do it a little bit different you are not limitied to one device connected to the ESP and you have less code (you can omit the ESPEasy rules)

Have fun.

There is an old Canadian sitcom called Red Green. His motto was “If it ain’t broke, you’re not trying hard enough.”


1 Like