[SOLVED] How to set battery icon values

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?

I’m not sure what you’re trying to do now.

You already had a working Number Item, approx 2 to 3 value.
You just need to make another Number Item.
Then a rule that runs when your source Item changes, does whatever maths you want (presumably convertin to 0-100%), and posts the result to your new Item.
Put the new Item on your sitemap and ignore the source Item.

If transform profiles worked with Numbers you could do it that way instead -but they don’t, yet.

Where did you do this? Which file or which code do I have to alter? Thanks for your help.