How to write an automation with multiple if statements?

I am in the middle of setting up the automations in my home after finally getting almost all of my devices connected. I have an idea of how I want to handle some of the main automations, but not quite sure the best way to implement it.

I have a 3 bedroom home with with GE Z-Wave Dimmers setup in each bedroom.
Each bedroom along with the Livingroom have a mode override setup by way of input_select.
Modes are as follows:
Morning
Evening
Night
Movie
Party
Sleep
Away
Vacation

The house itself also has a mode override input_select as well.
It has one addtional value of “Manual”

The idea here is that I can move every room in the house into a specific mode by changing the House Override. Likewise if the house is in one mode, an individual room can move itself into another mode by way of automation or user input.

What I am looking to do is trigger an automation based on state of 3 rooms (3 input_select values) at the same time.
I am planning on setting up the double tap feature in the GE-Dimmers for each of the bedrooms.
The double tap will put the bedroom into sleep mode.
I want to create an automation that if all 3 bedrooms move to sleep mode, the house mode moves to sleep as well.

Any thoughts or ideas?

This will trigger when the third of the three bedrooms is put into sleep…

automation:
  trigger:
    platform: state 
    entity_id:
      - input_select.bedroom_one
      - input_select.bedroom_two
      - input_select.bedroom_three
    to: 'Sleep' 
  condition:
    - condition: state 
      entity_id: input_select.bedroom_one
      state: 'Sleep'
    - condition: state 
      entity_id: input_select.bedroom_two
      state: 'Sleep' 
    - condition: state 
      entity_id: input_select.bedroom_three
      state: 'Sleep' 
  action:
    [YOUR ACTIONS]

Although, presuming you will want an automation that runs when one or more come out of ‘sleep’ a template binary sensor that registers true if all 3 are ‘sleep’ and false in all other cases might be more effective.

2 Likes

@anon43302295 Thanks for the reply.
This looks good for now. I was wondering if multiple entities could be listed for the trigger.
Good to know.

Hello again @anon43302295

Wondering if you could point me in the right direction of making this automation a bit more complex.
Its still basically the same concept, but Id like to make the conditions dynamic. At least I think that would be the easiest way.

I want to take into account a few extra things that will change the required number of bedroom input_selects moving to the sleep state, to run the action of moving the house into sleep state.

One of the bedrooms is a guest room, so I only need this input select to be in sleep state if the guest mode is on (input_boolean.guest_mode).

Additionally Id like to track myself (device_tracker.bill) and my roommate’s (device_tracker.stacey) location. IE if we are home.

So basically I am looking to build an automation that if guest mode is off, and my roommate is away it will only need my bedroom input_select moving to sleep, to change the house to sleep.
Likewise, if we are both home, but guest is off, it would require both our rooms going into sleep, but not the guest room. There are obviously a few other variations as well, but I think you get it.

I can figure out how to do this with a seperate automation for each of the variations, but I was hoping to get some help with creating a single automation to take care of this.

Any thoughts?

Always helps to sleep first.
Im pretty sure this will do it, do you see anything wrong with the logic?

alias: Put The House To Sleep
trigger:
  platform: state
  entity_id: 
    - input_select.master_bedroom_override
	- input_select.front_bedroom_override
	- input_select.rear_bedroom_override
  to: 'Sleep'
condition:
  - condition: state
    entity_id: input_select.master_bedroom_override
    data_template:
	  state: >
        {%- if is_state('device_tracker.bill', 'home') %}
          'Sleep'  
        {%- else -%}
          'Automatic'
        {% endif %}
  - condition: state
    entity_id: input_select.front_bedroom_override
    data_template:
	  state: >
        {%- if is_state('input_boolean.guest_mode', 'on') %}
          'Sleep'  
        {%- else -%}
          'Automatic'
        {% endif %}
  - condition: state
    entity_id: input_select.rear_bedroom_override
    data_template:
	  state: >
        {%- if is_state('device_tracker.stacey', 'home') %}
          'Sleep'  
        {%- else -%}
          'Automatic'
        {% endif %}
action:
  - service: input_select.set_options
    data:
      entity_id: input_select.house_override
      options: 'Sleep'

The logic looks sound, but I don’t know if you can add a template like that to conditions, so you might need to do it as a template condition that resolves to true or false.

Try it like that, and if it doesn’t validate let me know and I’ll try and help you get the logic rewritten in to template conditions if you need me to. :+1:

Guess you were right.

Failed config
automation:
- Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘data_template’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘condition’]. Got None. (See /config/configuration.yaml, line 103).

Not sure what you were suggesting with the template condition.

So the first condition should be (something like):

condition: template 
value_template: >
  {% if is_state('device_tracker.bill', 'home') and ('input_select.master_bedroom_override', 'Sleep') %}true
  {% elif is_state('device_tracker.bill', 'not_home') and ('input_select.master_bedroom_override', 'Automatic') %}true
  {% else %} false {% endif %}

So like this in total?

condition:
  - condition: template 
    value_template: >
      {% if is_state('device_tracker.bill', 'home') and ('input_select.master_bedroom_override', 'Sleep') %}true
      {% elif is_state('device_tracker.bill', 'not_home') and ('input_select.master_bedroom_override', 'Automatic') %}true
      {% else %} false {% endif %}

Question, is the ‘not_home’ a real value? When not in the home state it shows as Away, but there is also a change the user could be in another of my setup zones. So does not_home mean any zone but home?

Yes, and you’ll have to repeat the logic with the other conditions.

And yes, not_home == anywhere that’s not home.

One sec, something odd is going on in my system

Ok figured it out. Re-wrote all three scripts then when I went to delete the old ones, I accidently left one old and deleted one new. I think I need another cup of coffee.

Ok so the checker goes through correctly now. Thanks for your help.

I have been looking for a way to do “does not equal” to cut down on the number of things I need to track in conditions. Does the “not_X” work with anything? Im not too bad at working out logic, but considering I failed spanish class 2 times back in the day, Im guessing it takes me a while to pick up on syntax.

Here is the full working automation in case someone else comes along looking to do something similar.

alias: Put The House To Sleep
trigger:
  platform: state
  entity_id: 
    - input_select.master_bedroom_override
    - input_select.front_bedroom_override
    - input_select.rear_bedroom_override
  to: 'Sleep'
condition:
  - condition: template 
    value_template: >
      {% if is_state('device_tracker.bill', 'home') and ('input_select.master_bedroom_override', 'Sleep') %}true
      {% elif is_state('device_tracker.bill', 'not_home') and ('input_select.master_bedroom_override', 'Automatic') %}true
      {% else %} false {% endif %}
  - condition: template 
    value_template: >
      {% if is_state('input_boolean.guest_mode', 'on') and ('input_select.front_bedroom_override', 'Sleep') %}true
      {% elif is_state('input_boolean.guest_mode', 'off') and ('input_select.front_bedroom_override', 'Automatic') %}true
      {% else %} false {% endif %}
  - condition: template 
    value_template: >
      {% if is_state('device_tracker.stacey', 'home') and ('input_select.rear_bedroom_override', 'Sleep') %}true
      {% elif is_state('device_tracker.stacey', 'not_home') and ('input_select.rear_bedroom_override', 'Automatic') %}true
      {% else %} false {% endif %}
action:
  - service: input_select.set_options
    data:
      entity_id: input_select.house_override
      options: 'Sleep'

Does not equal:

Comparing numbers

{% if (states.sensor.home_humidity.state)  != 3 %} 

Comparing states

{% if not is_state('device_tracker.bill', 'home') %}

Hope this helps :+1:

Hey again @anon43302295
Not sure what is going on, but my roommate is finally back home so this automation can be tested correctly, but its not working as expected.

Its moving the input_select.house_override to Sleep no matter the conditions.
I tried adding the “condition: and” entry as shown below, but still not dice.
No matter how I change or what conditions are true/false it always fires the action and puts the house to sleep if I move any of the bedrooms into sleep mode.

alias: Put The House To Sleep
trigger:
  platform: state
  entity_id: 
    - input_select.master_bedroom_override
    - input_select.front_bedroom_override
    - input_select.rear_bedroom_override
  to: 'Sleep'
condition:
  condition: and
  conditions:
    - condition: template 
      value_template: >
        {% if is_state('device_tracker.bill', 'home') and ('input_select.master_bedroom_override', 'Sleep') %}
          true
        {% elif is_state('device_tracker.bill', 'not_home') and ('input_select.master_bedroom_override', 'Automatic') %}
          true
        {% else %}
          false
        {% endif %}
    - condition: template 
      value_template: >
        {% if is_state('input_boolean.guest_mode', 'on') and ('input_select.front_bedroom_override', 'Sleep') %}
          true
        {% elif is_state('input_boolean.guest_mode', 'off') and ('input_select.front_bedroom_override', 'Automatic') %}
          true
        {% else %}
          false
        {% endif %}
    - condition: template 
      value_template: >
        {% if is_state('device_tracker.stacey', 'home') and ('input_select.rear_bedroom_override', 'Sleep') %}
          true
        {% elif is_state('device_tracker.stacey', 'not_home') and ('input_select.rear_bedroom_override', 'Automatic') %}
          true
        {% else %}
          false
        {% endif %}
action:
  - service: input_select.select_option
    data:
      entity_id: input_select.house_override
      option: 'Sleep'

Check the templates in the template tool, maybe needs is_state for the second part of each template as well as the first?

Ha, didn’t even know that templating tool was there. That thing is handy.

So I figured it out, we need to add an additional “is_state” for the ‘and’ portions of the template.
So the full working version is as follows.

alias: Put The House To Sleep
trigger:
  platform: state
  entity_id: 
    - input_select.master_bedroom_override
    - input_select.front_bedroom_override
    - input_select.rear_bedroom_override
  to: 'Sleep'
condition:
  condition: and
  conditions:
    - condition: template 
      value_template: >
        {% if is_state('device_tracker.bill', 'home') and is_state('input_select.master_bedroom_override', 'Sleep') %}
          true
        {% elif is_state('device_tracker.bill', 'not_home') and is_state('input_select.master_bedroom_override', 'Automatic') %}
          true
        {% else %}
          false
        {% endif %}
    - condition: template 
      value_template: >
        {% if is_state('input_boolean.guest_mode', 'on') and is_state('input_select.front_bedroom_override', 'Sleep') %}
          true
        {% elif is_state('input_boolean.guest_mode', 'off') and is_state('input_select.front_bedroom_override', 'Automatic') %}
          true
        {% else %}
          false
        {% endif %}
    - condition: template 
      value_template: >
        {% if is_state('device_tracker.stacey', 'home') and is_state('input_select.rear_bedroom_override', 'Sleep') %}
          true
        {% elif is_state('device_tracker.stacey', 'not_home') and is_state('input_select.rear_bedroom_override', 'Automatic') %}
          true
        {% else %}
          false
        {% endif %}
action:
  - service: input_select.select_option
    data:
      entity_id: input_select.house_override
      option: 'Sleep'
1 Like