[SOLVED] Need help with a switch that sometimes comes in a loop

Platform: Synology Diskstation DS214+ with DSM 6.2
Openhab 2.2.0.010
Mosquitto 1.4.14-9

I have a few Sonoff switches for a group of lamps and normally everything is working quite well.
But sometimes one of the switches comes in a loop and continues to switch on and off without possibility to break the loop. This happens normally in combination or short after I have used the button on the switch to manually switch on or off the lamp.

Can anybody give me some hints, how to find the reason for this behaviour? I guess there is something wrong or missing in the configuration.
Below you see the content of the configuration files:

OpenHab configuration

=== Things ===
(openhab/conf/things/default.things)

ntp:ntp:demo [ hostname="se.pool.ntp.org", refreshInterval=60, refreshNtp=30 ]

=== Services ===
(openhab/conf/services/mqtt.cfg)

mybroker.url=tcp://192.168.1.51:1883
mybroker.clientId=nas

=== Items ===
(openhab/conf/items/default.items)

Switch 	Balkong 	"Balkongdörr"	{mqtt=">[mybroker:/BalkongdorrLamp/gpio/12:command:ON:1],>[mybroker:/BalkongdorrLamp/gpio/12:command:OFF:0],<[mybroker:/BalkongdorrLamp/lightState/Switch:command:OFF:0],<[mybroker:/BalkongdorrLamp/lightState/Switch:command:ON:1]"}
Switch 	Fonster 	"Fönster"  		{mqtt=">[mybroker:/VardagsrumLamp/gpio/12:command:ON:1],>[mybroker:/VardagsrumLamp/gpio/12:command:OFF:0],<[mybroker:/VardagsrumLamp/lightState/Switch:command:OFF:0],<[mybroker:/VardagsrumLamp/lightState/Switch:command:ON:1]"}
Switch 	Garage 		"Garage"  		{mqtt=">[mybroker:/UtomhusBalkong/gpio/12:command:ON:1],>[mybroker:/UtomhusBalkong/gpio/12:command:OFF:0],<[mybroker:/UtomhusBalkong/lightState/Switch:command:OFF:0],<[mybroker:/UtomhusBalkong/lightState/Switch:command:ON:1]"}
Switch 	Kontor 		"Kontor"  		{mqtt=">[mybroker:/UtomhusKontoret/gpio/12:command:ON:1],>[mybroker:/UtomhusKontoret/gpio/12:command:OFF:0],<[mybroker:/UtomhusKontoret/lightState/Switch:command:OFF:0],<[mybroker:/UtomhusKontoret/lightState/Switch:command:ON:1]"}

Number 	InneTemp 	"Temperatur inomhus [%.1f °C]" 	<temperatur> 	{ mqtt="<[mybroker:/UtomhusBalkong/InneTemp/Temperatur:state:default]" }
Number 	InneFukt 	"Fuktighet inomhus [%.0f %]" 	<fuktighet> 	{ mqtt="<[mybroker:/UtomhusBalkong/InneTemp/Fuktighet:state:default]" }

DateTime Date  "Date [%1$tY-%1$tm-%1$td %1$tH:%1$tM]"  { channel="ntp:ntp:demo:dateTime" }

=== Rules ===
(openhab/conf/rules/default.rules)

rule "lichtuit"
when
	Time cron "0 0 0 ? * *"
then
	sendCommand(Balkong, OFF)
	sendCommand(Fonster, OFF)
	sendCommand(Garage, OFF)
end

rule "lichtaan"
when
	Channel 'astro:sun:906dd801:set#event' triggered START
then
    sendCommand(Balkong, ON)
	sendCommand(Fonster, ON)
	sendCommand(Garage, ON)
end

rule "klockan_1800_on"
when
	Time cron "0 0 18 ? * *"
then
    sendCommand(Balkong, ON)
end

=== Sitemap ===
(openhab/conf/rules/default.rules)

sitemap default label="Huset" {
	Frame label="Datum" {
		Text item=Date
	}
 	Frame label="Vardagsrummet" {
		Switch item=Balkong label="Balkongdörr" icon="light"
		Switch item=Fonster label="Fönster" icon="light"
	}
 	Frame label="Utomhus" {
		Switch item=Framdorr label="Framdörr" icon="light"
		Switch item=Garage label="Garage" icon="light"
		Switch item=Kontor label="Kontor" icon="light"	
	}
}

=========================================================
BalkongdorrLamp (Sonoff S20 with ESP Easy)
Rules set1 (to be able to switch the light on and off with the button on S20)

on lightSwitch#Switch do
   if [lightState#Switch]=0
      gpio,12,1
   else
      gpio,12,0
   endif
endon

OK

You a using a command instruction for the inbound mqtt which will send the device in a command loop

First I recommend that you create a MAP transform to make your code clearer
Create a file in the transform folder called espeasy.map with the following content:
Make sure that the MAP transformation service is installed

0=OFF
1=ON
ON=1
OFF=0
Switch 	Balkong 	"Balkongdörr"	{ mqtt=">[mybroker:/BalkongdorrLamp/gpio/12:command:*:MAP(espeasy.map)],<[mybroker:/BalkongdorrLamp/lightState/Switch:state:MAP(espeasy.map)]" }
Switch 	Fonster 	"Fönster"  		{ mqtt=">[mybroker:/VardagsrumLamp/gpio/12:command:*:MAP(espeasy.map)],<[mybroker:/VardagsrumLamp/lightState/Switch:state:MAP(espeasy.map)]" }
Switch 	Garage 		"Garage"  		{ mqtt=">[mybroker:/UtomhusBalkong/gpio/12:command:*:MAP(espeasy.map)],<[mybroker:/UtomhusBalkong/lightState/Switch:state:MAP(espeasy.map)]" }
Switch 	Kontor 		"Kontor"  		{ mqtt=">[mybroker:/UtomhusKontoret/gpio/12:command:*:MAP(espeasy.map)],<[mybroker:/UtomhusKontoret/lightState/Switch:state:MAP(espeasy.map)]" }

That should do the trick

1 Like

Thank you for your answer.

I guess that the last 4 lines should be changed in default.items?

Here is the log from when item ‘Framdorr’ went in a loop:

2018-08-30 15:35:15.203 [vent.ItemStateChangedEvent] - Kontor changed from OFF to ON
2018-08-30 15:35:21.478 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command ON
2018-08-30 15:35:21.500 [vent.ItemStateChangedEvent] - Framdorr changed from OFF to ON
2018-08-30 15:35:25.492 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:35:25.504 [vent.ItemStateChangedEvent] - Framdorr changed from ON to OFF
2018-08-30 15:35:25.863 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:35:27.661 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command ON
2018-08-30 15:35:27.682 [vent.ItemStateChangedEvent] - Framdorr changed from OFF to ON
2018-08-30 15:35:28.059 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command ON
2018-08-30 15:35:28.697 [ome.event.ItemCommandEvent] - Item 'Kontor' received command OFF
2018-08-30 15:35:28.706 [vent.ItemStateChangedEvent] - Kontor changed from ON to OFF
2018-08-30 15:35:31.660 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:35:31.669 [vent.ItemStateChangedEvent] - Framdorr changed from ON to OFF
2018-08-30 15:35:32.060 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:35:45.129 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command ON
2018-08-30 15:35:45.143 [vent.ItemStateChangedEvent] - Framdorr changed from OFF to ON
2018-08-30 15:35:45.359 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command ON
2018-08-30 15:35:53.659 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:35:53.670 [vent.ItemStateChangedEvent] - Framdorr changed from ON to OFF
2018-08-30 15:35:54.062 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:36:10.638 [vent.ItemStateChangedEvent] - Date changed from 2018-08-30T15:35:10.615+0200 to 2018-08-30T15:36:10.625+0200
2018-08-30 15:36:11.708 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command ON
2018-08-30 15:36:11.717 [vent.ItemStateChangedEvent] - Framdorr changed from OFF to ON
2018-08-30 15:36:13.240 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF
2018-08-30 15:36:13.249 [vent.ItemStateChangedEvent] - Framdorr changed from ON to OFF
2018-08-30 15:36:13.591 [ome.event.ItemCommandEvent] - Item 'Framdorr' received command OFF

Correct

Thank you, Vincent!
Now it works fine, without looping.

Regards,
Harry

That’s great
Did you see the changes I made and do you understand why it was looping?

Hi Vincent,

I have seen the changes, but I do not really understand what was causing the loop.

Right,
Your binding was set like this

Switch xxxx { mqtt=">[broker:topic:command:XX:YY, <[broker:topic:command:XX:YY]" }

The outbound > was correct. React on a COMMAND and send the mqtt message
The outbound < was receiving the mqtt message and then sending a command to the item which in turn will set off the outbound binding which will set off the inbound and so on


This is the reason the item went in a loop when you actionned the switch manually and not from the rules

Does that make sense.

So use the outbound command and the inbound state with mqtt binding

1 Like

Thank you for the explanation, Vincent! Yes, that makes it more clear for me.
Great service!