Change Switch light bulb icon based on item state

I am controlling a relay via MQTT protocol and it is working great. I have a switch on my sitemap that can switch the relay on and off and I have a Text item that can show the status of the relay(1, 0).

However I would rather use the light bulb icon to the left of the switch label to indicate the actual status of the relay so it is all on one line. I am just using the default light bulb icon. Here is how everything looks right now:

sitemap demo label="My Relay Board" { Frame label="Relays"{ Switch item=relaySwitch label="Relay Switch" Text item=status } }

Switch relaySwitch "Relay Board" {mqtt=">[mybroker:setrelaystatus:command:ON:on],> [mybroker:setrelaystatus:command:OFF:off]"} Number status "Status is [%d]" {mqtt="<[mybroker:relaystatus:state:default]"}

When the state of the status item changes to a 1 I want to turn on the lightbulb icon for the relaySwitch item and when the state of the status item changes to a 0 I want to turn off the lightbulb icon.

I believe this is all done in rules but I just cannot figure it out. Also I do not know the names of the lightbulb icons so I cannot reference them in a rule. Where are those icons stored?

Sorry for the newbie questions but I have looked around for quite a while now and just cannot find the answer to this.

First let me clear up a couple of misconceptions. Icons are a sitemap thing and only a sitemap thing. You cannot do a thing about icons from a rule.

You can specify the light bulb icon to your relaySwitch by either specifying it in the item or on the sitemap. For example, in the Items:

Switch relaySwitch "Relay Board" <light> {mqtt=">[mybroker:setrelaystatus:command:ON:on],> [mybroker:setrelaystatus:command:OFF:off]"}

For the sitemap:

Switch item=relaySwitch icon="light"

NOTE: You only need to specify it in one place so if you put it in your .items file you don’t need to specify it on the sitemap. The same goes for label.

If you look in your openhab/webapp/images folder you will find all of the potential icons you can use on your sitemap. You will notice there are often several versions of the same icon. For example, light-on.png and light-off.png. What happens is the sitemap will choose the whatever icon matches the state of the item. So if you choose <light> as your icon for the relaySwitch Item, it will show light-on.png when the Switch is ON and light-off.png when the switch is OFF.

Notice that I haven’t mentioned the status Item yet. This is because I don’t think you don’t need it. I believe you can move the mqtt binding stuff from your status Item up to your relaySwitch Item and replace the “default” with a mapping that converts 1 to ON and 0 to OFF. This will cause the Switch to be updated to the actual state of the of the relay. The wiki page is a little ambiguous as to whether this will work but I think I’ve seen people post examples like this to this forum before.

If you decide you cannot get the status to work right all on one Item, and for completeness, your rule to update the relaySwitch from the status Item would look like the following:

rule "status received update"
    Item status received update
    relaySwitch.postUpdate(if(status.state.toString == "1") ON else OFF)

How icons are applied to Items and how they can be made dynamic is documented in the Items wiki page. How icons are applied to the sitemap elements is documented in the Sitemap wiki page. And how to have two way MQTT on the same Item is documented in the MQTT wiki page.

Hi @rlkoshak

Thank you for your very thorough response.

What I am hearing is it is not possible to set just the icon based on an item state correct? Can you not do something like relaySwitch.icon = ON or relaySwitch.icon = OFF or perhaps relaySwitch.icon = light-on.png or relaySwitch.icon = light-off.png? I want to completely disconnect the status of the light icon from the state of the switch and set it solely based on an item state.

I guess you can accomplish the same by setting the switch item to on and off I am just asking out of curiosity. I will try as you suggested.

Thank you

Not quite. If you assign <light> to a switch it will use light-on.png when the switch is ON and light-off.png when the switch is OFF. If you have your own icons or using an Item that has a different type of state (e.g. Contacts use OPEN and CLOSED so you would have door-open.png and door-closed.png) you can get the icon based on state by naming the icons appropriately.

No. There is no way to modify the icon dynamically except for the icon naming convention described above.

Now one thing you can do that is semi advanced and doesn’t work very well is to use the visibility flag on the sitemap. For example, I use it to change the icon on a switch based on the state of a contact.

Switch item=T_D_Garage1 label="Garage Door 1" icon="garagedoor-closed" mappings=[ON=Open] visibility=[N_D_GarageDoor1!="OPEN"]
Switch item=T_D_Garage1 label="Garage Door 1" icon="garagedoor-open" mappings=[ON=Close] visibility=[N_D_GarageDoor1=="OPEN"]

But the caveat is this doesn’t always refresh well in the phone apps but should work better in OH 2.

Why not have a single item?

Switch relaySwitch "Relay Board" {mqtt=">[mybroker:setrelaystatus:command:ON:on],>[mybroker:setrelaystatus:command:OFF:off],<[mybroker:relaystatus:state:MAP(]", autoupdate="false"}

1 Like

Not always possible - I have a number of outputs which have a separate physical tellback (detects that power is really applied to the output). So, like Travis’ original configuration, I have a “Number” tellback which I map to “ON” or “OFF”. I’d like to have the dynamic icons too, but have concluded from the documentation that they can only be associated with a Switch item.

1 Like

Does my example item not meet this description?

Could you provide a link to this incorrect assertion?

I was thinking about that while looking at other topics - you replied so quickly I didn’t have time to revisit my post!

I think I tried something like that a while back (probably on OH 1.7) and it didn’t work at the time - I’ll have to have another look.
In fact my scenario is a little more complex, in that I actually have two status bits relating to each output - one from the output drive (which I didn’t mention previously) and the tellback indicating that the output is powered.

Some of these outputs are on motion-triggered lights; thus the light can be switched on by the motion sensor independently of OH. With the two status inputs I can identify whether and why the light is turned on. The icon really needs to be driven from the “light on” tellback, while the “output on” tellback also needs to be reflected somewhere.

It was a deduction which came from reading the “Items” page in the wiki - the examples use a Switch item which is essentially a boolean.
Your comment suggests that I’ve assumed a limitation which doesn’t exist. Could I, for example, have a Number item whose numeric values 0…2 are mapped to the words “red”, “green”, “blue” and icon files “light-red.png”, “light-green.png” and “light-blue.png”? Then link the item and the icon “light” in the normal way (items file or sitemap). Is this possible?
Incidentally, are the state names case-sensitive when used with icons in this way?

Because I’m lazy, I was hoping you would point me to a poorly worded bit of wiki I could improve. :slightly_smiling:

If you have a number item like

Number dice "facet [%d]" <dice>

You could have icons like


If you have a string item like

String hvacMode "hvacMode [%s]" <hvac>

and it can have states like Heat, COOL, aUto, you can have icons like

1 Like

That was the clue I needed, thankyou - working nicely now.

I think the point I had missed was that you have to use the value returned from the variable (which in my case is numeric) rather than any text string it is mapped to for display.
You’ve created a nice example which could usefully be added to the wiki!

1 Like


I have an similar question:
My Switch value comes from an json object getting an String “on” or “off”.
Now I want to change the switch and the bulb light.


rule "lagerraum"
        Item Lagerraum_Light_OutValue received update
        Lagerraum_Light_Out.postUpdate(if(Lagerraum_Light_OutValue.state == "on") ON else OFF)

Text item=Lagerraum_Light_OutValue label="[%s]" icon=“none” shows each refresh “on” or “off” in the right state of the physical lamp. :slight_smile:
item is defined:
Switch item=Lagerraum_Light_Out label="Lagerraum: " icon=“light”

it does not change… do I have to restart openhab2 if rule is changed?
if I change a sitemap, the log tells me that it reloads sitemaps… but this is not shown if I change rules…
pls. help thanks

What’s the name of the rule file?

thank you so much! :wink: .rule is not the best try… .rules makes sense!

I know this thread is a few months old, but I just stumbled across this entry describing another way of changing icons dynamically if anyone should be interested: