Template sensor with a max of 100 percent for ventilation

I’m using a sensor to determine at what speed my ventilation would run (and this sends it to my KNX-system). This is working great, except one thing: when it counts to a number of +100 (percent) it won’t change it anymore and it stays at the previous state.

So what I’m looking for is a way to add to the value_template: if +99, set to 100.

  - platform: template
    sensors:
      ventilatiesnelheid:
        friendly_name: ventilatiesnelheid
        unit_of_measurement: 'procent'
        value_template: >      
          {% set total = 15 %}
          {% if is_state('input_boolean.wcboven', 'on') %}
            {% set total = total + 20 %}
          {% endif %}
          {% if is_state('input_boolean.wcbeneden', 'on') %}
            {% set total = total + 20 %}
          {% endif %}     
          {% if is_state('input_boolean.douche', 'on') %}
            {% set total = total + 15 %}
          {% endif %}
          {% if is_state('person.a, 'home') %}
            {% set total = total + 5 %}
          {% endif %} 
          {% if is_state('person.b, 'home') %}
            {% set total = total + 5 %}
          {% endif %}
          {% if is_state('input_boolean.gast', 'on') %}
            {% set total = total + 30 %}
          {% endif %}          
          {% if is_state('input_boolean.kuisvrouwaanwezig', 'on') %}
            {% set total = total + 25 %}
          {% endif %} 
          {% if is_state('input_boolean.oppasaanwezig', 'on') %}
            {% set total = total + 20 %}
          {% endif %}
          {% if is_state('sensor.buitentempverschil', 'on') %}
            {% set total = total - 5 %}
          {% endif %}
          {% if is_state('input_boolean.luchtkwaliteit', 'on') %}
            {% set total = total + 10 %}
          {% endif %}      
          {% if is_state('input_boolean.badkamervochtig', 'on') %}
            {% set total = total + 35 %}
          {% endif %}
          {{total}}

Put a if total > 100 Block at the end or use min() Template Designer Documentation — Jinja Documentation (3.0.x)

1 Like

Change the last line of the template from this:

{{ total }}

to this:

{{ [total, 100] | min }}

It will report the smaller of the two numbers so if total exceeds 100 then 100 is reported.

1 Like

Thank you both for a working solution!

If you’re interested, your Template Sensor can be reduced to this:

  - platform: template
    sensors:
      ventilatiesnelheid:
        friendly_name: ventilatiesnelheid
        unit_of_measurement: 'procent'
        value_template: >      
          {% set devices = { 'input_boolean.wcboven': 20, 'input_boolean.wcbeneden': 20,
                             'input_boolean.douche': 15, 'person.a': 5, 'person.b': 5,
                             'input_boolean.gast': 30, 'input_boolean.kuisvrouwaanwezig': 25,
                             'input_boolean.oppasaanwezig': 20, 'sensor.buitentempverschil': -5,
                             'input_boolean.luchtkwaliteit': 10, 'input_boolean.badkamervochtig': 35 } %}
          {% set ns = namespace(total = 15) %}
          {% for k in devices.keys() if is_state(k, 'home' if 'person.' in k else 'on') %}
            {% set ns.total = ns.total + devices[k] %}
          {% endfor %}
          {{ [ns.total, 100] | min }}

It’s easier to maintain because if you want to add/remove an entity, or modify its value, it’s all located in one place in the devices variable. There’s no need to modify the remaining code which simply computes the sum of all entities that are currently on or home if it’s a person entity.

1 Like