Using variable as input for filter

Hi, I would like to use a variable or input field the define the value for a filter.
Like in the example shown her for max_value, tried al sorts of things, nothing worked so far.

  - platform: integration
    name: "Battery SOC"
    id: battery_soc
    sensor: wattage_calculated
    integration_method: "left"
    time_unit: h
    restore: true
    device_class: energy
    filters:
        - clamp:            
            max_value: 640
            ignore_out_of_range: true
            max_value: !lambda return id(global_variable);

So far as I know, it require float - not templeatable.

Compilation time message as well:

      - clamp: 
          
          expected float.
          max_value: !lambda |-
            return id(global_variable);

You can try like this:

sensor:
  - platform: uptime
    type: seconds
    name: Uptime Sensor
    update_interval: 1s
    filters:
      lambda: |-
        if (x < id(max_val).state ) 
            {return x;}
          else
            return {};

number:
  - platform: template
    name: "Template number"
    id: max_val
    optimistic: true
    min_value: 0
    max_value: 300
    initial_value: 20
    step: 1

Bummer. I assume you defined the global variable as a float?

So i guess it can’t be done?
Maybe something for the WTH what the heck section?

You can see at about line 444 here that under the hood ESPHome is doing something similar to my suggestion above. So that would be the workaround.

https://esphome.io/api/sensor_2filter_8cpp_source