[SOLVED] Dynamic icon, from other item

Hi there.

I have been away for a while, but have been running openHAB 1.8 for quite some time now.
Today i began to migrate to version 2.1.0

When i make a push button, with dynamic icon in 1.8, i do this:
Sitemap:

Switch item=kokken_spot_switch label="Spot" icon="light-on" mappings=[ON="Sluk"]  visibility=[kokken_spot_state=="ON"]
Switch item=kokken_spot_switch label="Spot" icon="light-off" mappings=[ON="Tænd"]  visibility=[kokken_spot_state=="OFF"]

Items:

Switch kokken_spot_switch		(gStue_kokken)	{ ihc=">5039121"}
Switch kokken_spot_state		(gStue_kokken, lys)	{ ihc="5040146"}

Rules:

rule "kip spot"
when
Item kokken_spot_switch changed to ON
	then
	Thread::sleep(500)
	kokken_spot_switch.sendCommand(OFF)
end		

This actually works great.
The light icon reflects the state of the output.

I tried to accomplished this in 2.1.0, but can’t get it to work.
Correct me if i’am wrong, in openHAB 2, the icon follows the switch item, right?
So i can’t force the icon to follow the state of my output.

What do i do to get the above code to work in the version 2?

Thanks for reading and helping.

The behavior of dynamic icons has changed a bit. You can no longer refer to dynamic icons like that any longer.

What you need to do is copy the icons you want and name them something like kokkenon.png and kokkenoff.png (note there is not a - and it uses all lowercase). Then use those as your icons on the sitemap for those Items.

You can download the PNG version of the default icons here. I’m not sure where to get the svg icons.

1 Like

And of course all of this can also be found at http://docs.openhab.org/configuration/items.html#icons :slightly_smiling_face:

1 Like

Thanks @rlkoshak.
I cant believe it was so easy :slight_smile:

Thanks @ThomDietrich, i have read that page multiply times, and i do understand the new behavior of dynamic icons. But i can’t find anything about renaming the icons, to force them to follow the visibility state and not the item state.

I have done this for the heating icons based on the valve state.
items:

//Icons
Number WZ1_Icon
Number WZ2_Icon
Number KU1_Icon
Number KU21_Icon
Number SZ_Icon
Number EZ_Icon
Number BD_Icon
Number BR_Icon

icon rule:

rule "Wohnzimmer WZ1 Icons Heizung"
when
	Item WZ1_Heizung_Valve changed
then
	if (WZ1_Heizung_Valve.state >= 0.0 && WZ1_Heizung_Valve.state <= 0.2)	WZ1_Icon.sendCommand(20)	
	if (WZ1_Heizung_Valve.state > 0.2 && WZ1_Heizung_Valve.state <= 0.4)	WZ1_Icon.sendCommand(40)	 	
	if (WZ1_Heizung_Valve.state > 0.4 && WZ1_Heizung_Valve.state <= 0.6)	WZ1_Icon.sendCommand(60)		
	if (WZ1_Heizung_Valve.state > 0.6 && WZ1_Heizung_Valve.state <= 0.8)	WZ1_Icon.sendCommand(80)	 	
	if (WZ1_Heizung_Valve.state > 0.8 && WZ1_Heizung_Valve.state < 1.0)		WZ1_Icon.sendCommand(100)	 	
end

Sitemap:

 Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="heatingoff"	visibility=[WZ1_Icon==0]
                Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="heating20"		visibility=[WZ1_Icon==20]
                Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="heating40"		visibility=[WZ1_Icon==40]
                Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="heating60"		visibility=[WZ1_Icon==60]
                Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="heating80"		visibility=[WZ1_Icon==80]
                Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="heating100"	visibility=[WZ1_Icon==100]

The name of the icons are (have to be stored in /classic/icons of course)

heating20
heating40

etc

1 Like

Hello @alkaline, thanks for posting a complete example, however I have to ask why you did go through all that trouble. You’ve basically imitated the functionality of Dynamic Icons. The same thing can be accomplished by the following set.
Did you do all of that just to spare you of one additional sitemap element? I admire the passion :smiley:

Still, so that no one gets the wrong idea, a slightly different solution would be:

Number WZ1_Heizung_Valve_Percent <heating>
rule "Wohnzimmer WZ1 Icons Heizung"
when
	Item WZ1_Heizung_Valve changed
then
	WZ1_Heizung_Valve_Percent.postUpdate(WZ1_Heizung_Valve.state * 100)
end
Switch item=WZ1_Heizung_Mode label="Heizkörper" mappings=[1="An", 3="Aus"] icon="chart"
Number item=WZ1_Heizung_Valve_Percent label="Ventilstellung [%d %%]"
1 Like

Hi @ThomDietrich, thanks for clarification. However, the idea wasn’t to have two items displayed within sitemap, but one item (only the switch), which has the icon of the valve state applied which works great. I am acting from an end-user point of view since I am not a professional coder at all. So my solution can be optimized?

Hmmmmmmm,
nope I think your solution is the best you can do. The only thing you could probably improve is figure out a formula to reduce your rule to one line. Something like (untested!)

WZ1_Icon.postUpdate(((WZ1_Heizung_Valve.state)/2*10 + 0.5).intValue * 20)