Mqtt binding + triggered/updated/changed + rules

I have a motion sensor setup in my things file

Thing topic BedroomRepeaterNode
	{
		Channels:
			Type switch : MotionSensor "Bedroom Motion Sensor" [stateTopic = "mygateway1-out/200/1/1/0/16", on="1", off="0"  ]
    }

I was trying to get my motionsensor rule to fire. The log kept saying when motion was detected that the corresponding item (below)

Switch MasterBedroom_MotionSensor_raw (gMotionSensorsRaw) { channel = "mqtt:topic:broker1:BedroomRepeaterNode:MotionSensor"  }

Had changed from 0 to 1. and so on.

So I had a rule setup. No matter what though, I couldn’t get said rule to trigger. I tried using changed, updated, updated from [] to [], command ON ect. I even added all the different variations with this or that or this other thing.

Nothing.

But I did figure out that if I added the trigger=true i could get something what would fire.

Thing topic BedroomRepeaterNode
	{
		Channels:
			Type switch : MotionSensor "Bedroom Motion Sensor" [stateTopic = "mygateway1-out/200/1/1/0/16", on="1", off="0", trigger=true  ]
    }

So then my rule became

rule "testing MotionSensor changed"
when
   Channel "mqtt:topic:broker1:BedroomRepeaterNode:MotionSensor" triggered 1 
then

This worked. I was able to finish my rule and do more testing. Now, I am trying to stick to using design patterns. So I am trying to bring this in to my group rule.

rule "Raw Motion Sensor State Changed"
when
    Member of gMotionSensorsRaw received update  // from OFF to ON
then

So there is the problem… i don’t know if channel triggers can be grouped?..

Should i go back to just triggering on item changes? If so… is there something I am missing with the mqtt binding not triggering a rule to fire?

Thanks!

Maybe. It’s the Item that is important, so look in the events.logs for what it is doing.
For example, you mentioned a Switch type Item changing 0/1, but Switch types don’t do that, they’re ON/OFF

So here is my item, log when it changed and the rule (which unfortunately did not trigger)

Thing

Thing topic BedroomRepeaterNode
	{
		Channels:
			Type switch : MotionSensor "Bedroom Motion Sensor" [stateTopic = "mygateway1-out/200/1/1/0/16", on="1", off="0"  ]
	}

Item referencing thing

Switch  testing_Motion    { channel="mqtt:topic:broker1:BedroomRepeaterNode:MotionSensor"}

Log entry when i walked in front of sensor

2019-10-20 13:08:14.092 [vent.ItemStateChangedEvent] - testing_Motion changed from OFF to ON

Rule that didn’t fire :confused:

rule "testing MotionSensor changed"
when
    Item testing_Motion changed to ON
then
    logInfo("Testing Motion Rule", "IM HERE!!!")
end  

I have these test items setup now so I can really muck it up now lol

Updated rule condition to

rule "testing MotionSensor changed"
when 
    Item testing_Motion changed
then
    logInfo("Testing Motion Rule", "IM HERE!!!")
end

Log shows item changed from OFF to ON but still no entry saying IM HERE

2019-10-20 14:03:59.089 [vent.ItemStateChangedEvent] - testing_Motion changed from OFF to ON

Have you looked in openhab.log to see if your rule file loads?
Make sure there is only one rule (system wide) named “testing MotionSensor changed”

I am actively watching the openhab.log and events.log To ensure there is no duplicates I have renamed the rule to

rule “abc testing MotionSensor changed”

The rule is not firing. I have only been able to get it to fire when i used Channel triggered.

The item changing is just not firing.

==> /var/log/openhab2/openhab.log <==
2019-10-20 14:12:10.853 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test.rules'

==> /var/log/openhab2/events.log <==
2019-10-20 14:12:13.380 [vent.ItemStateChangedEvent] - testing_Motion changed from ON to OFF
2019-10-20 14:12:24.597 [vent.ItemStateChangedEvent] - testing_Motion changed from OFF to ON
2019-10-20 14:13:07.145 [vent.ItemStateChangedEvent] - testing_Motion changed from ON to OFF
2019-10-20 14:13:17.811 [vent.ItemStateChangedEvent] - testing_Motion changed from OFF to ON

But no entry saying “im here”

Initially I only wanted it to fire when changed from OFF to ON. But now i have it set to fire on any change and still nothing.

even with

rule "abc testing MotionSensor changed"
when 
    Item testing_Motion changed or 
    Item testing_Motion received command or
    Item testing_Motion received update
then
    logInfo("Testing Motion Rule", "IM HERE!!!")
end

I still get nothing

==> /var/log/openhab2/openhab.log <==
2019-10-20 14:16:19.019 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test.rules'

==> /var/log/openhab2/events.log <==
2019-10-20 14:16:26.335 [vent.ItemStateChangedEvent] - testing_Motion changed from ON to OFF
2019-10-20 14:17:30.740 [vent.ItemStateChangedEvent] - MasterBedroom_LightLevel changed from 70 to 72
2019-10-20 14:17:30.741 [vent.ItemStateChangedEvent] - testing_AmbientLight changed from 70 to 72
2019-10-20 14:17:36.907 [vent.ItemStateChangedEvent] - testing_Motion changed from OFF to ON
2019-10-20 14:17:45.712 [vent.ItemStateChangedEvent] - MasterBedroom_LightLevel changed from 72 to 77
2019-10-20 14:17:45.713 [vent.ItemStateChangedEvent] - testing_AmbientLight changed from 72 to 77

Well, of course it works for everybody else. I would suggest a restart of openHAB, it’s not unknown for things to get in a knot after in flight editing.

Yeah I thought maybe that samething, especially with the mqtt binding. So i have done a few restarts today. Even a full reboot of the box about an hour ago.

Ill keep poking away

Cause a different visible event

rule "testing MotionSensor command"
when 
    Item testing_Motion changed to ON
then
    testing_Motion.sendCommand(OFF)
end

Your binding does not generate commands, of course, so there won’t be any triggers off that.

That doesn’t trigger either. So far, the only way I have been able to get this rule to trigger is either using the channel trigger. or if i do a manual curl command and send a command to the item. But the item being updated from the mqtt binding is just not triggering it.

Okay, make a new Item. Put it on your sitemap as a Switch. Write a new rule that responds to a change. All new mind, no editing your old ones.
When that works as seen in your logs, add the zwave channel to your new Item and see if that ackles. (you can have more than one Item linked to the same channel)

I suspect something screwy with your Item, like a definition from both PaperUI and from xxx.items file.

this isn’t zwave. this is mqtt.

I have created new configs for testing this.

Switch NewTest_Item { channel=“mqtt:topic:broker1:BedroomRepeaterNode:MotionSensor”}

rule "new item test command"
when 
    Item NewTest_Item changed 
then
    logError("Another test", "NewTest_Item")
    
end

Send 1/0/ON/OFF to the topic, log says it updated the item. but the rule does not execute

==> /var/log/openhab2/events.log <==
2019-10-20 17:47:29.936 [vent.ItemStateChangedEvent] - NewTest_Item changed from OFF to ON
2019-10-20 17:47:40.239 [vent.ItemStateChangedEvent] - NewTest_Item changed from ON to OFF
2019-10-20 17:48:59.622 [vent.ItemStateChangedEvent] - NewTest_Item changed from OFF to ON

I know other rules are firing. But they aren’t being triggered by items using mqtt.

Just seems like the mqtt binding state topic isn’t actually triggering rules. If i do a curl cli command and send the command of ON/OFF/1/0 the previous rule would fire.

I have even removed and reinstalled the mqtt binding.

This is not comparable to what the MQTT binding should do, so it doesn’t help.
In general, commands with openHAB are issued by UI or rules and the bindings process those on towards realdevices.
Data coming in from real devices is processed by bindings into updates for Items. (with some exceptions)

My suggestion was to make a new Item with no MQTT linkage at all and satidfy yourself you can trigger a rule from UI. So that you know the Item and the rule are good.
Only then to add your channel. It’s a diagnostic process to narrow your problem down, but of course you may follow any track you wish.

If a binding channel (MQTT or anyone else) causes a successful Item state change, it seems difficult to blame the binding for missed events.

I have no idea if this is how it is supposed to be. But I did finally get it to trigger. I had to add “postCommand=true” to the things file entry.

Thing topic BedroomRepeaterNode
	{
		Channels:
			Type switch : MotionSensor "Bedroom Motion Sensor" [stateTopic = "mygateway1-out/200/1/1/0/16", on="1", off="0" , postCommand=true ]
    }

Then update my rule

rule "new item test command"
when 
    Item NewTest_Item received command ON
then
    logError("Another test", "NewTest_Item")
    
end

Now when the MQTT topic receives the update it triggers the rule now.

2019-10-20 18:39:42.595 [ome.event.ItemCommandEvent] - Item 'NewTest_Item' received command ON
2019-10-20 18:39:42.601 [nt.ItemStatePredictedEvent] - testing_Motion predicted to become ON

==> /var/log/openhab2/openhab.log <==
2019-10-20 18:39:42.595 [ERROR] [.smarthome.model.script.Another test] - NewTest_Item

I don’t know if that’s how it should be. Seems kind of odd, definitely seems like a state change from a sensor would be enough to actually trigger rules but for some reason it was not. I even upgraded from 2.4 to 2.5 to see if maybe something was just weird and got the same behavior.

Might have to spin up a docker image later of openhab and throw my configs in it to see if it’s the same there or if something is really wrong on my OH machine.

I have other bindings and what not that are triggering rules no problem. I see that stuff in the logs already.

But ill give this route a go as well.

What appears to be missing here (but perhaps just left off) is a change to the Item, as a consequence of autoupdate’s prediction. Perhaps it was already ON in this case.

If you are seeing changes, add yet another rule triggered from changed.

Well, not really. While MQTT binding does allow for this “command forcing” it is really intended for something like a keypad, where the usage is more like a UI “giving instructions” to openHAB.
The ordinary way of ‘stateTopic’ resulting in an update is meant for “device status” reporting.

Item is still the same

Switch NewTest_Item { channel=“mqtt:topic:broker1:BedroomRepeaterNode:MotionSensor”}

My sensor reports in current state every 5 seconds. So when it sends “0” i see it shows it’s off in mqttspy. I then send a message right behind it of 1 and prior to adding the postCommand=true it would show that the item updated. But the rule did not trigger. If you look at my previous logs above you will see I was sending info to update it to OFF then to ON then to OFF and nothing triggered.

I have not changed the item, but changed the trigger rule to now treat it as a command and with the postCommand=true when the 1 comes in its sending a the command to the item to be on. Just like what I was doing with curl.

I agree, i don’t think it should be like that. But for whatever reason the rules will not fire with the updated status. Even when I said don’t care if it’s updated to on or off, but ANY update write to the log, it still did not every trigger. This is the only way I have been able to get it to work.

I have a docker image spinning up right now pointed at the same configs :slight_smile: Lets see what it says about all this lol

Can you see your Thing with its channel in PaperUI? Is it actually ID as above, or
“mqtt:topic:BedroomRepeaterNode:MotionSensor”