Automation to turn on light based on two things isn't working

Hi,

I am trying to get an automation working. I would like this sequence. 1. Garage door opens. 2. Then back door opens. 3. If this happens, then living room lights on. After it works I will add other things, like time out, and based on time of day. But for now, I just am curious why it doesn’t work. Garage is a MyQ and sensor is a Wyze.

Here is my code

- id: '1583884149495'
  alias: Welcome Home
  description: ''
  trigger:
  - entity_id: cover.garage_door
    platform: state
    to: open
  condition:
  - condition: state
    entity_id: binary_sensor.wyzesense_779034ec
    state: 'on'
  action:
  - data: {}
    entity_id: light.36017824d8f15bcba1a1
    service: light.turn_on

You can try putting your state in quotes…

 to: 'open'

But also, the way it is now, it is only checking to see if the the back door is open… when the garage door opens… so if the back door isn’t already open when the garage door opens it isn’t going to do anything.

You would probably want to have the back door opening be the trigger, and the condition be if the garage door was opened in the last X amount of time.
Here is an example of something similar… it prevents me from getting an alert that the door was opened after getting alert that someone unlocked it. (obviously if I know someone used their “code” in the door, odds are they are about to open it, so I didn’t need two notifications)

- alias: Door Alert
  trigger:
  - entity_id: binary_sensor.front_door_opened
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.side_door_opened
    from: 'off'
    platform: state
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{(as_timestamp(now()) - (as_timestamp(states.group.key_group.last_changed))>60)}}"
    - condition: time
      after: '20:00'
      before: '9:00'
  action:
  - data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} Opened"
    service: notify.mobile_app_brians_iphone

So that keeps it from going off within 60 seconds, of a “key” code unlocking the lock.
Hopefully that will get you headed in the right direction…

Brilliant. Thank you, I am going to try this shortly (just have to turn off the automation for announcing the garage door is open so I don’t wake the kids).

I’ll let you know shortly. Thanks again.

I don’t know if you have Alexas but with the Alexa media component, there is a Do not disturb switch… I put all of those in a group and made an automation to turn all announcements off for 30 minutes… comes in handy sometimes when I’m messing around while everyone is sleeping

another excellent idea. For now I just disabled the automation to announce it.

still didn’t work.

- id: '1583884149495'
  alias: Welcome Home
  description: ''
  trigger:
  - entity_id: binary_sensor.wyzesense_779034ec
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{(as_timestamp(now()) - (as_timestamp(states.cover.garage_door.last_changed))>300)}}'
  action:
  - data: {}
    entity_id: light.36017824d8f15bcba1a1
    service: light.turn_on

check this in the template editor you might need less than < for your case, so if the garage door had been opened within the last 300 seconds… with greater than it will only be true if the garage door had been opened more than 300 seconds ago.

You can trim away a few of the nested parentheses by rephrasing it like this:

{{ now().timestamp() - states.cover.garage_door_opener.last_changed.timestamp() < 300 }}
1 Like

Thanks, I will try this.

That was it! Thanks so much. Now to just add a condition if the TV is on or it’s between a certain time.

One more little tip. These two are functionally equivalent:

{{ trigger.to_state.attributes.friendly_name }}
{{ trigger.to_state.name }}

The added advantage of the second form is that if there is no friendly_name it will use the entity’s object_id.

From State Object documentation:

Field Description
state.state String representation of the current state of the entity. Example off.
state.entity_id Entity ID. Format: .<object_id>. Example: light.kitchen.
state.domain Domain of the entity. Example: light.
state.object_id Object ID of entity. Example: kitchen.
state.name Name of the entity. Based on friendly_name attribute with fall back to object ID. Example: Kitchen Ceiling.