Including mapping in item depending on state of another item

I hope I’m explaining this right:

I’ve got an Item which when updated, sends the new value over MQTT to a remote unit.
If another Item changes to a a particular value over incoming MQTT (from the remote unit), I want the first Item to include a mapping file in subsequent updates over MQTT.
I was thinking of having three items (four if you include the one dealing with incoming MQTT) - one including the mapping file in the item definition and the other not and the last being the one that sends out MQTT commmands
When a rule is triggered (via the incoming MQTT), it would update the outgoing MQTT item to accept either the Item value of the item with the mapping or the one without, depending on the state of the incoming item value.

To explain what this is - it’s an LCD panel that gives information pertinent to that day and when a switch is pressed, it changes the language from English to German (hence the mapping file).

It all works apart from the mapping transformation.

What I basically need to know is how to change the value of a string item to that of another string item with mapping so the mapping is included in the first string file.

This is a rough idea of my files

String ItemWithoutMapping 
String ItemWithMapping "[MAP(de.map):%s]"

String OutgoingItem     {mqtt=">[broker:house/LCD1:command:*:default]" }
rule "Mapping test"
when
	Item TestSwitch2 changed
	
then
	if(TestSwitch2.state == ON) {

		OutgoingItem.sendCommand(ItemWithMapping.state.toString)
	 }

	 else {

		OutgoingItem.sendCommand(ItemWithoutMapping.state.toString)
	}

end

I’ve tried with variables in the rules instead and I think this is the way to do it but I couldn’t get it to work.

Any ideas?

Thanks!

Using the MAP in the label only changes how it is displayed on your sitemap. The Item’s actual state is still the unmapped state.

Without more details I can’t be certain, but I think you can eliminate the Mapping Item entirely. On your sitemap you would have:

    Text item=ItemWithoutMapping
    Text item=ItemWithoutMapping label="[MAP(de.map):%s]"

This will show the Item both ways, without the mapping and with the mapping.

In your Rule you can call the transformation:

rule "Mapping test"
when
	Item TestSwitch2 changed
then
	if(TestSwitch2.state == ON) 
		OutgoingItem.sendCommand(transform("MAP", "de.map", ItemWithoutMapping.state.toString))
	 }

	 else {

		OutgoingItem.sendCommand(ItemWithoutMapping.state.toString)
	}
end    

Does that meet your requirements?

1 Like

Thank you Rich. that works perfectly.

I ignored the sitemap as I’m only using HABpanel and my ItemWithoutMapping is actually a variable (I originally thought I would have to put it’s value into an item to do the transformation but your way is much more refined!

As other status changes are sent as well as the language change, I’m using switch-case so for completeness sake, this is what I ended up with:

rule "bin LCD lang change"

when
	Item LCD1status changed
then

 switch LCD1status.state {

	case "E": OutgoingItem.sendCommand(myVariable.toString)
	case "D": OutgoingItem.sendCommand(transform("MAP", "de.map", myVariable.toString))

 }
 end

Which is far simpler than what Is I started with.