[SOLVED] Rule on ChannelTriggeredEvent not work

  • Platform information:
    • Hardware: Processore Intel® Core™ i7-6700HQ CPU @ 2.60GHz, 2592
    • OS: Windows 10
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_211-b12)
    • openHAB version: 2.4.0-1

Hello everyone. I’m trying to capture a trigger event on a channel to turn on a light, but I have problems. I built a PIR sensor based on the ESP8266 chip. For each movement raised, send a message to a broker mqtt

This is my configuration

mqtt.things

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" @ "MQTT"[ 
  host="127.0.0.1",
  secure=false,
  port=1883,
  username="<user>",
  password="<pwd>"
]
{
	Thing mqtt:topic:pir_niccolo "Sensore di prossimita' " {
	   Channels:
			Type switch : mqttTrigger "MQTT_Trigger" [ stateTopic="sensor/door/pir", trigger=true]		  
	}
}

pir.rules


rule "Accendi la luce quando rilevi movimento"
when
		Channel "mqtt:topic:pir_niccolo:mqttTrigger" triggered
then
		logDebug("pir.rules", "TEST OK")
end

The trigger event is correctly detected as you can see from the log but I can’t associate a rule with it:

2019-09-01 09:15:25.398 [vent.ChannelTriggeredEvent] - mqtt:topic:pir_niccolo:mqttTrigger triggered {"uptime":"0.04","sensor":"pirOne","state":"OFF","motion":"1"}

2019-09-01 09:15:42.070 [vent.ChannelTriggeredEvent] - mqtt:topic:pir_niccolo:mqttTrigger triggered {"uptime":"0.05","sensor":"pirOne","state":"OFF","motion":"1"}

2019-09-01 09:16:58.687 [vent.ChannelTriggeredEvent] - mqtt:topic:pir_niccolo:mqttTrigger triggered {"uptime":"0.02","sensor":"pirOne","state":"OFF","motion":"1"}

2019-09-01 09:17:16.505 [vent.ChannelTriggeredEvent] - mqtt:topic:pir_niccolo:mqttTrigger triggered {"uptime":"0.02","sensor":"pirOne","state":"OFF","motion":"1"}

Am I wrong?
Thank you

Yes
Why don’t you create the MQTT thing in the PaperUI??

???

Create an item linked to the channel

Switch MyPIR { channel="......." } // examples

Then rule will trigger on:

when
    Item MyPIR changed
then

It’s a new binding feature. You’d need a new binding version to support it. It’s not in 2.4 docs.

1 Like

Coolio, good to know! Thanks
Never used channel triggers anyway

Thank you all for the suggestions. I created the following ITEM

Switch esp_8285_PIR_1 "Living Room Sensor" <motion> (all,sensors,contacts) {channel="mqtt:topic:pir_niccolo:mqttTrigger"}

And this rule:

rule "Accendi la luce quando rilevi movimento"
when
		Item esp_8285_PIR_1 changed
then
		logDebug("pir2.rules", "TEST OK")
end

But it still doesn’t work. I do not find the LOG message “TEST OK”

What MQTT payload does the device send?

Payload:{uptime='0.02', sensor='pirOne', state='OFF', motion='1'}

Perhaps your Item doesn’t change. (This is different from an update.) You can see Item changes in your events.log.

Don’t forget your thing has a parameter that won’t work with MQTT binding 2.4 , but I wouldn’t expect that to stopit working altogether.

Does that include the { and }?
This is not a valid JSON string

You shoud get: {"uptime":0.02, "sensor"="pirOne", "state":"OFF", "motion":1}
When you have that as a payload, you can use a JSONPATH transformation in the MQTT thing to extract the state value:
Just add: transformationPattern="JSONPATH:$.state" to your channel definition

Type switch : mqttTrigger "MQTT_Trigger" [ stateTopic="sensor/door/pir", transformationPattern="JSONPATH:$.stat, trigger=true]

The log I check is as follows:

tail -f /var/log/openhab2/openhab.log /var/log/openhab2/events.log

I removed the trigger option from the THINGS. A WARN now appears in the log file


2019-09-01 17:02:10.382 [WARN ] [eneric.internal.generic.ChannelState] - Command '{"uptime":"0.12","sensor":"pirOne","state":"OFF","motion":"1"}' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.{"uptime":"0.12","sensor":"pirOne","state":"OFF","motion":"1"}

But I still don’t see the expected message

What you see is the toString of the object I receive. This is the code loaded on the ESP8266:

void sendState() {
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();

  uptime = millis() / 1000 / 3600.00;
  root["uptime"] = (String)uptime;
  root["sensor"] = SENSORNAME;
  root["state"] = (stateOn) ? on_cmd : off_cmd;
  root["motion"] = (String)pirState;

  char buffer[root.measureLength() + 1];
  root.printTo(buffer, sizeof(buffer));
  String sPayload = "";
  root.printTo(sPayload);
  char* cPayload = &sPayload[0u];

  client.publish(light_state_topic, buffer, true);

  Serial.println("Published 1: " + String(buffer));

}

OR
get you device to send several messages instead of one with several values.
This is what MQTT was designed for:
Topic: sensor/door/pir/uptime Payload:0.02
Topic: sensor/door/pir/state Payload OFF or ON
Topic: sensor/door/pir/motion Payload:1
Topic:sensor/door/pirsensorId Payload:pirOne

Then your MQTT thing in openhab should have the stateTopic as sensor/door/pir/state
No transformation required

That’s what would do. Use MQTT as designed and lighten the load for openHAB because the sensor did the work of separating the values. The ESP will have no trouble doing that

That’s good this is a valid JSON
Add the transformation as above in your channel definition to extract the $.state from the JSON
A switch channel only accepts ON or OFF as valid payloads

Make sure you intall the JSONPATH transformation in the PAPER UI

This my THINGS:

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" @ "MQTT"[ 
  host="127.0.0.1",
  secure=false,
  port=1883,
  username="",
  password=""
]
{
	Thing mqtt:topic:pir_niccolo "Sensore di prossimita niccolo" {
	   Channels:
			Type switch : mqttTrigger "MQTT_Trigger" [ stateTopic="sensor/door/pir", trigger=true]	
			Type switch : mqttTrigger2 "MQTT_Trigger2" [ stateTopic="sensor/door/pir", transformationPattern="JSONPATH:$.state", trigger=true]			
	}
}

This my ITEM:

Switch esp_8285_PIR_1 "Living Room Sensor" <motion> {channel="mqtt:topic:pir_niccolo:mqttTrigger2"}

This is my Rule:

rule "Accendi la luce quando rilevi movimento"
when
		Item esp_8285_PIR_1 changed
then
		logDebug("pir2.rules", "TEST OK")
end

This is my LOG:

2019-09-01 17:20:53.459 [vent.ChannelTriggeredEvent] - mqtt:topic:pir_niccolo:mqttTrigger triggered {"uptime":"0.16","sensor":"pirOne","state":"OFF","motion":"1"}

2019-09-01 17:20:53.463 [vent.ChannelTriggeredEvent] - mqtt:topic:pir_niccolo:mqttTrigger2 triggered OFF

I add that the state actually never changes but always comes with the value OFF. For me the movement is detected by the arrival of the message on the queue.

Thanks

Then change the rule trigger to received update

Nothing, it still doesn’t work

Post the rule, please

rule "Accendi la luce quando rilevi movimento"
when
		Item esp_8285_PIR_1 received update
then
		logDebug("pir2.rules", "TEST OK")
end

Change the logDebug to logInfo

I don’t think the problem is the log level, because in the logging I find messages at debug level

Post the log, please