Need help with added automation logic. Turn on specific switch based on room temp

I have an automation for my office that currently does the following logic:

If office.wall_switch is on
turn on office.speakers
turn on office.mixer
turn on office.neon
turn on office.fan
notify.my_phone }

Via config:

- id: '1601403496491'
  alias: Office On
  description: Turn on / Off office equipment from the wall
  trigger:
  - platform: device#wall_switch
    type: turned_on
    device_id: 5065b9a3027e11ebb1edef6033660a86
    entity_id: light.inovelli_unknown_type_aa00_id_aa02_level
    domain: light
  condition: []
  action:
 - type: turn_on #neon
    device_id: e6b9c94d04d211eb813d0516c7e42527
    entity_id: switch.02014880c82b96522a96
    domain: switch 
  - type: turn_on #speakers
    device_id: 1447309601b111ebbe397f316a3028e9
    entity_id: switch.inovelli_unknown_type_ff00_id_ff05_switch_3
    domain: switch
  - type: turn_on  #mixer
    device_id: e6b7da1204d211eba6e237ae79e431b4
    entity_id: switch.53720850c44f33c31323
    domain: switch
  - type: turn_on #fan
    device_id: e6b8f8d604d211ebaacbfd22cb766353
    entity_id: switch.02014880d8f15ba08242
    domain: switch
  - service: notify.mobile_app_pixel_4_xl 
    data:
      message: Office Turned On
  mode: single

This has been working fine but now that it is getting colder I would like to add some logic to consider the temperature in the office and turn on the fan if it is above a certain temperature and the space heater if below a certain temperature.

If office.wall_switch is on
turn on office.speakers
turn on office.mixer
turn on office.neon
if office.temp under 68
turn on office.heater
else if office.temp over 75
turn on office.fan
notify.my_phone

I am trying to understand the best way to implement this added condition/logic. I want to be certain that the fan and heater aren’t on at the same and that neither is on if temp is between the high (fan) and low (heat). I was looking at conditions within the actions but that dead-ended quick since the actions stop if evaluation = false.

Thank you in advance.

If I understood your requirements correctly, this should work:

- alias: Example Office On
  mode: single
  variables:
    office: "{{ states('sensor.my_office_temperature') }}"
  trigger:
  - platform: state
    entity_id: light.inovelli_unknown_type_aa00_id_aa02_level
    to: 'on'
  action:
  - service: switch.turn_on
    entity_id: 
    - switch.02014880c82b96522a96
    - switch.inovelli_unknown_type_ff00_id_ff05_switch_3
    - switch.53720850c44f33c31323
  - choose:
    - conditions: "{{ office | int < 68 }}"
      sequence:
      - service: switch.turn_on
        entity_id: switch.my_heater
    - conditions: "{{ office | int > 75 }}"
      sequence:
      - service: switch.turn_on
        entity_id: switch.02014880d8f15ba08242
  - service: notify.mobile_app_pixel_4_xl 
    data:
      message: Office Turned On

Let me know if you need clarification for any aspect of the automation.


EDIT

Added the use of variables to help streamline the templates used in the conditions.

1 Like

Thank you, I appreciate you taking the time to respond. This appears to work as expected. I didn’t realize I could use variables nor did I know that you could use conditions the way presented. I can use both to simplify some of my other automations. Thanks again!

1 Like

You’re welcome!

You should know that the automation employs new functionality introduced in version 0.113.

choose allows you to introduce ‘branches’ in the logic’s flow. If you are familiar with other forms of programming, it’s like a chain of if-elif statements (or a case statement). In this automation, if the office temperature is below 68 then the heater is turned on. If it is above 75, the fan is turned on. If it is neither below 68 or above 75 (i.e. it is between the two temperatures), neither the heater or fan are turned on.

variables let you define a variable that can be referenced anywhere within the automation. However it cannot be referenced outside of the automation. In this automation, we define a variable called office based on a sensor’s value. The variable is referenced twice by two separate conditions.

The automation also uses the new compact way of defining a Template Condition. Instead of the traditional way (which is still valid):

    - conditions:
        condition: template
        value_template: "{{ office | int < 68 }}"

it uses the compact format:

    - conditions: "{{ office | int < 68 }}"
1 Like