MQTT Map transformation

Hello

I have an esp8266 flashed with the Tasmota firmware working ok and is sending messages to openhab, i have a Contact item and when the ON or OFF message is sent from the MQTT it changes from OPEN and CLOSED, can i change this so it displays SET or UNSET as it will follow the state of my intruder alarm?

Thanks

Sure. Use a MAP transform in the Item label. This doesn’t change the state of the Contact Item - it’ll still be OPEN/CLOSED so far as any rules are concerned, etc. - but you can display what text you want in the UI.

Thanks @rossko57

Is this the correct syntax in my items file?

Contact Intruder_Status “Alarm System Status [%s]” (Alarm) { channel=“mqtt:topic:53bacaf3:alarm-status” [profile=“transform:MAP”, function=“alarm.map”]}

the alarm.map is the file i created in the transform folder and looks like the following;

CLOSED = UNSET
OPEN = SET

Thanks

Nope. You cannot set a Contact type Item to state e.g “UNSET” by using a transform in the binding, or a profile.

What I suggested was not to do that, to leave the state alone, because it’s really the display you are interested in. The display is governed by the [format] part of the label in Item (or sitemap).
At the moment you’ve got [%s], meaning show state as string e.g. OPEN
To map OPEN into “SET”, you need to put your MAP in the [ ] part.

If you’ve not done this before you might need to install the MAP transformation service.

Hi @rossko57

I have installed the map transformation service and changed the contact to a string item like so

String Intruder_Status “Alarm System Status [%s]” (Alarm) { channel=“mqtt:topic:53bacaf3:alarm-status” [profile=“transform:MAP”, function=“alarm.map”]}

Then my map is;

ON = SET
OFF = UNSET

Its now working,thanks for the advice

Regards

1 Like

Okay, it’s not what you asked for.
You might want to add a line for UNDEF to your map, to catch exceptions.

@rossko57

I did try the above with a contact item but it wouldn’t change? i the tried it as a string and its working.

to add the UNDEF will that be an empty value so it accepts any other string it receives and displays UNDEF? i.e

" " = UNDEF

No, if the binding encounters a problem (a communication failure for example) it will set the state to UNDEF. That will then throw an error because it cannot look up UNDEF in your original map file.
So you might add a line like
UNDEF = Unexpected problem
or whatever you want your string to be. You might be happy with UNDEF, so
UNDEF = UNDEF

MAP in label [format] works just fine for many years.

Perhaps you’re viewing with PaperUI. Remember that is an administrator’s toolkit, allowing you inspect Items. It’s not intended as a user UI.

@rossko57

Apologies i see what you mean by the map in the label now sorry new to this just trying to pick things up as i go along.

I could have done " Alarm System Status [MAP(alarm.map):%s]" would that have been correct?

I will add the UNDEF to my map now, thanks again

Yep, that’s the ticket :smiley: browsers etc. can need a refresh to pick these things up, it can be a nuisance.

More fiddly bits … if you do use it in label display (as opposed to channel/binding side) then you may need to deal with NULL state too, where the system has been loaded but no update has yet taken place. Sometimes, the system seems to do a “pre-transform” I’ve never really understood and substitute “-” for NULL.
So in my own display maps I just put both
NULL = uninitialized
- = uninitialized
This isn’t needed for profile/binding side use.

I was testing this in the app not the browser though? anyway its working thanks for the patience.

@rossko57

I have changed it over to the below now if that’s the way most people do it.

[MAP(alarm.map):%s]