Problem creating rule for MQTT

  • Platform information:
    • VM, QEMU Virtual CPU version 2.5+
    • OS: Fedora Linux 35 (Server Edition)
    • Java Runtime Environment: java-11-openjdk 1:11.0.13.0.8-2.fc35
    • openHAB version: openhab 3.2.0-1

Hello,

I have been using openHAB for some time now (about 2 Years) and is so far happy with it, but this is my first post here.
I have some configurations in text-files (Eg. items & rules) that was migrated from openHAB2 to my current openHAB3 confuguration, other things are only in openHAB’s internal DB.

Now I wanted to start using MQTT for some homemade hardware (Wateralarm).
This is how the message from the device looks like
{"sensor":"Wateralarm","time":1640868995,"status":"OK"}
“sensor” is just a label
“time” is the last updated time in seconds since epoch
“status” is the status of the sensor

But I have some problems creating “usable” rules for it.
Here is what I have done:

  1. Install MQTT-Binding
  2. Configured my mosquitto-MQTT-Broker (not the “System MQTT Broker”) as a Thing
UID: mqtt:broker:b98cdf62cd
label: MQTT Broker
thingTypeUID: mqtt:broker
configuration:
  lwtQos: 0
  publickeypin: false
  clientid: a0d03faa-eaba-4324-9585-cc629b212a33
  keepAlive: 60
  secure: false
  certificatepin: false
  password: xxxxxxxxxx
  qos: 0
  reconnectTime: 60000
  host: ###.###.###.###
  lwtRetain: true
  enableDiscovery: true
  username: xxxxxxxx

  1. Created a Thing (Wateralarm) with two channels:
UID: mqtt:topic:b98cdf62cd:0f40334c67
label: Wateralarm
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:b98cdf62cd
channels:
  - id: Watchdog
    channelTypeUID: mqtt:datetime
    label: Watchdog
    description: ""
    configuration:
      qos: 0
      stateTopic: basement/wateralarm/state
      transformationPattern: JSONPATH:$.time
  - id: Status
    channelTypeUID: mqtt:string
    label: Status
    description: ""
    configuration:
      allowedStates: OK,WARNING,CRITICAL,ERROR
      qos: 0
      stateTopic: basement/wateralarm/state
      transformationPattern: JSONPATH:$.status
  1. In the events.log I can see that openHAB recieves the messages:
2021-12-30 13:28:59.980 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Wateralarm_Watchdog' changed from 2021-12-30T13:26:41.925616+0100 to 2021-12-30T13:28:59.977570+0100
2021-12-30 13:31:18.036 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Wateralarm_Watchdog' changed from 2021-12-30T13:28:59.977570+0100 to 2021-12-30T13:31:18.032917+0100
  1. I have started to create a rule (and here is where my problem starts…)
configuration: {}
triggers:
  - id: "2"
    configuration:
      cronExpression: 0 * * * * ? *
    type: timer.GenericCronTrigger
  - id: "3"
    configuration:
      itemName: Wateralarm_Watchdog
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-
        logInfo("Wateralarm_Watchdog", "Triggered Status:" + Wateralarm_Watchdog);
    type: script.ScriptAction
  1. I can see the logentries in openhab.log
2021-12-30 13:28:59.983 [INFO ] [ore.model.script.Wateralarm_Watchdog] - Triggered Status:Wateralarm_Watchdog (Type=DateTimeItem, State=2021-12-30T13:28:59.977570+0100, Label=Watchdog, Category=, Tags=[Point])
2021-12-30 13:29:00.877 [INFO ] [ore.model.script.Wateralarm_Watchdog] - Triggered Status:Wateralarm_Watchdog (Type=DateTimeItem, State=2021-12-30T13:28:59.977570+0100, Label=Watchdog, Category=, Tags=[Point])
2021-12-30 13:30:00.878 [INFO ] [ore.model.script.Wateralarm_Watchdog] - Triggered Status:Wateralarm_Watchdog (Type=DateTimeItem, State=2021-12-30T13:28:59.977570+0100, Label=Watchdog, Category=, Tags=[Point])
2021-12-30 13:31:00.877 [INFO ] [ore.model.script.Wateralarm_Watchdog] - Triggered Status:Wateralarm_Watchdog (Type=DateTimeItem, State=2021-12-30T13:28:59.977570+0100, Label=Watchdog, Category=, Tags=[Point])
2021-12-30 13:31:18.039 [INFO ] [ore.model.script.Wateralarm_Watchdog] - Triggered Status:Wateralarm_Watchdog (Type=DateTimeItem, State=2021-12-30T13:31:18.032917+0100, Label=Watchdog, Category=, Tags=[Point])

Now for my problem: How can I “access” the information in the status-message?
I want to be able to compare the DateTime in the “message” with the current DateTime to be able to detect a “stale” device (the DateTime in the message are more then 10 Minutes old).

Any suggestions or a link to an up2date dokumentation?

Regards,
Dan Johansson

For each information (sensor, time, status) in your message you need to specify an own channel and link an item to this channel, then you can build a rule with the item.

Btw: why has your current rule a cron trigger. This should be avoided normally

Yes, that is what I have (or at least think I have).
One item for “Status” and one for “Watchdog”.


Dan

The cron-trigger is there to unsure that the rule runs when no events from the device comes (device is offline). Or does openHAB have an other method to check for missing events?


Dan

Sure. Because there is a large variety of ‘missing’ events, there is no generic method.
A bunch of approaches are outlined here and elsewhere -

Its the detection techniques of interest here, not the Thing related stuff.

As a general comment, you’re not really interested in the timestamp that the device sent to you - it might not even be in the same timezone. You just want to know if any update did not arrive in the last N minutes.
Expire function is useful for that - applied to an Item, you can have it set state to UNDEF if no updates arrive.
A rule can easily be triggered from change-to-UNDEF to wave flags or whatever you wanted to do.

If you already have an item linked that is showing status = OK, I am not sure if I have understand your issue or if you still have an issue to miss some data.

Thanks for all feedback.
Especially the tip with the Expire function (I had not used this before).
That works like I wanted, without me writing a bunch of script code. :grinning:

Once again thanks for you support and insight.


Dan