Problem with a dynamic icon in a Switch

I am trying to figure out why dynamic icons do not work with a Switch, using MQTT binding.

I want to have a switch which would have a dynamic icon displaying the status of my device (which is received via a separate MQTT channel). I believe the following code is written correctly, but it doesn’t work (the icon doesn’t change when the status received via MQTT changes):

 Switch item=Clear_case mappings=[ON="Clear case"]

Switch Clear_case "" <casestatus> {mqtt="<[broker:3d_printer/clear_case/status:state:default],>[broker:3d_printer/clear_case/switch:command:ON:1]"}

I did create casestatus.png, casestatus-0.png, and casestatus-1.png inside icons folder. The MQTT incoming message is either “0” or “1”.

What is interesting that exactly the same setup (mqtt, icons) works perfectly fine when I separate my code into two items - a switch and a number:

 Switch item=Clear_case mappings=[ON="Clear case"]
 Text item=Clear_case_status label="[]"

Number Clear_case_status "[%d]" <casestatus>  {mqtt="<[broker:3d_printer/clear_case/status:state:default]"}
Switch Clear_case "" <empty> {mqtt=">[broker:3d_printer/clear_case/switch:command:ON:1]"}

Which proves that I got the MQTT and dynamic icons stuff right. But I am puzzled why the same doesn’t work for the case of a dynamic icon with a single Switch item (my first example).

BTW I am using Basic UI, OpenHab2.

Can anyone tell me what I am doing wrong here?


Could this be related to raw vs mapped state?

I think I nailed this one. What adds to the confusion is that apparently every time I make a change related to icons (add a new icon file, or even just change the icon description in the item), I have to shut down and restart OpenHab for this to take effect. That makes it harder to converge to a proper solution.

Anyway, this now seems to work exactly as I needed, with a single sitemap element (switch) performing two functions - a button, and a state display:


Switch item=Clear_case mappings=[ON="Clear case"]

Switch Clear_case "" <casestatus> {mqtt=">[broker:3d_printer/clear_case/switch:command:ON:1],<[broker:3d_printer/clear_case/status:state:MAP(]"}

The trick was to add MAP(, and then restart OpenHab. In my icons folder I have both casestatus-0, 1 and casestatus-on, off files, so not quite sure which one are used (probably on, off?).