MQTT binding items value when the associated Thing goes offline

Hello

I’ve been playing with OH3 MQTT binding and found the very useful feature “Availability Topic”, so using LWT messages openHAB can track the status of a thing. But this feature is opaque to all the UIs.
When the item goes offline associated channels maintain status and items show values, I would like the UI to show either:

A warning flag on the item like not responding (as apple’s HomeKit does)
A type of illegal value, UNDEF, NULL whatever.

When the MQTT binding is restarted all items are set to NULL, but I cannot force this value with any message. Would it be possible with transforms?.

I’ve seen script based solutions, expire is quite clever, but I would prefer something config only and easy to maintain, like using metadata.

On the subject, is ti possible monitor thing statuses and/or generate alerts when they go offline?

Thanks

It’s just a topic you can listen to with an ordinary channel, as well or instead.

        Type switch : Reachable [ stateTopic="tele/sonoff/4ch_Mezz/LWT", on="Online", off="Offline" ]

Linked to a ‘reachable’ switch Item, a rule can act on change to OFF to set any other Item states you like to UNDEF.

If you do not persist/restore those other Items, they will be state NULL at startup until valid data comes along.

Yes that’s a a solution, I would update it to use the Thing status, so no need to declare an auxiliary item. Every use case benefits from a different solution, for example in the case of a sensor I need to check the freshness of the value, the Online status is not enough, so I would use the expire feature. That’s not what Im looking.
In OH3 when you fill the semantic model you are rewarded with a complete UI with zones and equipment. If the default item presentation is not adequate you can add metadata and get sliders, set point, etc.
I’m asking if there is something similar for things and channels, so the UI automatically shows that the actual device is faulty. More a general than a specific solution. I realize that I asked it specifically for MQTT,
If not I would prefer a method to detect and alert for offline devices.

There’s two parts to this.

Maintaining data, detecting/avoiding ‘stale’ data. That’s about Items.

Maintaining a view of external services/devices. That’s technology dependent, and may involve Things and/or dedicated ‘test’ mechanisms like Network ping channels.

“What to do” and “what to do about it” are highly individual decisions, there are lots of posts here about ways to do it. I don’t know what you’re looking for.

It’s not opaque to the Settings → Things UI. When the Things is detected as offline it gets a big red badge next to the Thing on that UI.

Neither of these are illegal and in both cases the UI indicates the Item has one of these two states using “-” so if you see that it means the Item’s state is either NULL or UNDEF.

What do you mean by “force this message”? You can update any Item you want to any supported state, including NULL or UNDEF. Theoretically you can create a transfromation that converts an Item update to become NULL or UNDEF. But that wouldn’t be very useful. The Item only updates when it’s alive and reporting.

??? Expire is metadata.

Yes but it requires rules.

I believe that when the LWT topic and messages are configure on a Thing, when the OFFLINE message is received the binding will set all the linked Items to that Thing to UNDEF. However, this depends on the LWT message for that Thing to be retained and the device needs to publish a retained ONLINE message when it reconnects. Not all devices will do this. So if the device doesn’t support it or if you want to do something other than set the states of the Items to UNDEF a Rule like rossko57 describes is required.

There is no configuration nor customization available for Things and Channels.

But the Things page already does show this, at least to the extent that it’s possible. It’s not always easy for a binding to tell when something is offline on it’s own so often the ONLINE status only means that the communications channel to the device is open and working, not necessarily that the device is working.

For MQTT one can get some more info if the LWT is implemented right. In that case, when the device stops communicating to the MQTT broker we will get an OFFLINE message on the LWT topic. When that happens, if the Thing is configured to use that, the Thing will go OFFLINE and in the UI it will have a big red OFFLINE badge. Furthermore the Items linked to the Channels of that Thing will change to UNDEF. In the Item views for those Items the state will be represented as “-” to show it has an undefined state, either NULL or UNDEF.

The binding is going to the best that it can to mark a Thing as OFFLINE if it’s not reporting. If it can’t do that you need to use something else to determine if the device is offline. If the Item reports periodically Expire is a good choice.

All of these generate events and events can trigger rules which is where you would implement the “what to do”, e.g. send an alert.

Quite right, my fault, sorry for the poorly worded post. I mean the user UI not the admin, you are right that there is big red message for offline things, but I mean location,equipment, habpanel et al.
I don’t care really about the semantics as long the item gets an state outside a “normal” value for example no value is perfect. Habpanel hides on/off status when items go undef. NaN or nothing at all is perfect for numbers.

When a MQTT thing go offline via the availability topic items linked to the channels don’t change, for example they don’t go undef.

You mention that it’s possible to set a item to undef via transform, please can you post a link to an example that would be very useful in a lot of scenarios like some sensors that report null value that is ignored by the channel.

I am not looking for a solution but for nicety. Let me explain, OH2 was blind to to MQTT things status, the were always online. With the new binding you can config an availability topic and you get that nice red badge when the thing goes offline. It would be nice to get the items linked to channel set to undef or nothing at all. As I understand the user UI don’t have any knowledge of things so all must be done via items.

If you ask me, I would choose to have a red badge next to the item In the UI :slight_smile:

Sorry for this long thread.

Make it so with rules, today. Do not force other users to conform to your needs. An optional enhancement might be useful.

but that has nothing to do ith ‘unreachable’

Again, let’s not force other people to do what you want.
Imagine an Item showing “currently calling phone number”. Most of the day, that will be UNDEF (there isn’t one) and this is not unexpected.
You can configure your UI to hide the entry altogether, or show red blobs, or whatever you want.

Many thanks for your advice and I’m sorry to bother you again but I cannot get the undef value with transform.

I have the following thing

UID: mqtt:topic:openHAB:4ff67aa811
label: Generic MQTT Thing
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:openHAB
channels:
  - id: generic
    channelTypeUID: mqtt:number
    label: generic
    description: ""
    configuration:
      stateTopic: stat/generic/value
      transformationPattern: JS:hola.js

the transform is only this

(function(i) {
    return 'UNDEF'
})(input)

and I get “Incoming payload ‘UNDEF’ not supported by type ‘NumberValue’”

What I’m doing wrong?

Nothing. The MQTT binding has a bug in this very specific case.

You can work around that by applying the same transformation in a profile. This applies to the link between channel and Item, operating on the data after it has left the binding/channel but before it gets to the Item.

Things and Channels are not and never will be represented there. Only Items. So if you want to see the status there you need to either link to a status Channel on the Thing (if one exists) or create a rule that triggers when the Thing changes to OFFLINE or ONLINE and update an Item and/or take any other actions necessary.

Well, as we’ve both indicated, NULL and UNDEF are those states. So you need an Item and you need something to change the state of an Item to NULL or UNDEF when the Thing goes OFFLINE. That’s what rules are for if the binding won’t or can’t do that for you.

I also said that doing so would be pointless. A transformation would only run when the Item has received an update. If the Item was updated that means the Thing is ONLINE and working. You can’t run a transformation in the absence of an event, or when it’s been a long time since there was an update.

As always you’re quite correct, I’m mixing too many things (not openhab Things :grin:). It’s for another use case, but many thanks.