Looking for constructive criticism of automation

Firstly, I’m aware that there are thermostat scheduling integration and blueprints out there. But I wanted to try my own as a learning experience. I just completed this and noticed there are numerous dangling “else” statements. I believe that is normal. Also, I’m using multiple triggers so I changed the mode to “restart”. I believe this is necessary in this case. Please correct me if I am wrong.

Even destructive criticism is welcome! :slight_smile:

-Thanks

alias: Thermostat - Downstairs Schedule
description: ""
triggers:
  - trigger: time
    at: "05:30:00"
    id: Downstairs - 5:30 AM
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
  - trigger: time
    at: "11:00:00"
    id: Downstairs - 11 AM
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
  - trigger: time
    at: "23:30:00"
    id: Downstairs - 11:30 PM
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
conditions: []
actions:
  - if:
      - condition: trigger
        id:
          - Downstairs - 5:30 AM
    then:
      - action: climate.set_temperature
        metadata: {}
        data:
          hvac_mode: heat_cool
          target_temp_high: 78
          target_temp_low: 67
        target:
          entity_id: climate.thermostat_downstairs
      - if:
          - condition: trigger
            id:
              - Downstairs - 11 AM
        then:
          - action: climate.set_temperature
            metadata: {}
            data:
              hvac_mode: heat_cool
              target_temp_high: 78
              target_temp_low: 68
            target:
              entity_id: climate.thermostat_downstairs
          - if:
              - condition: trigger
                id:
                  - Downstairs - 11:30 PM
            then:
              - action: climate.set_temperature
                metadata: {}
                data:
                  hvac_mode: heat_cool
                  target_temp_high: 79
                  target_temp_low: 65
                target:
                  entity_id: climate.thermostat_downstairs
    else:
      - if:
          - condition: trigger
            id:
              - Downstairs - 11 AM
        then:
          - action: climate.set_temperature
            metadata: {}
            data:
              hvac_mode: heat_cool
              target_temp_high: 78
              target_temp_low: 68
            target:
              entity_id: climate.thermostat_downstairs
mode: restart

Those nested Ifs will never do anything because any given run of an automation will only ever have 1 trigger. As it is currently configured the 5:30am trigger will produce the desired results and the 11am trigger will work in the Else. The 11:30pm trigger will never do anything.

For branched logic where all the options are mutually exclusive, like times, the Choose action is a better fit than If/Then. Especially when there are more than 2 possible options.

If you just want to do it with If/Then, it is possible. You just need to un-nest the Ifs so that each of them is always checked:

alias: Thermostat - Downstairs Schedule
description: ""
triggers:
  - trigger: time
    at: "05:30:00"
    id: Downstairs - 5:30 AM
  - trigger: time
    at: "11:00:00"
    id: Downstairs - 11 AM
  - trigger: time
    at: "23:30:00"
    id: Downstairs - 11:30 PM
conditions: []
actions:
  - if:
      - condition: trigger
        id:
          - Downstairs - 5:30 AM
    then:
      - action: climate.set_temperature
        metadata: {}
        data:
          hvac_mode: heat_cool
          target_temp_high: 78
          target_temp_low: 67
        target:
          entity_id: climate.thermostat_downstairs
  - if:
      - condition: trigger
        id:
          - Downstairs - 11 AM
    then:
      - action: climate.set_temperature
        metadata: {}
        data:
          hvac_mode: heat_cool
          target_temp_high: 78
          target_temp_low: 68
        target:
          entity_id: climate.thermostat_downstairs
  - if:
      - condition: trigger
        id:
          - Downstairs - 11:30 PM
    then:
      - action: climate.set_temperature
        metadata: {}
        data:
          hvac_mode: heat_cool
          target_temp_high: 79
          target_temp_low: 65
        target:
          entity_id: climate.thermostat_downstairs
mode: single

With the current triggers and actions, the restart will never be needed. That mode is only needed in cases where you need to break into a running automation because it is taking time doing something. The actions you have should be completed in microseconds and your triggers are hours apart.

There are a number of ways to make this automation more compact / less verbose.

Thank you! I’ll certainly look into the “Choose action” option.

Your automation is designed to perform one action (climate.set_temperature) but using different temperature values depending on the time of day. The temperature values can be specified as trigger variables in each one of the three Time Triggers.

alias: Thermostat - Downstairs Schedule
description: ""
triggers:
  - trigger: time
    at: "05:30:00"
    variables:
      hi: 78
      lo: 67
  - trigger: time
    at: "11:00:00"
    variables:
      hi: 78
      lo: 68
  - trigger: time
    at: "23:30:00"
    variables:
      hi: 79
      lo: 65
conditions: []
actions:
  - action: climate.set_temperature
    metadata: {}
    data:
      hvac_mode: heat_cool
      target_temp_high: "{{ hi }}"
      target_temp_low: "{{ lo }}"
    target:
      entity_id: climate.thermostat_downstairs
mode: single

In effect, the decision-making (which temperature value to use) becomes part of triggers so actions doesn’t need to decide (using if statements or condition) what to do.

2 Likes

Thank you I’ll study this further. Right now I’m struggling with a Reolink doorbell.

-Thanks

Update: That is much cleaner, using the variables. Thanks again!

Question – I’m a YAML novice. Is there any way to see the variables on the visual editor?

Also, I’m curious as to why when I added an additional time trigger for testing purposes, it also copied the hi and lo variables. Not sure why it did that!

-Thanks

Currently, the Automation Editor’s visual mode doesn’t show everything that is available to an automation in YAML mode.

Got it thank you. And thanks again for the help with the thermostat scheduler. And YAML syntax!