Lookup Table Sensor

Can anyone help with a lookup table sensor, I cant get it to work, I need it to ready the cm height and convert it to volume.

  - sensor:
  - platform: template
    sensors:
      oil_volume_liters:
        friendly_name: "Oil Volume"
        unit_of_measurement: "L"
        value_template: >
          {% set depth_cm = states('sensor.liquid_depth') | float %}
          {% set lookup_table = {
            1: 11.309776720584
            2: 22.619553441168
            3: 33.929330161752
            4: 45.239106882336
            5: 56.54888360292
            6: 67.858660323504
            7: 79.168437044088
            8: 90.478213764672
            9: 101.78799048526
            10: 113.09776720584
            11: 124.40754392642
            12: 135.71732064701
            13: 147.02709736759
            14: 158.33687408818
            15: 169.64665080876
            16: 180.95642752934
            17: 192.26620424993
            18: 203.57598097051
            19: 214.8857576911
            20: 226.19553441168
            21: 237.50531113227
            22: 248.81508785285
            23: 260.12486457343
            24: 271.43464129402
            25: 282.7444180146
            26: 294.05419473519
            27: 305.36397145577
            28: 316.67374817635
            29: 327.98352489694
            30: 339.29330161752
            31: 350.60307833811
            32: 361.91285505869
            33: 373.22263177927
            34: 384.53240849986
            35: 395.84218522044
            36: 407.15196194103
            37: 418.46173866161
            38: 429.77151538219
            39: 441.08129210278
            40: 452.39106882336
            41: 463.70084554395
            42: 475.01062226453
            43: 486.32039898511
            44: 497.6301757057
            45: 508.93995242628
            46: 520.24972914687
            47: 531.55950586745
            48: 542.86928258803
            49: 554.17905930862
            50: 565.4888360292
            51: 576.79861274979
            52: 588.10838947037
            53: 599.41816619095
            54: 610.72794291154
            55: 622.03771963212
            56: 633.34749635271
            57: 644.65727307329
            58: 655.96704979388
            59: 667.27682651446
            60: 678.58660323504
            61: 689.89637995563
            62: 701.20615667621
            63: 712.5159333968
            64: 723.82571011738
            65: 735.13548683796
            66: 746.44526355855
            67: 757.75504027913
            68: 769.06481699972
            69: 780.3745937203
            70: 791.68437044088
            71: 802.99414716147
            72: 814.30392388205
            73: 825.61370060264
            74: 836.92347732322
            75: 848.2332540438
            76: 859.54303076439
            77: 870.85280748497
            78: 882.16258420556
            79: 893.47236092614
            80: 904.78213764672
            81: 916.09191436731
            82: 927.40169108789
            83: 938.71146780848
            84: 950.02124452906
            85: 961.33102124964
            86: 972.64079797023
            87: 983.95057469081
            88: 995.2603514114
            89: 1006.570128132
            90: 1017.8799048526
            91: 1029.1896815731
            92: 1040.4994582937
            93: 1051.8092350143
            94: 1063.1190117349
            95: 1074.4287884555
            96: 1085.7385651761
            97: 1097.0483418967
            98: 1108.3581186172
            99: 1119.6678953378
            100: 1130.9776720584
            101: 1142.287448779
            102: 1153.5972254996
            103: 1164.9070022202
            104: 1176.2167789407
            105: 1187.5265556613
            106: 1198.8363323819
            107: 1210.1461091025
            108: 1221.4558858231
            109: 1232.7656625437
            110: 1244.0754392642
            111: 1255.3852159848
            112: 1266.6949927054
            113: 1278.004769426
            114: 1289.3145461466
            115: 1300.6243228672
            116: 1311.9340995878
            117: 1323.2438763083
            118: 1334.5536530289
            119: 1345.8634297495
            120: 1357.1732064701
          } %}
          {% set target = 71 %}
          {% set ns = namespace(diff=float('inf'), depth=None) %}
          {% for d in lookup_table.keys() %}
            {% set cur_diff = (d - target) | abs %}
            {% if cur_diff < ns.diff %}
              {% set ns.diff = cur_diff %}
              {% set ns.depth = d %}
            {% endif %}
          {% endfor %}
          {% set nearest_depth = ns.depth %}
          {% set volume_liters = lookup_table[nearest_depth] %}
          {{ volume_liters }}

I just get an error in HA

  • Invalid config for ‘template’ at configuration.yaml, line 82: ‘sensor’ is an invalid option for ‘template’, check: sensor->1->sensor Invalid config for ‘template’ at configuration.yaml, line 82: required key ‘state’ not provided
  • Invalid config for ‘template’ at configuration.yaml, line 83: ‘platform’ is an invalid option for ‘template’, check: platform Invalid config for ‘template’ at configuration.yaml, line 88:

Here’s how I’m leveraging an array - in my case to create an icon_template: - but should be easily convertible for your scenario:

      icon_template: >
        {% set array = { 'clear-night':'night',
                          'cloudy':'cloudy',
                          'exceptional':'sunny',
                          'fog':'fog',
                          'hail':'hail',
                          'lightning':'lightning',
                          'lightning-rainy':'lightning-rainy',
                          'partlycloudy':'partly-cloudy',
                          'rainy':'rainy',
                          'snowy':'snowy',
                          'snowy-rainy':'snowy-rainy',
                          'sunny':'sunny',
                          'windy':'windy' } %}
        {% set icon = array[state_attr('sensor.weatherflow_forecast_hourly','forecast').1.condition] %}
        mdi:weather-{{ icon }}

overly simplified yours:

{% set depth_cm = 33 %}
{% set lookup_table = {
  32: 361.91285505869,
  33: 373.22263177927,
  34: 384.53240849986
} %}
{{ lookup_table[depth_cm] | float }}

Thank you, will give that a go today. I managed to get original code to work, was missing some ,

However when the sensor is reading 66cm it’s giving the wrong volume of oil from the lookup table.

all working now thank you