Light, Light, Cover Open (.102)

I would like to automate a routine and add a dash of salt. Our master bath has a huge walk-in closest connected to it. I have a small corner with chair for my quiet time. So on a work day, when I get up I go into the master bath and shut the door to keep from waking the wife up. I turn on the master bath light and do not turn it off till I am ready to leave. So when I turn the master bath light off, I want to turn on the kitchen light. That gives me enough light to get to the door that goes into the garage. Then when I turn off the kitchen light, then open the garage door. The time of day would be between 4:00 am and 7:00 am. I have my own workday sensor that if today is M-F and my calendar.vacation is not on, then it is yes. Then I go to my HA google calendar and add the vacation days. If It is a holiday that I am off, I just count it as vacation also.

Let’s use these entities for the sake of discussion.
light.masterbath
light.kitchen
cover.twocar
sensor.myworkday (‘yes’ or ‘no’)

I know I can keep some flags using input.boolean but I was wondering if there is a more creative way?

Maybe something like the following, starting with the automation:

- trigger:
  - platform: state
    entity_id: light.masterbath
    to: 'off'
  condition:
  - condition: time
    after: '04:00'
    before: '07:00'
  - condition: state
    entity_id: sensor.myworkday
    state: 'yes'
  action:
  - service: script.turn_off
    entity_id: script.leaving
  - service: script.leaving

And the script:

leaving:
  sequence:
  - service: light.turn_on
    entity_id: light.kitchen
  # Allow enough time for light's state to change
  - delay: 2
  - wait_template: "{{ is_state('light.kitchen', 'off') }}"
    timeout:
      minutes: 15
    continue_on_timeout: false
  - service: cover.open
    entity_id: cover.twocar

At least this is a start. I suspect you may need to refine it. Let me know if you have any questions.

@pnbruckner You gave me exactly what I was looking for. Something out of the box.
Calling this script is new to me. Let me do a little looking and I will reach back. Thanks.

@pnbruckner Thanks again, usually when I post a question it is either I don’t see my mistake or trying to expand my learning You did that with the script and now to throw another wrinkle. I may be getting close to having to use that input_boolean switch.
I now have my switches wired in and I am now to more of a “rubber meeting the road” situation. I now have the actual entities’ names.

One other event I can add is taking a shower. So after the master_bath light has been on for more than say 10 minutes and the master_shower light comes on, and in between the times, then I will be leaving for work. Actually the master_bath light on, is not that big of a deal.
The technique I am struggling with is the turning off the master_bath/master_shower light. In theory, they could go off in any order.

I wonder if the wait template could be used? Can I wait for both to happen in the same condition?


#******************************************************************************************
# Automate Paul Work Day Routines 
#******************************************************************************************

# ---- 
# In the morning, first thing is quite time, then shower, then leave so ..
#    The switches sequances would look like:
#       Master Bath On
#       Master Shower On
#       Master Bath/Shower off
#    and if it is a workday and between 4:00am and 7:30am
#    then I am leaving, so turn on the kitchen pendant light
#    If the pendant light is on, when I turn it off open garage door.
#    Opening the garage door will triger it own automation.

#  Entities used:
#    binary_sensor.my_work_day
#    cover.almond_plus_garagedooropener_two_car_4_1
#    light.master_bathroom
#    light.master_shower
#    light.kitchen_pedant
#    script.leaving

# ---- Sensor to know if today is workday or not. Combines calendar for vacations
binary_sensor:
  - platform: workday
    country: US
  - platform: template
    sensors:
      my_work_day:
        friendly_name: "My Work Day"
        value_template: 
          "{{ states('binary_sensor.workday_sensor') == 'on' and states('calendar.vacation_day') == 'off' }}"

# --- Automation to start the leaving for work script
automation:
  - trigger:
    - platform: state
      entity_id: light.master_bathroom
      to: 'off'
    condition:
    - condition: time
      after: '04:00'
      before: '07:30'
    - condition: state
      entity_id: binary_sensor.my_work_day
      state: 'on'
    action:
    - service: script.turn_off
      entity_id: script.leaving
    - service: script.leaving

script:
  leaving:
    sequence:
    - service: light.turn_on
      entity_id: light.kitchen_pedant
    # Allow enough time for light's state to change
    - delay: 2
    - wait_template: "{{ is_state('light.kitchen_pedant', 'off') }}"
      timeout:
        minutes: 180
      continue_on_timeout: false
    - service: cover.open
      entity_id: cover.almond_plus_garagedooropener_two_car_4_1

I guess I’m not completely following you. Are you saying you only want to trigger the automation when both of the lights go off? Just seeing one of them to go off isn’t enough?

I suppose you could change the trigger to this:

  - trigger:
    - platform: template
      value_template: >
        {{ is_state('light.master_bathroom', 'off') and
           is_state('light.master_shower', 'off') }}

This will trigger when both lights are off after at least one has been on. Is that what you’re looking for?

The reason I why I am thinking I need to watch for both lights is to keep a “leaving home” from firing when someone gets up and uses the bathroom. Again, this automation has stretched my learning.
To help picture the routine, let me explain some of the layout. From the master bed room there is the master bath. Keep walking and you go into a huge closet. In the corner of the closet is a chair, light, and table for my quiet time. So I when I wake up go into the bathroom, shut door and turn on bathroom light. This keeps from waking the wife up. Then go into closet, do quiet time, go back into bathroom, finish getting ready for work. That is when I turn on shower light. After getting ready, it times to leave. Turn off both lights and leave. That is when it would be nice to have the light on in the kitchen to see walking through the house. Turn that light off as I go out the door. That would be a good single to open the garage door.

If the trigger can be tied to both lights then it would be more certain that I am leaving for work. What I don’t know how to do is to know that one or the other light was just turned off. For now, I don’t see how to know if the state changed in the last 30 seconds. I was looking at the history to see if I can get it from there. https://www.home-assistant.io/integrations/history_stats/. There are examples of how to replace minutes, I think I need to do date/time math to get the last 30 seconds.
Not sure if any of this still makes sense.

I am posting this as a FYI and maybe more information than anyone cares. :slight_smile:
Here is a test yaml that I am using to test how certain configs work.
I just got the sensor working. I need to use it, but I will spend more time later.
Bad time. :grin:

input_boolean:
  testswitch_master_bath:
    name: Test Master Bath
    initial: off  
  testswitch_master_shower:
    name: Test Master Shower
    initial: off  
  testswitch_kitchen_pendant:
    name: Test Kitchen Pendant
    initial: off
  testswitch_leaving_result:
    name: Test Leaving Result
    initial: off

# Example configuration.yaml entry
sensor:
  - platform: history_stats
    name: Test Master Bath
    entity_id: input_boolean.testswitch_master_bath
    state: 'on'
    type: count
    start: '{{ (as_timestamp(now()) - (30)) | timestamp_custom("%Y-%m-%d %H:%M:%S%z")}}'
    end: '{{now()}}'


  
automation:
  - alias: test_leaving_for_work
    trigger:
    - platform: state
      entity_id: input_boolean.testswitch_master_shower
      to: 'false'
    condition:
    - condition: time
      after: '04:00'
      before: '10:30'
    - condition: state
      entity_id: binary_sensor.my_work_day
      state: 'off'
    - condition: state
      entity_id: input_boolean.testswitch_master_bath
      state: 'false'
    action:
    - service: script.turn_off
      entity_id: script.test_leaving_for_work
    - service: script.leavingtest_leaving_for_work
      
script:
  test_leaving_for_work:
    sequence:
    - service: input_boolean.turn_on
      entity_id: input_boolean.testswitch_kitchen_pendant
    # Allow enough time for light's state to change
    - delay: 2
    - wait_template: "{{ is_state('input_boolean.testswitch_kitchen_pendant', 'false') }}"
      timeout:
        seconds: 30
      continue_on_timeout: false
    - service: input_boolean.turn_on
      entity_id: input_boolean.testswitch_leaving_result

I think I am close now. I added the lovelace card at the bottom that I am using to test with. I need to test more combinations, but I am close.
My question now is, how much load does sensors like the way I am using them put on the system?
Is there a more efficient way?


input_boolean:
  testswitch_master_bath:
    name: Test Master Bath
    initial: off  
  testswitch_master_shower:
    name: Test Master Shower
    initial: off  
  testswitch_kitchen_pendant:
    name: Test Kitchen Pendant
    initial: off
  testswitch_leaving_result:
    name: Test Leaving Result
    initial: off

# Example configuration.yaml entry
sensor:
  - platform: history_stats
    name: Test Master Bath
    entity_id: input_boolean.testswitch_master_bath
    state: 'on'
    type: count
    start: '{{ (as_timestamp(now()) - (30)) | timestamp_custom("%Y-%m-%d %H:%M:%S%z")}}'
    end: '{{now()}}'
  - platform: history_stats
    name: Test Master Shower
    entity_id: input_boolean.testswitch_master_shower
    state: 'on'
    type: count
    start: '{{ (as_timestamp(now()) - (30)) | timestamp_custom("%Y-%m-%d %H:%M:%S%z")}}'
    end: '{{now()}}'


  
automation:
  - alias: test_leaving_for_work
    trigger:
    - platform: template
      value_template: '{{ (states.sensor.test_master_bath.state | int > 0) and 
                          (states.sensor.test_master_shower.state | int > 0) and
                          (states.input_boolean.testswitch_master_bath.state == "off") and 
                          (states.input_boolean.testswitch_master_shower.state == "off") }}'
    condition:
    - condition: time
      after: '04:00'
      before: '10:30'
    - condition: state
      entity_id: binary_sensor.my_work_day
      state: 'off'
    action:
    - service: script.turn_off
      entity_id: script.test_leaving_for_work
    - service: script.test_leaving_for_work
      
script:
  test_leaving_for_work:
    sequence:
    - service: input_boolean.turn_on
      entity_id: input_boolean.testswitch_kitchen_pendant
    # Allow enough time for light's state to change
    - delay: 2
    - wait_template: "{{ is_state('input_boolean.testswitch_kitchen_pendant', 'off') }}"
      timeout:
        seconds: 30
      continue_on_timeout: false
    - service: input_boolean.turn_on
      entity_id: input_boolean.testswitch_leaving_result
      
#Just for referance, lovelace card to test this test.
#This card is to test and understand responses
# entities:
  # - entity: input_boolean.testswitch_master_bath
  # - entity: input_boolean.testswitch_master_shower
  # - entity: input_boolean.testswitch_kitchen_pendant
  # - entity: sensor.test_master_bath
  # - entity: sensor.test_master_shower
  # - entity: binary_sensor.my_work_day
  # - entity: automation.test_leaving_for_work
  # - entity: script.test_leaving_for_work
  # - entity: input_boolean.testswitch_leaving_result
# type: entities

Why don’t you just trigger the automation when the shower light goes off, and ignore the bathroom light altogether? You already have the time and workday constraints. If you ignore the bathroom light then that would prevent it from triggering if someone happens to just go to the bathroom (during the workday morning time frame.)

I see your point, I just got so wrapped up in automating the routine, lost sight of the forest from the trees.
Still learned a lot. First time to use scripts from your first post.

1 Like