Switch AC unit to OFF depending on day

I think I’m not seeing the wood for the trees with merging two automations into one.

I am using two automations for switching some AC units to OFF in the morning depending on whether it is a working day/school day or whether it is holidays or weekends.

As two separate automations, one for work days/school days and another automation for switching the AC units to OFF on holidays/weekends this is working perfectly for years now.

Now I want these two automations mergend into one but it seems I am missing something: The merged two automations simply don’t trigger. The “trace” is showing me that at step “choose” the automation simply jumps to the end without actually doing the “choosing”:

Here comes my latest try which is also simply refusing to fire correctly at the given times. The template editor reports no error (today is Sunday, AC in state “Cool”) and also activating the automations renders no errors. It simply doesn’t fire.

  trigger:
    - platform: time
      at:
        - '05:00:02'
      id: "school_days"
    - platform: time
      at:
        - '07:00:02'
      id: "holidays"
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: "school_days"
            - condition: template
              value_template: >
                {{ is_state('calendar.kalender_local', 'off') }} or {{
                is_state('binary_sensor.workday_sensor', 'on') }} and {{
                is_state('climate.master_bedroom_ac', 'cool') }}
          sequence:
            - service: climate.set_temperature
              target:
                entity_id: climate.master_bedroom_ac
              data:
                temperature: 25
            - service: climate.set_fan_mode
              target:
                entity_id: climate.master_bedroom_ac
              data:
                fan_mode: auto
            - delay:
                seconds: 1
            - service: climate.turn_off
              target:
                entity_id: climate.master_bedroom_ac
        - conditions:
            - condition: trigger
              id: "holidays"
            - condition: template
              value_template: >
                {{ is_state('calendar.kalender_local', 'on') }} or {{
                is_state('binary_sensor.workday_sensor', 'off') }} and {{
                is_state('climate.master_bedroom_ac', 'cool') }}
          sequence:
            - service: climate.set_temperature
              target:
                entity_id: climate.master_bedroom_ac
              data:
                temperature: 25
            - service: climate.set_fan_mode
              target:
                entity_id: climate.master_bedroom_ac
              data:
                fan_mode: auto
            - delay:
                seconds: 1
            - service: climate.turn_off
              target:
                entity_id: climate.master_bedroom_ac
  mode: single

Where did I went wrong here? :thinking:

Your templates.
It’s one template not three.

             {{ is_state('calendar.kalender_local', 'off') or
                is_state('binary_sensor.workday_sensor', 'on') and
                is_state('climate.master_bedroom_ac', 'cool') }}
        
1 Like

Thank you for the reply.

I’ve checked your template with the editor and it shows me:

Please correct me if I’m wrong but is the workday sensor and the climate-state sensor also getting checked this way? Wondering because the template checker just tells me that calendar.kalender_local is getting into account.

Addendum: Played around with it and your solution makes the automations work perfectly now :+1:t3: - Thank you Hellis :hugs:

Unfortunately the automation is still not working as it should. Today the kids have holidays. The automation fired at 05:00 but skiped firing at 07:00 alltogether.

As for the first condition block for: id: "school_days"

value_template: >
   {{ is_state('calendar.kalender_local', 'off') or
       is_state('binary_sensor.workday_sensor', 'on') and
       is_state('climate.master_bedroom_ac', 'cool') }}

(IF) the kids have school today (not holiday):

- condition: template
  value_template: >
    {{ is_state('calendar.kalender_local', 'off') }}

which results correctly to:

  • condition: template
    value_template: >
    False

(OR) today is not an official public holiday:

- condition: template
   value_template: >
     {{ is_state('binary_sensor.workday_sensor', 'on') }}

which results correctly to:

  • condition: template
    value_template: >
    True

(AND) the AC unit is not OFF:

- condition: template
   value_template: >
     {{ is_state('climate.master_bedroom_ac', 'cool') }}

which results correctly to:

  • condition: template
    value_template: >
    True

But then, putting all three conditions back into one block:

 - condition: template
   value_template: >
     {{ is_state('calendar.kalender_local', 'off') or
         is_state('binary_sensor.workday_sensor', 'on') and
         is_state('climate.master_bedroom_ac', 'cool') }}

I expect the result of getting false. Which should skip:
id: "school_days"

and jump straight to:
id: "holidays"

Instead when checking the condition block id: "school_days" through the template editor the result is:

Here is the whole automation:

  trigger:
    - platform: time
      at:
        - '05:00:02'
      id: "school_days"
    - platform: time
      at:
        - '07:00:02'
      id: "holidays"
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: "school_days"
            - condition: template
              value_template: >
                {{ is_state('calendar.kalender_local', 'off') or
                    is_state('binary_sensor.workday_sensor', 'on') and
                    is_state('climate.master_bedroom_ac', 'cool') }}
          sequence:
            - service: climate.set_temperature
              target:
                entity_id: climate.master_bedroom_ac
              data:
                temperature: 25
                hvac_mode: cool
            - service: climate.set_fan_mode
              target:
                entity_id: climate.master_bedroom_ac
              data:
                fan_mode: auto
            - delay:
                seconds: 1
            - service: climate.turn_off
              target:
                entity_id: climate.master_bedroom_ac
        - conditions:
            - condition: trigger
              id: "holidays"
            - condition: template
              value_template: >
                {{ is_state('calendar.kalender_local', 'on') or
                    is_state('binary_sensor.workday_sensor', 'off') and
                    is_state('climate.master_bedroom_ac', 'cool') }}
          sequence:
            - service: climate.set_temperature
              target:
                entity_id: climate.master_bedroom_ac
              data:
                temperature: 25
                hvac_mode: cool
            - service: climate.set_fan_mode
              target:
                entity_id: climate.master_bedroom_ac
              data:
                fan_mode: auto
            - delay:
                seconds: 1
            - service: climate.turn_off
              target:
                entity_id: climate.master_bedroom_ac
  mode: single

I’m already considering to move back to the two automations (one for holidays and the other one for school days) since having two automations works correctly for this.

However, still scratching my head…

In short, with :

value_template: >
   { is_state('calendar.kalender_local', 'off') or
      is_state('binary_sensor.workday_sensor', 'on') }}

Shouldn’t:

and:

result in “False”?

But instead checking with the tempate editor the above results in “True”:

:thinking:

Why would you expect the template {{ false or true }} to result in false?

And your earlier template is essentially {{ false or true and true }} which will be evaluated left-to-right so {{ (false or true) and true }} so the expected result should be true here as well.

1 Like

As for:

    - platform: time
      at:
        - '05:00:02'
      id: "school_days"

and:

value_template: >
  {{ is_state('calendar.kalender_local', 'off') or
       is_state('binary_sensor.workday_sensor', 'on') }}

The kids have school holidays, thus the entity for:
“calendar.kalender_local” shows “on” (but not “OFF”)

since it is not an official public holiday today, the entity for:
“binary_sensor.workday_sensor” shows “on”

Shouldn’t this result in “False” instead of “True” (since the first condition is not met)?

No. You use the word OR.
Read up on what and/or means and it will be clear

value_template: >
  {{ is_state('calendar.kalender_local', 'off') or
                                                ^ OR..
       is_state('binary_sensor.workday_sensor', 'on') }}

Thank you mekanek and Hellis81 for pointing me to the right direction :+1:t3:

I came up with the following where the results within the template editor are showing exactly what I expect in regards to the various combinations (states of binary_sensor workdays & calendar.kalender_local) for the times to turn OFF the AC units.

Will see tomorrow at 05:00 local time whether it fires according to the given conditions.

  trigger:
    - platform: time
      at: "05:00:02"
      id: school_days
    - platform: time
      at: "07:00:02"
      id: holidays
  condition: []
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: school_days
            - condition: state
              entity_id: climate.master_bedroom_ac
              state: cool
            - condition: template
              value_template: >
                {{ is_state('calendar.kalender_local', 'off') and
                is_state('binary_sensor.workday_sensor', 'on')  }}
          sequence:
            - service: climate.turn_off
              target:
                entity_id: climate.master_bedroom_ac
        - conditions:
            - condition: trigger
              id: holidays
            - condition: state
              entity_id: climate.master_bedroom_ac
              state: cool
            - condition: template
              value_template: >
                {{ is_state('calendar.kalender_local', 'on') and
                is_state('binary_sensor.workday_sensor', 'off') or
                is_state('calendar.kalender_local', 'on') and
                is_state('binary_sensor.workday_sensor', 'on') }}
          sequence:
            - service: climate.turn_off
              target:
                entity_id: climate.master_bedroom_ac

You don’t need to use templates for anything you’re doing, and in my opinion it makes understanding your code more complicated. Also in your second set of conditions you logic doesn’t make sense. That template condition will pass whenever calendar.kalendar_local is on regardless of the state of the workday sensor.

My guess is you’re trying to achieve this:

description: ""
mode: single
trigger:
  - platform: time
    at: "05:00:02"
    id: school_days
  - platform: time
    at: "07:00:02"
    id: holidays
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: school_days
          - condition: state
            entity_id: climate.master_bedroom_ac
            state: cool
          - condition: state
            entity_id: calendar.kalender_local
            state: "off"
          - condition: state
            entity_id: binary_sensor.workday_sensor
            state: "on"
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.master_bedroom_ac
            data: {}
      - conditions:
          - condition: trigger
            id: holidays
          - condition: state
            entity_id: climate.master_bedroom_ac
            state: cool
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.workday_sensor
                state: "on"
              - condition: state
                entity_id: calendar.calendar_local
                state: "on"
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.master_bedroom_ac
            data: {}
1 Like

Correct. The templates are just a remnant of another try using state conditions before but failed (caused by the “or” error). Will go back to the state conditions.

It does because the following situation happens:

1st. condition:
→ Kids have holidays.
→ It is a official public holiday.

but also (or):

2nd. condition:
→ Kids have holidays.
→ It is not a official public holiday.

Your template is doing what you’ve explained in English. But I don’t understand why you’d want to do that, because those two sets of conditions could be replaced by the singular condition
→ Kids have holidays.

The public holiday portion is serving no purpose. You essentially are saying the condition should pass if “the kids have holidays, regardless of whether or not it is an official public holiday”.

If you’re sure that is what you want, just remove the template condition and replace it by this single state condition:

          - condition: state
            entity_id: calendar.kalender_local
            state: "on"

OMG. You are absolutely correct! I was thinking around too many corners here :sweat_smile:

Thank you mekaneck :hugs: