Sync Software switch State with Physical Switch

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

Okay so I have the ESP with the relay working from OH software switch. But without the ESPEasy rules how do I control the relay using the physical switch. I’m assuming I need OH to listen for ESP_377E2F/SwitchIn/Switch:state and send the ESP_377E2F/gpio/5:command depending on the state. But don’t I then have to use OH rules?
Here is my item in OH

Switch  Lamp2   "Bed Room Lamp"   <poweroutlet_us> {mqtt="<[broker:ESP_377E2F/Relay/state:state:MAP(],>[broker:ESP_377E2F/gpio/5:command:ON:1],>[broker:ESP_377E2F/gpio/5:command:OFF:0]"}

Here is my Devices SwitchIn is sending the MQTT status too

I took your advice and changed my Controller

Can you give me some advice on how to use the Physical switch to control the relay?

This is how I control my relay on an ESP8266 with ESPEasy:

Switch Alarm_Relay <switch> {mqtt=">[mosquitto:/esp8266two/gpio/4:command:ON:0],>[mosquitto:/esp8266two/gpio/4:command:OFF:1]"}

Note that I left the default values from the firmware, so I have to use a slash.

And this is a switch config:

Switch Alarm_Switch_WLAN_IN <alarm>  {mqtt="<[mosquitto:/esp8266two/alarm/switch:state:MAP(],<[mosquitto:/esp8266two/alarm/switch:command:ON:1],<[mosquitto:/esp8266two/alarm/switch:command:OFF:0]"}

If I understand correctly you have 2 items in your items file. One is Alarm_Relay and one is Alarm_Switch_WLAN_IN

The first switch Alarm_Relay controls the relay it’s self by turning gpio 4 high or low. The 2nd switch subscribes to
esp8266two/alarm/switch state, what do the commands do after Do you have 2 items in your sitemap?
One for turning the alarm on and off and the other for the alarm state?
Here is my physical setup since you can’t make out the pin numbers I’ll elaborate. The pushbutton reset switch (pulled from an old PC case) is connected to GPIO 13 and ground. the Relay is connected to GPIO 5. I guess my question is how do you tell GPIO 5 to go high or low when GPIO 13 changes it’s state? Is this possible without using any rules in ESPEasy and OH2?

I was a bit short on time yesterday, so let me elaborate a bit on the link I provided:

Assuming the solution in that link is a working solution (I did not try anything similar with my ESP yet, I’m just using virtual switches for my relay), this should be a working solution for you:

Switch Lamp2 "Bed Room Lamp" <poweroutlet_us> {mqtt="<[broker:ESP_377E2F/SwitchIn/Switch:command:MAP(],>[broker:ESP_377E2F/gpio/5:command:ON:1],>[broker:ESP_377E2F/gpio/5:command:OFF:0]"}

where content is:


If that does not work you could use a simple rule in openHAB to synchronize the switch with the relay.

Have fun.

1 Like