Templating sensor.in_range

I have an input.number defined in HA:

  - platform: homeassistant          # Set drum high level from HA            
    name: "Set drum high level"
    entity_id: input_number.drum_high_level_setpoint #defined in homeassistant
    id: high_level_setpoint

and need to use it in sensor.in_range:

          condition:
            sensor.in_range:
              id: drum_water_level_sensor
              above: !lambda 'return ((id(high_level_setpoint).state));' 

but I can’t get it to pass validation.

Is my syntax incorrect or can’t the range be templated?

The docs don’t say it can, so, if that is the case, how else could I achieve this?

1 Like

Well, having exhausted all options, it seems it’s not doable.

So, for completeness, here’s my solution using a script. Not ideal, but as far as I can see it’s the only way to achieve it.

esphome:
  name: water-level-sensor-test

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  ap:
    ssid: "water-level-test-hotspot"
    password: !secret OTA_password

captive_portal:

web_server:
  port: 80
  auth:
    username: admin
    password: !secret OTA_password

time:
  - platform: homeassistant     # Sync time with HA
    id: homeassistant_time 

i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true
  


ads1115:
  - address: 0x48
    id: ads1115_48
    continuous_mode: on

sensor:
  - name: "Drum water level sensor"
    id: drum_water_level_sensor
    platform: ads1115
    ads1115_id: ads1115_48
    multiplexer: 'A0_GND'
    update_interval: 1s
    unit_of_measurement: "Cm"
    icon: "mdi:gauge"
    accuracy_decimals: 3
  #  gain: 6.144 #(measures up to 6.144V)
    gain: 4.096 #(measures up to 4.096V)
  #  gain: 2.048 #(measures up to 2.048V)
  #  gain: 1.024 #(measures up to 1.024V)
  #  gain: 0.512 #(measures up to 0.512V)
  #  gain: 0.256 #(measures up to 0.256V)
    # filters:
    # - median:
    #     window_size: 7
    #     send_every: 4
    #     send_first_at: 3
    on_value:
      then:
        - script.execute: set_drum_level_binary_sensors

  - platform: homeassistant          # Set drum high level from HA            
    name: "Set drum high level"
    entity_id: input_number.drum_high_level_setpoint #defined in homeassistant
    id: high_level_setpoint

  - platform: homeassistant          # Set drum low level from HA            
    name: "Set drum low level"
    entity_id: input_number.drum_low_level_setpoint #defined in homeassistant
    id: low_level_setpoint

      
binary_sensor:

  - platform: status
    name: "Koi pond pressure Status"

  - platform: template              # Drum low level is up to HA
    name: "Drum low level"
    id: drum_low_level_reached

  - platform: template              # Drum low level is up to HA
    name: "Drum high level"
    id: drum_high_level_reached

script:
  - id: set_drum_level_binary_sensors
    then:
      - if:
          condition:
              - lambda: 'return id(drum_water_level_sensor).state <= id(low_level_setpoint).state;'
          then:
            - binary_sensor.template.publish:
                id: drum_low_level_reached
                state: ON
            - binary_sensor.template.publish:
                id: drum_high_level_reached
                state: OFF
      - if:
          condition:
              - lambda: 'return id(drum_water_level_sensor).state >= id(high_level_setpoint).state;'
          then:
            - binary_sensor.template.publish:
                id: drum_low_level_reached
                state: OFF
            - binary_sensor.template.publish:
                id: drum_high_level_reached
                state: ON
      - if:
          condition:
            and:
              - lambda: 'return id(drum_water_level_sensor).state < id(high_level_setpoint).state;'
              - lambda: 'return id(drum_water_level_sensor).state > id(low_level_setpoint).state;'
          then:
            - binary_sensor.template.publish:
                id: drum_low_level_reached
                state: OFF
            - binary_sensor.template.publish:
                id: drum_high_level_reached
                state: OFF
1 Like