Need help on template multiple conditions automation for house climate control

I’ve been struggling for days with automation control for my house ventilation system. I can control 4 fan speeds and recirculation on/off with a relay card on ESPHome. This works great manually.
But the last days I’ve been trying to automate the system to keep inner climate cool by blowing full fan during nights when outsde temperature is low and during day time when outdoor is hot keepin low fan and turning on recirculation. I realized that I can’t make an automation script with both multiple conditions AND multiple actions depending on condition, so I made several automations but I’m struggling with the coonditions in each one of these when trying to use templating and I really need help. When run my automations the always come out as “TRUE” every time regardless of what values the temperature sensors show and I don’t know what I am doing wrong. So I need help with script or maybe how I can debug. The idea is that the automations would trigger once every hour and check the current conditions/temperatures and the set the correct fan-speed actions.

This is how my automations for each actions currently looks like:

- id: '1596296409426'
  alias: Climate Control - Cold outside and above 18 inside
  description: Normal fan
  trigger:
  - hours: '1'
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - condition: template
      value_template:  >-
        {% if states('sensor.utluft_temp')|int > 18 and states('sensor_utluft_temp')|int < 25 and states('sensor.inluft_temp)|int < states('sensor.utluft_temp')|int %}
        true
        {%else%}
        false
        {%endif%}
  action:
  - data: {}
    entity_id: switch.vent_fan2
    service: switch.turn_on
  - data: {}
    entity_id: switch.vent_recirc
    service: switch.turn_off
  mode: single
- id: '1596304013346'
  alias: Climate Control - Colder outside medium FAN
  description: Starting to get colder outside, set FAN medium
  trigger:
  - hours: '1'
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - condition: template 
      value_template: >-
        {% if states('sensor.utluft_temp')|int > 20 and states('sensor.utluft_temp')|int < 24 and states('sensor.utluft_temp')|int > states('sensor.inluft_temp')|int %}
        true
        {%else%}
        false
        {%endif%}
  action:
  - data: {}
    entity_id: switch.vent_fan3
    service: switch.turn_on
  - data: {}
    entity_id: switch.vent_recirc
    service: switch.turn_off
  mode: single
- id: '1596304338482'
  alias: Climate Control - Very hot inside set FULL FAN
  description: Very hot inside but cold outside, set full fan
  trigger:
  - hours: '1'
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - condition: template
      value_template: >-
        {% if states('sensor.utluft_temp')|int > 25 and states('sensor.inluft_temp')|int < states('sensor.utluft_temp')|int %}
        true
        {%else%}
        false
        {%endif%}
  action:
  - data: {}
    entity_id: switch.vent_fan4
    service: switch.turn_on
  - data: {}
    entity_id: switch.vent_recirc
    service: switch.turn_off
  mode: single
- id: '1596304574084'
  alias: Climate Control - Keep cool
  description: Colder inside than outside - keep cool inside
  trigger:
  - hours: '1'
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - condition: template
      value_template: >-
        {% if states('sensor.utluft_temp')|int <= states('sensor.inluft_temp')|int and states('sensor.utluft_temp')|int > 20 %}
        true
        {%else%}
        false
        {%endif%}
  action:
  - data: {}
    entity_id: switch.vent_fan1
    service: switch.turn_on
  - data: {}
    entity_id: switch.vent_recirc
    service: switch.turn_on
  mode: single

have you got the fans link to a climate control

#=======================================================================
# Office Fan control
#=======================================================================
  - platform: generic_thermostat
    name: "Office Temperature"
    heater: switch.office_fan
    target_sensor: sensor.office_temperature
    min_temp: 15
    max_temp: 30
    ac_mode: true
    target_temp: 24
    cold_tolerance: .5
    hot_tolerance: .5
    initial_hvac_mode: "off"
    away_temp: 16
#=======================================================================
'``
letting that control the fans

Thanks for the input on the Generic Thermostat myle. Unfortunately this will not work for me because i don’t have any dedicated active heater or air condition. I rely on outdoor and indoor temperatures and only want to make best optimisation of ventilation based on target temperature.
So I need to keep track of 2 temperatures (outdoor & indoor) and act differently depending on which of these are the coldest or warmest compared to target temperature. So temp sensors.
The Generic thermostat can only have one switch controlled per thermostat but I want to be able to regulate both 4 different fan speeds (4 switches) AND if the recirculation switch when i want to preserve the cold indoor-air.
When I read the generic_thermostat howto i don’t see that it can do all of the above. It’s not generic enough :slight_smile:
But if I could just get the automation working with the value_template I think it would be excellent.

That’s probably your first mistake. Automations should generally be triggered by events that should cause a change. If you feel yourself saying, “I’ll trigger the automation periodically and have it check …”, that’s probably a strong indicator that you’re approaching it less than optimally. Think about real thermostats; they don’t check conditions once an hour. They turn things on and off as thresholds are crossed, etc.

So think about the events that should cause the switches to be changed. E.g., “it just got too hot inside”, or “it’s cool enough now”, or “the difference between inside and outside just got …” or “it doesn’t matter what the temps are, the fan has just been on too long”, or …

Once you know what those are, and can express them in terms of the available sensors, then you’ll be in a good position to start writing appropriate automation triggers, grouping those into automations, etc.

Yes, I totally agree to that and this will probably be the case later om. But at this point I want to learn by hourly basis first. The problem is that ventilation/air change is a.very slow system regulatory wise and at this point I don’t want the fans to change speed several times a minute or hour just because I go back and forth a threshold over a certain temperature.
As the ventilation system is such a slow system I am totally ok at this point to check and only update to other fan speed once per hour. But maybe I’ll change it later on to trigger on the certain changes in indoor/outdoor temp.
But at this point I need help in understanding why my value_templates condition ALWAYS seems to come out true for all automations when I run them manually… regardless of the values in the sensors. I don’t know if the code is wrong or if I misunderstood how value_template conditions works. :slight_smile:

There are common ways of implementing constraints like that.

Ok, sure…

First, the value_template for a template condition should generally be written this way:

{{ BOOLEAN_EXPRESSION }}

Not this way:

{% if BOOLEAN_EXPRESSION %}true{% else %}false{% endif %}

The BOOLEAN_EXPRESSION will already result in true or false. There’s no point in saying “if true then true else false.” Of course it will work this way, but why make things harder than they need to be?

Regarding the expression in the template condition in your first automation:

states('sensor.utluft_temp')|int > 18 and states('sensor_utluft_temp')|int < 25 and states('sensor.inluft_temp)|int < states('sensor.utluft_temp')|int

I see one problem: states('sensor_utluft_temp')|int < 25

sensor_utluft_temp is not a valid entity_id, hence states() will return “unknown” which, when fed through the int filter will result in the default value of zero (because “unknown” cannot be interpreted as an integer), and zero is always less than 25.

But, that will only prevent that one sensor from being properly checked. It won’t cause the entire expression to always result in true.

Another problem is the trigger. That trigger will only fire at 01:00:00, not every hour. If you want a trigger that fires once an hour, then:

  trigger:
  - hours: '/1'
    platform: time_pattern

So, at this point, I really couldn’t say why the automations are always running.

But let’s take another step back. Are you saying that at 01:00:00, when the trigger fires, the actions are running no matter what the sensors are that are used in the conditions? If so, I don’t understand that.

OR, are you maybe manually triggering the automation to try and test it? If so, how exactly are you doing that? It’s possible the way you’re doing that is causing the conditions to be ignored.

Hi
Thanks for all the great inputs and I should really update accordingly.
As for you last question I haven’t turned on the automatic in the automations because I still doing the development so that’s why I didn’t bump into the 01:00 o’clock issue. At this point I’m only running the automation scripts manually from run at all automations. So might it be that if I run automations manually they ignore the conditions in it?

I’m not exactly sure what you mean by that.

If you run an automation using the EXECUTE button, then that simply causes it to run the actions. It ignores whether or not the automation is on, and ignores triggers and conditions.

If you want to test the condition and action parts of your automation, then you need to use the automation.trigger service from the SERVICES tab of the Developer Tools page, and specify the following in the Service Data box:

skip_condition: false