[SOLVED] Converting value to percent level water container

Hi, i begin on openhab rules so i wonder if is a way to show on my sitemap the values recieved from mqtt in number to pourcent .

For exemple to 25cm to 100% full
to 180cm to 0% Empty

I recieve “cm” value from SR04 ultrasonic sensor.

Here is my mqtt items:

Number EspsonicCiterne “Niveau citerne eau de Pluie [%s cm]” (InfoTech,It_Group_Li_Distance) {channel=“mqtt:topic:OpenhabDht22:Espsonic”}

And here my sitemap Text label.

Default item=EspsonicCiterne label=“Niveau Citerne Actuel”

I know a little (i mean a little) about mapping values but this not what i want beacause i want to know the exact level (exemple 80%, or 81% exc…)

how i can do ?

thanks a lot for your help

How about a second item which is recalculated whenever the value for EspsonicCiterne changes?

Number EspsonicCiterne ...
Number EspsonicCiternePercent
rule "EspsonicCiternePercent"
when
     Item EspsonicCiterne changed
then
     EspsonicCiternePercent.sendCommand(EspsonicCiterne.state * 100 / 1.8) //for 100% = 180cm
end

Define a new item for your percent-value:

Number EspsonicCiterne_percent "Filled [%.0f %%]"

Then define a rule, that reacts to your cm-item:

rule "calculate percent from cm"
when
    Item EspsonicCiterne changed
then
    EspsonicCiterne_percent.postUpdate(100 - ((((EspsonicCiterne.state as DecimalType) -25)*(100))/(180-25)))
end

MAP has a companion transformation SCALE. You could use this in your [label] to show as a percentage, it will not change the Item’s actual state.

You could also use a Javascript tranformation

(function(value) {
    var niveau = parseInt(value);
    var resultat = (100 - ((niveau -25)*100)/(180-25))
    return resultat;
})(input)
2 Likes

Thanks for all your reply i really appreciate it, a test that quickly

Ok so here is what i do , i use the dedicate Transformation service to solve this case, again thanks all for your help.

I use the scale Transformation services,
i create i file named “CiterneCmToPourcent.scale” inside i write this:

[…18]=100 //[…18 ]for cm and 100 for pourcent
]18…20]=99
]20…22]=98
]22…24]=97
]24…26]=96
]26…28]=95
]28…30]=94
]30…32]=93
]32…34]=92
exc… to 0

Then i a item file i create a new Item type String.

Here is the original value from the sensor:
Number EspsonicCiterne “Niveau citerne eau de Pluie [%s cm]” {channel=“mqtt:topic:OpenhabDht22:Espsonic”}

There is the new item converted to pourcent value with the profile transform:SCALE:

String EspsonicCiterneRestant "Citerne en % restant " { channel=“mqtt:topic:OpenhabDht22:Espsonic”[profile=“transform:SCALE”, function=“CiterneCmToPourcent.scale”]}

then in sitemap:

Default item=EspsonicCiterneRestant label=“Niveau Citerne restant [%d %%]” labelcolor=[“green”] //the labelcolor need to be chose when state is below…75% ou 50% for exemple…i don’t know how actually :slight_smile:

it work like a charm !

Thanks for the community !

2 Likes

Example
Text item=someItem valuecolor=[>75="red",>50="yellow",>25="green",>0="blue"]
First condition satisfied gets used

Thank you! Again this community is amazing, a learn with you more quickly then ever, Openhab is… Endless amazing

Thanks