ESPhome: Template parameter as lambda?

Hi Community,

I want to have a certain value in max_value for my number-template, read from a sensor with lambda.
Is it wrong syntax, or just not working?
Any other ideas to make this work?

number:
  - platform: template
    name: "Sollwert"
    id: sollwert_vorgabe
    icon: "mdi:radiator"
    unit_of_measurement: "W"    
    optimistic: true
    min_value: 0
    max_value: !lambda |-
      return id(max_power).state;
    step: 10
    device_class: "power"
    on_value:
      then:
        - lambda: |-
            id(output_power).publish_state(x);

The error message from the compiler is:

expected float.
max_value: return id(max_power).state;

max_power is a float value, but gets a value first at startup of the esp.
The goal is to limit the range of the input number. The max_value can change sometimes.

Thanks for help!
Stefan

You cannot use a lambda for the max_value option. Like most options - except those that specifically say they can use lambdas in the documentation.

Fixed min and max values makes sense. If the boundaries are 0…100, you set its value to 90 and then change the max to 50, the value it holds would suddenly be outside its boundaries.

Okay, I understand.
Flexible max value would make sense in my case, because too high values given by the UI will be outside the range. The max. possible value can change and is set from a measurement in may case.

I adapted my code to check the given value somewhere else.

The project is a power regulator, and the connected power depends on the ac-devices, that are hooked on. The power is measured at startup of the ESP, but can also repeated later.
The max_value in the input field should be set to the max. power of the connected devices.

Is it then possible to reduce a actually too high value to my given maximum?
Like: Input number was 2000. Max. possible value is just 1500 → reduce the value to 1500 AND show this in the UI…

self referencing in the on_value lambda with

id(sollwert_vorgabe).publish_state(id(max_power).state)

did not work if I remember right.