Dynamic Battery Icon for LowBattery in sitemap

I wanted to create a sitemap to show the battery status of all my battery powered things. Some have a battery percentage where it was easy to find an example sitemap, but for devices that only report lowbattery through a channel it was not so easy to find a nicely formatted example.

Here is a workable solution - not perfect as each item has to be added twice, but the results are pleasing.

First of all a battery percentage is trivial, showing dynamic icons and the actual percentage on the right.

Frame label="Trådfri Controllers Battery Level"
    Default label="TRADFRIRemoteControl_BatteryLevel"

But the low battery item default, although simple to define, the results are not so great.

    Default label="TRADFRIRemoteControl_LowBattery"

This results in a moveable switch and the battery icon does not vary to show low battery warning

After scanning the OH docs and community, I came up with this:

Frame label="Nest Protects Low Battery"

    Text item=KitchenNestProtect_LowBattery 
        label="Kitchen Nest Protect Battery" icon="battery" visibility=[ KitchenNestProtect_LowBattery==OFF]
    Text item=KitchenNestProtect_LowBattery 
        label="Kitchen Nest Protect Battery Low" icon="lowbattery" visibility=[ KitchenNestProtect_LowBattery==ON]

When battery is OK it shows as a green icon

When battery is reporting low, then it displays a red icon - obviously a real example where a different device is currently reporting low battery.

In the sitemap snippet, there are two entries for each device which are alternately displayed (visibility) based on the low battery status. The standard icons - battery and lowbattery are used. The label adds the word low and the switch icon is suppressed by changing the item type to text.


1 Like

Nice approach. You could think about another approach using th dynamic icon feature, a proxy item and some rules.

1.) Create some icons representing the battery states 10% 20% … 90%
2.) Create a proxy item that is displayed in the sitemap with the icons just created
3.) Create a rule that sets the proxy item on every change or update to a rounded value of the real battery state e.g. 10%, 20%, …

So you should be able to use 10 or more different icons to display the battery state

If I recall correctly, the dynamic icon picker automagically rounds a Number Item down to the next available icon
e.g. if you have batt-20.svg , batt-50.svg etc
a value of 47 will show batt-20

1 Like

But LOW_BATTERY is either ON or OFF :pensive:

If it is only a ON/OFF low battery warning, I would personally just go with:

Text item=KitchenNestProtect_LowBattery 
        label="Kitchen Nest Protect Battery Low" icon="lowbattery" visibility=[ KitchenNestProtect_LowBattery==ON]

to only show a as warning if there is something to be alarmed about. In all other cases, the text and icon have very limited value to show.
But that is probably mostly a personal preference.

An alternative solution would be to make copies of the battery icons you want to use for ON/OFF and NULL then make copies of them into the icons/classic folder using https://www.openhab.org/docs/configuration/items.html#dynamic-icons.

That way you don’t need to use the visibility flag to achieve this behavior.

1 Like