[SOLVED] How to set battery icon values

I have a Thing channel of type number , which receives a voltage from an MQTT state topic. It is typically around 2 to 3.5 volts. This is linked to an Item “B” of category battery.

In OH panel , a widget is linked to the Item "B"and I use the battery icon from “classic set” icons.

Now, the battery icon is just white regardless of the Item “B” value but if i create a new Item, link that to the openhab panel widget , and set that Item value to say 50, the battery icon has a green infill of about 50%, as expected.

So , where can i define the scaling for the icon, because clearly my Item for voltage (around 3V) is not generating any infill of the battery icon.

?

I have to look at how mine is setup exactly. If your number is structured right I believ battery works out of the box.

The number needs to be between 0-100 if I remember correctly. This is for sitemaps.

1 Like

mmm, well, i suppose I’ll have to do an output transformation on the channel then to scale it up, because min value on the topic will be 1V (1) and max is 3.5V (3.5) so that’s a range 2.5 on a scale of 0.100 (for battery)

?

See https://www.openhab.org/docs/configuration/items.html#dynamic-icons for a full documentation for how dynamic icons work. If the default settings do not work for you, you can either create proxy Items or transform the value to work with the icons which expect a range between 0 and 100 or you can create a new set of icons with the _<state> parts in the ranges you expect.

The battery icons that come with OH were configured to work with percentages, not voltages.

1 Like

PERFECT!

thank you Rich

Daz.

I’ve transformed the output to scale at a resolution of 28.57 x value… thus 3.5V (max) gives 100 percent

Please tick the solution, thanks

1 Like

Quite correct; sorry; done now!

Apologize to get into a SOLVED topic, but (as a newbie) I do not understand yet the mechanics of the “output” transformation… are we talking of Javascript-ing the labels? Or in the channel? In the latter case is it possible to tranform the output in PaperUI?

Thanks!
Max

You can’t use dynamic icons or value transforms effectively in PaperUI. It’s just an administrators toolbox, not intended for finished user facing display.
What are you trying to do?

In general, transforms can be used in rules, or in displaying item states, or in profiles linking channels to Items,or a great many bindings allow you to apply transforms to incoming or outgoing data.

Hi Rossko57,

Apologize, I was not clear.

Let me clarify:

  • I have T/humidity sensors powered by LiPo batteries. The ESP8266 reports battery voltages through the MQTT protocol, so an item is created
Number   LivingDining_BatteryCharge "Living Battery charge [%.1f V]"         <batterylevel>      (LivingDining, gBatteryCharge)      ["Battery charge"]      {channel="mqtt:homie300:efd6615c:livesp8266pwrsht30:livESP8266PwrSHT30#batteryLevel"}
  • As expected, dynamic icons cannot be applied, so with a voltage around 2-3V an empty battery icon is shown

  • So, I decided to use a transformation

(function(i){
var minV = 2.
var percBatt = 0.
percBatt = (parseFloat(i) - minV)*100
return Math.round(percBatt)
})(input)

  • This should return a percentage (o an integer?) between 0-100 when the V range is between 2 and 3 - according to my experience the voltage range needed for the humidity sensor to work properly

  • The label is modified as below

Number   LivingDining_BatteryCharge "Living Battery charge [JS(v-to-charge.js):%d]"         <batterylevel>      (LivingDining, gBatteryCharge)      ["Battery charge"]      {channel="mqtt:homie300:efd6615c:livesp8266pwrsht30:livESP8266PwrSHT30#batteryLevel"}
  • But I get always 100.0 (voltage is reported to be 2.55 --> my expectation is to get 55 instead)

  • Additionally, I get the error below in the log file

2020-02-08 22:04:17.671 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state ‘2.55’ on item ‘LivingDining_BatteryCharge’ with pattern ‘JS(v-to-charge.js):%d’: Cannot format state ‘2.55’ to format ‘%d’

I am pretty sure I am missing something trivial, perhaps could you help?

Thanks
Max

P.S.: using now OH2.5 on Rpi3B+

Transformations always return strings.
“2.55” is a four character string here.
It cannot be formatted using %d for decimal number.
You’d want to use string format %s

It’s not going to help any with icons; dynamic icons are selected on the Item’s state.
The Item’s state remains unaltered by transforms applied in the label, which only affect the displayed value.

I almost feared that :fearful:

Does it mean I need either to define another item (and assigning a transformation of the input value) or change the Arduino code I put into the sensors? Or perhaps there is a third way (e.g. transforming the input value) I do not know yet?

Thanks for being so patient with a newbie :rofl:

Either of those methods can be used with another Item.
Or MQTT binding will let you transform incoming data before passing it to channel/Item.

Hi rossko57,

thanks for mentoring :wink:

Does it mean I need to redefine the Thing in a text file? Up to now I have been very happy to use PaperUI for the initial conf (discovery->linking), then going down with text files for final tuning.

The least intrusive way I can think I could do is to use a proxy item that will source from the original one (defined in the PaperUI) and apply the transformation. I guess I need to use something like shown here

String Bedroom_Sonos_CurrentTitle "Title [%s]" (gBedRoom) {channel="sonos:..."}

But if you have more elegant examples let me know…

Max

Apologize, the link should point to here

And perhaps this could be an example to borrow from

No. If you want to edit your Things with PaperUI, go ahead. Look for transformation options on the channel.

:crazy_face:

I must be completed off today… I cannot find the transformation option in the channel (Paper UI -> Configuration -> Things -> Configure Channel), see below.

I am sure I have seen this before, at least for another binding.

In general, MQTT binding lets you do this stuff.

But you’re using Homie? Which auto-does the settings and presumably takes options away from you.

Ok, it means I need an additional item+transformation to workaround this (I like Homie :slightly_smiling_face:).

My first try, just to check if I can “copy” the value (surely not right)

Number mqtt_homie300_efd6615c_livesp8266pwrsht30_livESP8266PwrSHT30_batteryLevelperc   "Battery level"  <batterylevel>  (LivingDining, gBatteryLevel)  ["batteryLevel"] {stateTopic="homie/livesp8266pwrsht30/livESP8266PwrSHT30/batteryLevel" } 

The topic is directly copied from

mosquitto_sub -v -t homie/# -u openhabian
homie/livesp8266pwrsht30/livESP8266PwrSHT30/batteryLevel 2.54

But unfortunately the value is set to NULL.

Is my approach correct at all?