[SOLVED] How to translate a sensor value (number) to text using map transformation

Reading the documentation on the map transformation service and searching the forum didn’t give me a clear answer on how to setup a simple number to text mapping for a sensor item.

The case: I read a value from an MQTT message (DSMR meter tariff) which returns 1 or 2, and I want to display that on a sitemap as “Low” or “High”. Since it is not a switch or selection, I cannot use the mapping parameter (it wouldn’t make sense to be able to change the selection from Low to High for instance).
I found some clue in the map transformation documentation but don’t really know what to fill in at the Channel ID parameter.

I read the value from MQTT message with:

Number PowerTariff "Huidig Tarief [%d]" {mqtt="<[mosquitto:sensor/dsmr:state:JSONPATH($.power.tariff)]"}

So, I figured something like this would do the trick, except that I don’t know what to fill in at the channel parameter:

String TariffTxt "Tarief" { channel="What???" [profile="transform:MAP",function="tariff.map",sourceFormat="%d"] }

Of course, I have created a tariff.map file with:

1=Low
2=High
2 Likes

Put the mapping in the item label:

Number PowerTariff "Huidig Tarief [MAP(tariff.map):%s]" {mqtt="<[mosquitto:sensor/dsmr:state:JSONPATH($.power.tariff)]"}

Then in your sitemap:

Text item=PowerTariff

Thanks! That was easy… ;o)
I got a little confused about the Number item type, but I guess it’s about the input value and not the output, right?

Coolio, please mark the thread as solved, thanks
hc_292

@noppes123
I followed your solution for doing kind of the same in my case, but it does not work.
I hope you can help me.
Situation: From my smart meter, I get tariff info in number format (0001 or 0002)
image
I would love to replace the numbers by text: 0001 should be ‘Day’ and 0002 should be ‘Night’.
I have, as you mentioned, put the mapping in the item label:

Number Elektriciteit_TariffIndicator “Huidig Tarief: [MAP(tariff.map):%s]” { channel=“dsmr:electricity_emucs_v1_0:1:emeter_tariff_indicator” }

Entered in my sitemap:

Text item=Elektriciteit_TariffIndicator

And created tariff.map with this inside the file:

0001=Day
0002=Night
NULL=Unknown

But the result I get is this:
image

What am I doing wrong here?

Remove the : between Tarief and [MAP like below and check your " as they are not correct.

Number Elektriciteit_TariffIndicator "Huidig Tarief [MAP(tariff.map):%s]" { channel="dsmr:electricity_emucs_v1_0:1:emeter_tariff_indicator" }

Hi @H102,
thanks for the fast reply!
I have removed the : as you said so the item is as following:

Number  Elektriciteit_TariffIndicator   "Huidig Tarief [MAP(tariff.map):%s]" { channel="dsmr:electricity_emucs_v1_0:1:emeter_tariff_indicator" }

But this seems not to change a lot, this is the result:
image
Any idea?

Anything in the logs that indicated what the issue is?

No, not really…
I have been searching on this for some time now…

When I change the item from ‘Number’ into ‘String’,

String Elektriciteit_TariffIndicator "Huidig Tarief [MAP(tariff.map):%s]" { channel="dsmr:electricity_emucs_v1_0:1:emeter_tariff_indicator" }

I do get a value, but then the result is this:
image

Leave the item as a Number and move the MAP to your sitemap.

Item:

Number Elektriciteit_TariffIndicator "Huidig Tarief" { channel="dsmr:electricity_emucs_v1_0:1:emeter_tariff_indicator" }

Sitemap:

 Text item=Elektriciteit_TariffIndicator label="Huidig [MAP(tariff.map):%s]"

You have installed the MAP transformation service?

You have installed the MAP transformation service?

Yes, I can tell You I certainly have installed it :wink:

Hi,

I have moved the MAP to my sitemap and the result is following:
image
It seems it does not receive a value, as it shows NaN

Is your Item now a Number or a String type? Look in your events.log to see what states it is taking up.
When it is a Number, it won’t be 0001, it’ll be just 1 when converted to a string for lookup in the MAP. You can have both 1= and 0001= in your map file.

It seems it is a String type, and not a number type.
So I presume I should change this in the item?

It doesn’t really matter if you’re going to MAP / translate it anyway. Just decide which you’re going to do, it’s your Item.

Well, like I said, I want a text to be displayed instead of the string that by default is shown.
So instead of 0002, it shoud show: Night

When changing the item from number to string; it shows following:
image
So no transforming from number to text done :frowning:

Not sure why MAP is not working correctly but as a work around you can use a rule with a proxy item to post Night/Day on sitemap.

What we don’t know with all the changes and suggestions is what you have now.
Please show us -
Item definition
Sitemap line
An events.log entry showing a state change of current Item.

You should have error messages in your openhab.log at least relating to earlier attempts.

Found it!
Was searching around other posts and found this:

This person said:


So following this; I made some changes:
Changed the item (added prifole, function, etc):

String Elektriciteit_TariffIndicator "Huidig Tarief" { channel="dsmr:electricity_emucs_v1_0:1:emeter_tariff_indicator" [profile="transform:MAP", function="tariff.map", sourceFormat="%s"] }

Changed back the sitemap:

Text item=Elektriciteit_TariffIndicator

and now I get the wanted result:
image

Thanks to you guys @rossko57 and @H102 !
Would not have found it without your help!! :slight_smile:

1 Like