MQTT, MySensors, OpenHab1 - Is this a rules issue?

I have OpenHab1 running on a Raspberry Pi 2 along with MySensors configured hardware dotted around. They speak over a MQTT connection like normal, however i’m having a little issue with one of my nodes, well maybe two.

So i have a custom made light switch and a relay module with an on board button/switch. At the moment my initial thinking was to have the light switch send a message onto a MQTT topic that has a ‘number’ item assigned to it to either be on or off. Then i would have my relay as another item, but this is a ‘switch’ item that is either on or off.

The items are:

Number CinemaRoom_Switch01 "Light Switch [MAP(]" <shield> (SF_CinemaRoom, gSwitch) {mqtt="<[mymosquitto:Gateway1-out/3/3/1/0/2:state:default]"} Switch CinemaRoom_Light01 "Ceiling Lights" <light> (SF_CinemaRoom, gLight) {mqtt=">[mymosquitto:Gateway1-in/4/1/1/0/2:command:ON:1],>[mymosquitto:Gateway1-in/4/1/1/0/2:command:OFF:0]"}

For this to work, the light switch sends a message on the topic that my relay is assigned to, it receives the message and activates. The rule for this is:

rule "Cinema Room Light Toggle" when Item CinemaRoom_Switch01 changed then if (CinemaRoom_Light01.state == ON){ sendCommand(CinemaRoom_Light01, OFF) } else { sendCommand(CinemaRoom_Light01, ON) } end

As you can see from the rules, i have it set up so that when it receives a message it then toggles the state of the relay, no matter what the current state is. However, this all becomes a mess when i use the on board switch on the relay. It works fine, however, when i come to use the light switch afterwards, the first press/toggle will not work. The second does and any after that does, until i then use the on board switch again. The web interface is acting exactly the same way as the light switch node does, this makes me think that maybe the relay output is not working with my way of configuration on OpenHab, I’m not sure if i need to change my rules config or the MySensors config to correct this.

I’m not 100% sure this is an issue with my OpenHab configuration, but we’re lost on the MySensors community regarding this and thought that maybe its my error with config on the OpenHab side maybe?

If the logs from the MySensors are any help to you, these are the logs from the gateway when it receives its messages from the nodes:

Light Switch Node:
0;0;3;0;9;read: 3-3-0 s=3,c=1,t=2,pt=2,l=2,sg=0:1 0;0;3;0;9;Sending message on topic: Gateway1-out/3/3/1/0/2 0;0;3;0;9;Message arrived on topic: Gateway1-in/4/1/1/0/2 0;0;3;0;9;send: 0-0-4-4 s=1,c=1,t=2,pt=0,l=1,sg=0,st=ok:1

On board switch:
0;0;3;0;9;read: 4-4-0 s=1,c=1,t=2,pt=2,l=2,sg=0:1 0;0;3;0;9;send: 0-0-4-4 s=1,c=1,t=2,pt=2,l=2,sg=0,st=ok:1 0;0;3;0;9;Sending message on topic: Gateway1-out/4/1/1/0/2

OpenHab Web Interface:
0;0;3;0;9;Message arrived on topic: Gateway1-in/4/1/1/0/2 0;0;3;0;9;send: 0-0-4-4 s=1,c=1,t=2,pt=0,l=1,sg=0,st=ok:1

Why? Why not have them both be a switch. I’m pretty sure that if you send a 0 to a switch it will interpret that as OFF and a 1 as ON. I know for sure it works if you change you MySensor to send the string “ON” and “OFF”.

Regardless, When one receives a message it should use postUpdate to update the switch, not sendCommand. When you use sendCommand it passes that command on to the underlying binding which you don’t want. You only want to update the OH internal state of the Switch to match the physical device which is what postUpdate is for.

Also, as you have it now you are assuming that CinemaRoom_Switch01 and CinemaRoom_Light01 are being successfully kept in sync. But since it clearly isn’t and I don’t know that you can ever reliably assume that it is. Also, when you switch your switch from OH does it go out to the MySensors device which then send the change back to CinemaRoom_Switch01? If that is the case you switch it ON, then it gets toggled back to OFF. Therefore I would write your rule as:

NOTE: to properly format code wrap it in three backticks.

your code goes here
rule "Cinema Room Light Toggle"
    Item CinemaRoom_Switch01 changed 
    if(CinemaRoom_Switch01.state.toString == "1") {
    else {

Firstly, thank you for informing me on the 3 backticks, sorry i made the post look untidy, i will change that to suit now.

I could change the number item to a switch i suppose, but i will keep that as it is currently as i don’t think that is having any effect on the problems i face right now. If i get it all sorted and ironed out, i will then change that too.

So, are you saying that you think the issue is the fact that i’m trying to send a command each time i receive a switch toggle from the web interface and the switch node and because of this it is trying to command the switch to turn on or off fine, but if the switch is in that state already through the on board switch, it won’t take effect until the second toggle on the switch node?

Yes, that is basically what I think it happening. The state within OH and the state on the device are ending up out of sync so it is requiring you to trigger it twice from the device to get them back into sync.

Okay, and simply changing what i have now

rule "Cinema Room Light Toggle"
	Item CinemaRoom_Switch01 changed
	if (CinemaRoom_Light01.state == ON){
		sendCommand(CinemaRoom_Light01, OFF)
	} else {
		sendCommand(CinemaRoom_Light01, ON)


rule "Cinema Room Light Toggle"
	Item CinemaRoom_Switch01 changed
	if(CinemaRoom_Switch01.state.toString =="1") {
	else {

would change this in your thoughts?

Does it change the behavior you are seeing?

Do i need to make the changes in the items rule like you suggested to make your new rules work?

I don’t understand your question.

There are Items and there are Rules. You don’t need to change your Items. You only need change the Rules.

In this case, it is not working and has actually stopped working all together now.

I’m restarting OpenHab to see if that forces an update, however since its not working at all now i think it has already updated it.

Restart did nothing

When you trigger the light switch from openHAB (e.g. the sitemap or a rule) do you send the command to CinemaRoom_Light?

The flow should go something like this:

OH Triggered ON:

  1. Sitemap (or Rule) toggles CinemaRoom_Light01
  2. CinemaRoom_Light01’s binding publishes the ON to the proper topic
  3. Your device switches the relay to ON
  4. Your device publishes its new state to the proper MQTT topic
  5. CinemaRoom_Switch01 gets updated with the new state (i.e. 1 assuming 1 == ON)
  6. The rule Cinema Room Light Toggle" triggers and calls CinemaRoom_Light01.postUpdate(ON) (NOTE: CinemaRoom_Light01 should already be ON, the postUpdate prevents the ON command from going out to the device again)

Your flow may stop at step 3.

Manually Triggered ON at the Device

  1. Physically flip switch/press button/etc.
  2. Relay sends ON state to MQTT topic
  3. CinemaRoom_Switch01 receives the message and changes to 1
  4. “Cinema Room Light Toggle” triggers and calls CinemaRoom_Light01.postUpdate(ON)
  5. CinemaRoom_Light01’s state changes to ON to is matches the actual state of the device, but it does not resend a command to turn on the device because of the postUpdate.

If your flow doesn’t match the above then the rule I posted will not work. However, the flow I posted is one of the “correct” ways to keep a controlling switch in sync with changes in state caused by another source.

If you were to modify your device to send “ON” and “OFF” (you can test with 1 for ON and 0 for OFF too) you could eliminate the rule and Number Item entirely and just have an incoming and outgoing mqtt binding config all on the same Item. However, I personally like doing this sort of thing in a rule. I find it easier to figure out what is going on and therefore easier to debug. Others find the all on one line way easier.

1 Like

Firstly, thank you for going into such depth on this with me, really appreciate your time and knowledge. I think its best if i drop my 3 files here:


Number	CinemaRoom_Switch01	"Light Switch [MAP(]"	<shield>	(SF_CinemaRoom, gSwitch)	{mqtt="<[mymosquitto:Gateway1-out/3/3/1/0/2:state:default]"}
Switch	CinemaRoom_Light01	"Ceiling Lights"			<light>		(SF_CinemaRoom, gLight)		{mqtt=">[mymosquitto:Gateway1-in/4/1/1/0/2:command:ON:1],>[mymosquitto:Gateway1-in/4/1/1/0/2:command:OFF:0]"}


sitemap default label="Main Menu"
	Frame label="Cinema Room" {
		Switch item=CinemaRoom_Light01


rule "Cinema Room Light Toggle"
	Item CinemaRoom_Switch01 changed
	if(CinemaRoom_Switch01.state.toString =="1") {
	else {

Let me just get the theory correct, this is what i thought should be happening, please let me know if i’m incorrect in thinking this. This is the process from pressing the light switch node, not the relay switch:

  1. Message from switch gets published on the mqtt topic of Gateway1-out/3/3/1/0/2
  2. This then changes the item “CinemaRoom_Switch01” (which has a mapping file 1 = ON, 0 = OFF)
  3. OpenHab has a rule that when the item “CinemaRoom_Switch01” changes then toggle the light, doesn’t matter what state the switch is or the relay, if it gets a change of state on “CinemaRoom_Switch01” then change the state of the relay

I’m sorry if you feel you have already answered this, I see that you’re assuming that i’v kept everything to how you have previously suggested, but the naming was off and i attempted to correct it to what i thought you meant but it still didn’t work. You can see the rule file above is what i have edited to be after i found some item naming to be incorrect, if it doesn’t match to what it should be please instruct me on what your feeling it should be, obviously you’re more advanced with this side of it than i am.

I fear that my ‘careless’ attitude to the state will not work the way i intend it to. The way i thought would work would to basically say “i don’t care what state the light is, when you get a change in this item (the switch) then change the state of the light/relay”.

That is correct. You have to care what state the relay is in and synchronize your Switch to match it. Just blindly toggling it will get the state of the Switch and the state of the relay out of sync.

That is why I provided the message/event flow above, to illustrate one way it should work.

I’m not trying to be awkward or anything, but i ask this:

Why do you have to have the switch and relay synced? I don’t understand, i’m sorry. I do want it to work like your suggesting, i just want to learn why it needs to be synced.

I’m not sure whats wrong then if i’ve tried what your suggesting to get it working like you want it to and i’m having no luck.

Both your Number Item and your Switch are both a representation of your physical device within OH. As a representative of that physical device, they should reflect the current state of that device (i.e. ON or OFF). Therefore they should be kept in sync. Otherwise you cannot rely on the state of the Number or Switch Item on your sitemap or in your rules. In short, if you don’t keep them in sync OH has no idea what state your light is in.

To put it another way, the purpose of the Switch is for OH to be able to send a command to the relay to change its state (i.e. turn ON or turn OFF). The Number is for the relay to tell OH that the relay has changed its state, whether that change occurred in response to a command from OH or in response to someone physically pressing a button on the device. The only think the Number Item is used for is to update the state of the Switch so it matches the actual state of the relay. The rule is what does this update when the Number Item changes.

Honestly, I’ve spent hours and hours and hours trying my hardest to help people on this forum to get MySensors devices working. I’ve always failed. You’ve made it far farther than most (at least your device and OH can talk to each other) so there is still hope, but I am limited in what I can do to help. I don’t use MySensors and unless you are great at Arduino and everything else around it, it appears to be very difficult to get working right.

I can help with the overall logic but I can’t really help with the lower level debugging.

Right okay, i understand that now, thank you.

As for the MySensors section, there are a few of us that use it well with openhab in the community. I don’t think that the problem lies with the hardware side if i’m honest. I do believe its a config error on my behalf either in the items or rules, probably both knowing me.

Did you check my items and rules that i posted in one of the previous posts? I had to modify your namings, not sure if i got it correct or not.

I agree. The fact that you got messages both directions points to the MySensors part working for the most part.

As well as I can on my phone. I don’t see anything wrong.

When things don’t work I find it best to step back and test each part individually. Go through the steps in the event flow one by one and verify that what is supposed to happen at that step is happening. Watch the logs and add logInfo statements to your rule to help verify each step.

Okay cool, when i get home tonight i will take another look through. Is all the information regarding what commands, triggers and strings etc on the github page or is there some more documentation i could use to varify my coding/instructions are correct?

Everything is either on or linked to (e.g. Xtend documentation) from the Rules wiki page. However, most of that will cover syntax. If you are using Designer (please please please use Designer) syntax errors will not be a problem. Designer will catch those for you as you type.