Template Sensor with limit

Hi I need to create a sensor that adds a number to another existing sensor, but it must not exceed 100, I created the template and it works, but it exceeds 100, how can I do?

this is my template:

  - platform: template
    sensors:
     umidita_sottocantina:
        value_template: '{{(states(''sensor.umidita_cantina'') | float + 36 ) | round (1)}}'
        friendly_name: 'Umidità Sottocantina'
        unit_of_measurement: '%'

Please format your code properly.
You can use the min() function

min(x, 100)
1 Like

i have format the code, where i put

min(x, 100) ?

  - platform: template
    sensors:
     umidita_sottocantina:
        value_template: >-
          {% set x = (states('sensor.umidita_cantina') | float + 36 ) | round (1) %}
          {{ ([0, x, 100] |sort) [1] }}
        friendly_name: 'Umidità Sottocantina'
        unit_of_measurement: '%'

You can customize 0 as lower limit and 100 as upper limit.

        value_template: '{{ min((states(''sensor.umidita_cantina'') | float + 36 ) | round (1), 100) }}'
2 Likes

Thanks Work!