Max & Min constraints in Template Sensor using separate Input Number

Hello all,

A puzzle I’m trying to solve which I could use some help with. I am coding a template sensor, and have it currently set so that the output has a minimum value of 0 and a maximum value of 9:

  - sensor:
      - name: "soc_required_charge_plus_boost"
        unique_id: "soc_required_charge_plus_boost"
        unit_of_measurement: 'kWh'
        device_class: energy
        state: "{{ [[states('sensor.soc_required_charge')| float(0) + states('input_number.boost_charge')|float(0) | round(2), 0.0 ] | max , 9.0 ] | min }}"

My question is - rather than having 9.0 as my maximum output value, is it possible to define this value using a separate input.number helper? So that the maximum output of this template sensor could be toggled by a user on-the-fly in the UI without changing any code?

Thank you in advance for your consideration :slight_smile:
Jev

Yes

  - sensor:
      - name: "soc_required_charge_plus_boost"
        unique_id: "soc_required_charge_plus_boost"
        unit_of_measurement: 'kWh'
        device_class: energy
        state: |-
         {% set sum = (states('sensor.soc_required_charge')| float(0) + states('input_number.boost_charge') | float(0)) | round(2)%}
         {% set max = states('input_number.example') | float(0) %}
         {{ ([0, sum, max] | sort)[1] }}
1 Like

One word answers with code are the BEST response on this Forum :slight_smile:

Thank you @Didgeridrew for your assistance, it is greatly appreciated!