Home Assistant Automation – The Front door is open

Digging into the code today, to document how I have our door sensors configured, in case this example is useful for anyone else. In our case, we have a set of door sensors (we use YoLink brand, but any Alexa-compatible will work) attached to various doors, including the front. These are configured thru their respective applications to connect to Alexa, and register their state. Where the magic happens is the integration into Home Assistant, though it is actually more of a Home Assistant entry integrating into Alexa.

In plain language – this automation will trigger an Alexa announcement after a door is open for 30 seconds, repeating every 15 seconds until the sensor is closed. Once closed, it will announce to confirm. If the sensor is open for less than 30 seconds, no actions will be taken.

These instructions are specific to Home Assistant and Alexa, but likely can be modified easily for other options. This process does assume you already have Alexa Media Player integration already configured (which in turn assumes HACS is installed).

  • Inside Configuration -> Helpers
    • Create two Helpers (variables), one for the door state, and one for the door state triggered condition.
    • In this example, we will use Door_Front, and Door_Front_Alerted.
    • They should both be toggle type, which will create two new entities, input_boolean.door_front and input.boolean.door_front_alerted
  • Inside Configuration -> Scenes
    • Create a new Scene for the door condition – Front Door Open. Add the entity for the front door, input_boolean.door_front
    • Create a second Scene for the door condition – Front Door Closed. Add the entity for the front door, input_boolean.door_front
    • They should both be otherwise identical, other than the name
  • Resync your Home Assistant entities, so that the scenes are available in Alexa
  • Inside Alexa’s configuration
    • Set your sensor to activate your Front Door Open Scene when the sensor is open
    • Set your sensor to activate your Front Door Closed Scene when the sensor is closed
  • Inside Configuration -> Scenes
    • Edit the Front Door Open scene, then open the physical sensor. This will set the Home Assistant state to “open”. Save the Scene, to record the state.
    • Edit the Front Door Closed scene, then close the physical sensor. This will set the Home Assistant state to “closed”. Save the Scene, to record the state
  • Inside Configuration -> Automation
    • Create a new automation, Door_Front_Open
alias: Door_Front_Open
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.door_front
    from: 'off'
    to: 'on'
    for: '00:00:30'
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.door_front_alerted
  - repeat:
      while:
        - condition: state
          entity_id: input_boolean.door_front
          state: 'on'
      sequence:
        - service: notify.alexa_media
          data:
            message: 'Warning, the front door is open'
            data:
              type: tts
            target: >-
              media_player.office_echo_dot, media_player.family_room_echo_dot,
              media_player.living_room_echo_show
        - delay: '00:00:15'
mode: restart
  • Inside Configuration -> Automation
    • Create a new automation, Door_Front_Closed
alias: Door_Front_Closed
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.door_front
    from: 'on'
    to: 'off'
condition:
  - condition: state
    entity_id: input_boolean.door_front_alerted
    state: 'on'
action:
  - service: notify.alexa_media
    data:
      message: Thank You
      data:
        type: tts
      target: >-
        media_player.office_echo_dot, media_player.family_room_echo_dot,
        media_player.living_room_echo_show
  - service: input_boolean.turn_off
    data: {}
    entity_id: input_boolean.door_front_alerted
mode: single

In both sets of code, change the target to the list of Amazon devices you want the announcement to play on, as many or as few as you wish. Adjust the timers to a time frame that fits best for your preferences and life style.

You can also configure a series of entities on the Lovelace dashboard for each of your boolean variables. As the sensor is opened and closed, you should see each of them shift to the proper values.

If you have issues, problems, or questions, let me know. I haven’t posted this level of detail before, so I could easily be unclear in a few areas.

Discover more from Halfling13 Hobbies

Subscribe now to keep reading and get access to the full archive.

Continue reading