Is there something like expire for things?

I have several generic MQTT things that do not provide ‘Availability’ topic, so once OH has received some data from, they will be shown as ONLINE forever.
What is the easiest way to automatically mark things that did not send anything for, say, 24 hours, as OFFLINE?

For items, I use the Expire binding to set them us UNDEF, but this does nothing for the online status of the thing.

I guess I could write a rule that triggers on thing update and starts a 24-hour timer for marking the thing as offline (assuming it is possible) - but that would mean one rule per thing, which is not very practical…

1 Like

Sounds like a proper candidate for feature request. To my knowledge easiest path would be mqtt binding itself, because it has knowledge of all channels assigned to a thing and can provide a “watchdog” which will mark it offline.

There is no generic functionality such that in openHAB itself. In theory it could be done through profile which lives at link level between channel and item.

Not really. You could easily combine this into one rule. You’d need one trigger per Thing though but the rule itself can be generic. The event passed into the rule will tell you which Thing triggered the rule.

However, for now, Things don’t generate events every time there is communication between it and the end device. For that you’d need a Channel linked to an Item and you can put all those into a Group and even get away with only the one trigger. You could even one of the rule templates to detect when events stop happening for a long time and call a rule you write to handle it, even further reducing the amount of code you need to write.

You could use Open Reminder [3.3.0;3.4.9) - #5 by tdlr (OH 3) or Threshold Alert and Open Reminder [4.0.0.0;4.9.9.9] (OH 4) detect when an Item linked to a Channel on the Thing to call a rule you write to send your alert or handle the case where it hasn’t updated in a given amount of time. All you’d have to write is a Script or rule to do the alerting/setting the Thing to OFFLINE, or what ever makes sense.

I do this with most of my home automation sensors.

For example, I’m on OH 4 so I use the second link. I put one Item that updates frequently from each sensor Equipment into a Group called Sensors. I’ve configured the rule created from the template as follows:

configuration:
  dndEnd: 08:00
  rateLimit: ""
  invert: true
  initAlertRule: new-sensor-status-proc
  dndStart: 20:00
  alertRule: new-sensor-status-proc
  endAlertRule: ""
  thresholdState: UnDefType
  defaultRemPeriod: ""
  operator: ==
  hysteresis: ""
  reschedule: true
  namespace: sensorStatus
  gkDelay: 250
  defaultAlertDelay: PT15M
  group: Sensors

That config will call a rule new-sensor-status-proc when a member of Sensors remains in a state for 15 minutes or more or when the Item changes after new-sensor-status-proc was previously called. You could change the trigger to the rule to received update if you want to count updates as well as changes.

There’s more stuff there (it will wait until 08:00 if an alert occurs after 20:00 to send the alert, the time can be overwritten on the Item level using sensorStatus Item metadata, etc.).

The rule gets called with the name of the Item that timed out or came back. along with some additional information that can be used by the called rule.

My new-sensor-status-proc rule looks like this:

configuration: {}
triggers: []
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        var equipment = actions.Semantics.getEquipment(items[alertItem]);

        var statusItem = items[equipment.name+'_Status'];

        if(equipment === null || statusItem === null) {
          console.warn(alertItem + ' does not belong to an equipment or equipment doesn\'t have a Status Item!');
          false;
        }

        else {
          var statusItem = items[equipment.name+'_Status'];
          console.debug('Sensor status reporting called for ' + alertItem + ', equipment ' + equipment.label + ', is alerting ' + isAlerting + ', and is initial alert ' + isInitialAlert);
          // Sensor is back online                          Sensor went offline
          (isInitialAlert && statusItem.state == 'OFF') || (!isInitialAlert && statusItem.state != 'OFF');
        }
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript
      script: >
        var {alerting} = require('rlk_personal');

        var logger = log('Sensor Alert');

        var equipment = actions.Semantics.getEquipment(items[alertItem]);

        var statusItem = items[equipment.name+'_Status'];


        if(!isInitialAlert && statusItem.state != 'OFF') {
          statusItem.postUpdate('OFF');
          alerting.sendAlert('Offline: ' + equipment.label + ' has stopped reporting', logger);
        }

        else if(isInitialAlert && statusItem.state != 'ON') {
          statusItem.postUpdate('ON');
          alerting.sendAlert('Online: ' + equipment.label + ' is back', logger);
        }
    type: script.ScriptAction

It’s pretty simple. I have a condition to make sure that the Item is a member of an equipment. If it is an alert gets sent saying it’s stopped responding or has returned and a status Switch Item that is also a member of the Equipment gets set to ON/OFF as appropriate.

Thanks for the answer (and sorry for the delay).

However, I am not sure that it answers my question: I wanted to a way to change the online status of THINGS - I do not like seeing things listed as ONLINE in PaperUI even if I know that they are offline.
Actually, I am now pretty sure that what I want cannot be done in a script, since thing status seems to be read-only (by getThingStatusInfo())

The only path I can see (specific to MQTT things) is very convoluted and includes steps that may not be possible:

  • make a set of items, one for each thing of interest
  • if the item becomes UNDEF (for ex. using Expire):
    1. get the thing (is there a better way than going via REST API?)
    2. if the thing’s status in ONLINE
      a. somehow get the thing’s availability topic
      b. somehow post “OFFLINE” to that topic

You can disable the Thing.

The approach outlined above still holds. The only difference is that instead of setting a Switch Item and sending an alert, publish an OFFLINE message to an MQTT topic.

Just set up a convention for the status topic that lest you generate the topic to publish to based on the name/label/metadata of the status Item using the MQTT’s publish Action. Then of course, configure the MQTT Thing to monitor that MQTT topic for it’s online/offline status.

The tricky part is that you’ll have to publish the ONLINE message when the Thing comes back online but how will OH know? The Thing won’t receive messages while the Thing is marked as OFFLINE I think. So you’ll need some other way to detect when the device comes back online.

Thanks Rich, good points as always.

What about RFXCOM binding? While MQTT supports online status (so the things with wrong status are due to wrong implementation by devices), I did not find anything like this for RFXCOM devices - I checked settings for both things and the bridge.

I have several old sensors and it looks like once OH has seen them, they are marked as ONLINE as long as the bridge is ONLINE - even if a couple of them have stopped reporting some time ago…
Anything I can do about this, or should I just learn to ignore the Thing status and rather focus on items, with warning about UNDEF in the way you have shown above?

Ultimately, the Thing’s status has only ever been intended to indicate that the communication path between OH and the device is up and working. Anything more than that is at the discretion of the binding author and largely driven by the technology in question. Since, as I understand it, RFXCOM is one way communication there really is not any way for the binding to know whether or not the device is offline or not. So as long as the bridge is up and working, all it can really do is assume that all the Things under that bridge are also up and working.

NOTE: I don’t use RFXCOM so I’m making assumptions.

I think this is the best approach. Once you have the status in an Item you can do stuff like show it on your MainUI pages, use it in rules, persist and chart it, etc. Heck, you could even use that status Item to change the icon and/or color of all the other Items served by that device or even hide controls from a page when it’s offline. You can do all that with a Thing’s status alone.