Dynamic Icons to show battery status

Hi there,

I’m trying to figure out how to get the battery status displayed for different battery-operated devices. They all return their status in percent and I want to display an according icon.

I know it’s supposed to work like the other dynamic icons i.e. switch.png, switch-on.png and switch-off.png. And that works fine, but when using a percentage value the icon is only displayed when there is an icon with the corresponding value present i.e. battery-25.png, battery-50.png, battery-75.png and battery-100.png.
So the icons only appear when the battery is exactly 25%, 50%, 75% or 100%.

How can I change that behaviour? I think it should work with a MAP-File. Can you give me a hint?

Not sure this is currently possible. I also was playing with it, but gave up on an icon for every value. Tho if you had a cool graphic set with 100 points in the bar that could be cool.

why not working with a tem variable instead that is used for display.

if you get a return value for the battery state - you post this one with a rule into a temp variualbe that puts it to 25/50/75/100 state based on the value you want to display it?

something like:

rule "BatteryTemp"
	Item battery received update
	if (battery.state<=25) {
		postUpdate(batteryDisplay, 25)
	if (battery.state<25<=50) {
		postUpdate(batteryDisplay, 50)
	if (battery.state<50<=75) {
		postUpdate(batteryDisplay, 75)
	if (battery.state<75<=100) {
		postUpdate(batteryDisplay, 100)

((!! untested rule !!)) 

And then you use this for the decimal to display?


Thanks that’s an interesting approach.
If i understand correct, that would leave me with the correct icons but the percentage-display would only display 25, 50, 75 or 100 and not the real battery values like 56%, 77%,… or do I miss something?

not sure - as you might be able to use the icon the following way:

Text item=battery icon=“batteryDisplay”

but untested.

This is my working configuration:


Text item=Battery	icon="measure_battery-0"	label="LG G4  [%d %%]"	visibility=[Icon==0]
Text item=Battery	icon="measure_battery-25"	label="LG G4  [%d %%]"	visibility=[Icon==25]
Text item=Battery	icon="measure_battery-50"	label="LG G4  [%d %%]"	visibility=[Icon==50]
Text item=Battery	icon="measure_battery-75"	label="LG G4  [%d %%]"	visibility=[Icon==75]
Text item=Battery	icon="measure_battery-100"	label="LG G4  [%d %%]"	visibility=[Icon==100]


rule "BatteryIcon LG G4"
    	Item Battery received update
    	if (Battery.state>=0 && Battery.state<15) {
    		postUpdate(Icon, 0)
    	if (Battery.state>=15 && Battery.state<30) {
    		postUpdate(Icon, 25)
    	if (Battery.state>=30 && Battery.state<60) {
    		postUpdate(Icon, 50)
    	if (Battery.state>=60 && Battery.state<80) {
    		postUpdate(Icon, 75)
    	if (Battery.state>=80 && Battery.state<=100) {
    		postUpdate(Icon, 100)


Number  	Battery			"Dominic's LGG4 Battery"		
Number  	Icon			"Dominic's LGG4 Battery Icon"
1 Like

I think this is one of many instances where we need to work on a decent dashboard type interface that can be customised and repurposed at will.

Did you get any further with this? Perhaps it is possible to add dynamic SVG states with a bit of client side programming? I am new to openHab - but digging in as I type.

I wanted to do the same thing but for a volume icon that has 3 states. I solved it with brute force.

What I did was created a sub folder ‘volume’ in the icons directory and copied my three icons there. I then created a bunch of hard links to them. One link for each possible volume value. Note that it has to be hard links. Soft links doesn’t work.

The bash command I used to create the hard links to the icons. The icon file names are volume–99.png, volume–59.png and volume-15.png. The volume range is -99 to 15:

for  i in `seq -98 -60`; do sudo ln  volume--99.png volume-${i}.png; done
for  i in `seq -58 -20`; do sudo ln volume--59.png volume-${i}.png; done
for  i in `seq -19 14`; do sudo ln  volume-15.png volume-${i}.png; done

My sitemap (I used the trick above to get the mute icon in)

Setpoint item=Volume icon="volume/volume" minValue=-99.0 maxValue=15 step=1 visibility=[Mute==OFF]
Setpoint item=Volume icon="volume/mute" minValue=-99.0 maxValue=15 step=1 visibility=[Mute==ON
Switch item=Mute icon=""

What I like with this solution is that it doesn’t depend on any rules or extra items to work.
It’s ugly but works like a charm.

1 Like