How to use multiple inputs of different types (entity, bool) in blueprints

I have a blueprint that is switching on/off a entity(FAN) based on two humidity sensors data.
I have some situations were I need to block this condition to happen, and I added an input that can be a switch or a input_boolean. What I am trying to do is if any of the entities or input bools are True or On, I want to leave the FAN switch on and not allow the humidity difference to turn it off. It seemed to work with one entity but not with multiple: true. any ideas?

blueprint:
  name: Humidity Difference
  description: Turn a fan on and off based on the difference between a humidity sensor and a baseline
  domain: automation
  input:
    humidity_sensor:
      name: Humidity Sensor
      description: A sensor that measures the humidity of the area
      selector:
        entity:
          domain:
          - sensor
          multiple: false
    reference_humidity:
      name: Reference Humidity
      description: A percentage point value that indicates the baseline humidity if there is no reference sensor available
      default: 60
    reference_humidity_sensor:
      name: Reference Humidity Sensor
      description: A sensor that indicates the baseline humidity of the location
      selector:
        entity:
          domain:
          - sensor
          multiple: false
      default: []
    fan_switch:
      name: Fan Switch
      description: A switch that turns the fan on and off
      selector:
        entity:
          domain:
          - switch
          multiple: false
    blocking_entities:
      name: Blocking entities
      description: Switches or booleans that block the fan from turning off (optional)
      selector:
        entity:
          domain:
          - switch
          - input_boolean
          multiple: true
      default: []
    rising_threshold:
      name: Rising Threshold
      description: How many percentage points above the reference humidity the sensor can rise before the fan is turned on
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
      default: 8
    falling_threshold:
      name: Falling Threshold
      description: How many percentage points above the reference humidity the sensor must fall to before the fan is turned off
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
      default: 3
trigger:
- entity_id: !input humidity_sensor
  platform: state
- entity_id: !input reference_humidity_sensor
  platform: state
condition: []
action:
- service: switch.turn_{{ mode }}
  entity_id: !input fan_switch
variables:
  reference_humidity: !input reference_humidity
  humidity_sensor: !input humidity_sensor
  reference_humidity_sensor: !input reference_humidity_sensor
  fan_switch: !input fan_switch
  blocking_entities: !input blocking_entities
  blocking_entities_state: >
    {% set result = False %}
    {% for switch in blocking_entities %}
      {% if states(switch) == 'on' %}
        {% set result = True %}
        {% break %}
      {% endif %}
    {% endfor %}
    {{ result }}
  switch_state: '{{ states(fan_switch) }}'
  rising_threshold: !input rising_threshold
  falling_threshold: !input falling_threshold
  difference: '{{ states(humidity_sensor)|float - (states(reference_humidity_sensor)|float or reference_humidity|float) }}'
  mode: >
    {% if blocking_entities_state %}
      on
    {% elif switch_state == 'off' and difference|float > rising_threshold|float %}
      on
    {% elif switch_state == 'on' and difference|float < falling_threshold|float %}
      off
    {% else %}
      on
    {% endif %}

mode: single

Hello Mardari Grigore,

When you go to multiple: true, the output is now a list and not just one thing. [thing1,thing2,keepsGoingIfNeeded]
Even if there is only one, that becomes a list of one [thing1] kind of thing.
You would need to change your code accordingly.

The loop is unecessary:

{{ blocking_entities | select('is_state', 'on') | list | count | bool }}

If, for some reason, you prefer to use a loop it needs to use a namespace to extract the value:

{% set ns = namespace(result = False) %}
{% for switch in blocking_entities %}
  {% if states(switch) == 'on' %}
    {% set ns.result = True %}
    {% break %}
  {% endif %}
{% endfor %}
{{ ns.result }}

I would also suggest using a number selector for the reference_humidity input and constructing the difference variable definition without the or, using the float’s default instead:

difference: |
  {% set ref = states(reference_humidity_sensor) | float(reference_humidity)  %}
  {{ states(humidity_sensor)|float - ref }}
1 Like

OMG, it is working. Thank you very much. ChatGPT has failed me this time.

ChatGPT is a language generator, and home assistant is not one of it’s languages. It can write pseudo code that looks right, and sometimes even gives you code that works, but it had no idea of syntax or what it is actually doing. NEVER trust it. It makes shit up and does crazy stuff sometimes.

Now if someone taught an LLM to actually do Home Assistant YAML and Jinja, then we would have something. Currently doesn’t exist TMK.

1 Like