Verify range value

good morning,
I have this code that I would like to modify but I can’t do it myself.
Since the value does not go to step ten I could have intermediate values.
So I should check if the value is:

> = 1 and <= 10
> = 11 and <= 20
> = 21 and <= 30
etc.
> = 91 and <= 100

how can I change the code to get this control?
Thanks, Alberto
      entities:
        - sensor.posizione_grafica_tapparella_cucina
      image_template: '
        var imageName = "";
        switch (entity.state) {
          case "10"
            imageName = "10";
            break;
          case "20"
            imageName = "20";
            break;
          case "30"
            imageName = "30";
            break;
          case "40"
            imageName = "40";
            break;
          case "50"
            imageName = "50";
            break;
          case "60"
            imageName = "60";
            break;
          case "70"
            imageName = "70";
            break;
          case "80"
            imageName = "80";
            break;
          case "90"
            imageName = "90";
            break;
          case "100"
            imageName = "100";
            break;
        }
        return "/local/custom_ui/floorplan/images/cover/" + imageName + ".svg";
        '

Thats a switch case, you can’t modify that code without a complete rewrite because it doesn’t translate 1 to 1. You have to convert this to if then or come up with a different solution. But to be quite honest, that switch case is pretty crap to begin with. The original intent could have been solved by just passing the entity.state instead of using a switch case.

This template should work for yours and the previous javascript as well.

      entities:
        - sensor.posizione_grafica_tapparella_cucina
      image_template: >
        var value = parseFloat(entity.state) || 0;
        var base = Math.floor(value/10)*10;
        value =  base + Math.ceil((value%10)/10)*10;
        return "/local/custom_ui/floorplan/images/cover/" + value.toString() + ".svg";
1 Like

The code is for floorplan

Yes, I know.

Ah !! :blush:

the code above will return the following based on these state:

> "entity.state=0 returns /local/custom_ui/floorplan/images/cover/0.svg"
> "entity.state=1 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=2 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=3 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=4 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=5 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=6 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=7 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=8 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=9 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=10 returns /local/custom_ui/floorplan/images/cover/10.svg"
> "entity.state=11 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=12 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=13 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=14 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=15 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=16 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=17 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=18 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=19 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=20 returns /local/custom_ui/floorplan/images/cover/20.svg"
> "entity.state=21 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=22 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=23 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=24 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=25 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=26 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=27 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=28 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=29 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=30 returns /local/custom_ui/floorplan/images/cover/30.svg"
> "entity.state=31 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=32 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=33 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=34 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=35 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=36 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=37 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=38 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=39 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=40 returns /local/custom_ui/floorplan/images/cover/40.svg"
> "entity.state=41 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=42 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=43 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=44 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=45 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=46 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=47 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=48 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=49 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=50 returns /local/custom_ui/floorplan/images/cover/50.svg"
> "entity.state=51 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=52 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=53 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=54 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=55 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=56 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=57 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=58 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=59 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=60 returns /local/custom_ui/floorplan/images/cover/60.svg"
> "entity.state=61 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=62 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=63 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=64 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=65 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=66 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=67 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=68 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=69 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=70 returns /local/custom_ui/floorplan/images/cover/70.svg"
> "entity.state=71 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=72 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=73 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=74 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=75 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=76 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=77 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=78 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=79 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=80 returns /local/custom_ui/floorplan/images/cover/80.svg"
> "entity.state=81 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=82 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=83 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=84 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=85 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=86 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=87 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=88 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=89 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=90 returns /local/custom_ui/floorplan/images/cover/90.svg"
> "entity.state=91 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=92 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=93 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=94 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=95 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=96 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=97 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=98 returns /local/custom_ui/floorplan/images/cover/100.svg"
> "entity.state=99 returns /local/custom_ui/floorplan/images/cover/100.svg"

you are right
Your code works perfectly. :star_struck:
I could never get there by myself. :worried:
Thanks 1K

1 Like