home-assistant-configuration icon indicating copy to clipboard operation
home-assistant-configuration copied to clipboard

Improved room presence and alexa announcements

Open wmandra opened this issue 2 years ago • 1 comments

First, I'd just like to say how much of a help your configuration repo has been for me as I've built out automation within my system. Truly excellent work!

I've expanded a bit on your room presence methods to what currently seems like a better fit (at least for my use case), that might be helpful to yourself or others.

I started with creating a room_presence binary sensor for every single room in the house. Each of these template sensors is able to use different rules to determine state.

For example, here's the binary sensor template for my master bedroom:

room_presence_master_bedroom:
  friendly_name: "Room Presence Master Bedroom"
  device_class: occupancy
  value_template: >-
    {% if is_state('switch.master_bedroom_do_not_disturb_switch', 'on') or 
     not is_state('media_player.tv_master_bedroom', 'off') or
     is_state('switch.master_bedroom_main_lights', 'on') %}
      on
   {% else %}
      off
   {% endif %}

Rules are pretty simple, do not disturb is turned on via automation when I go to sleep and turned off via automation when I get up, otherwise it's always off, so pretty safe bet that do not disturb means I'm in bed and room is occupied. The FireTV in the bedroom is only ever on when someone is in there, so another safe bet, same with the lights.

vs the kitchen which has a motion sensor:

room_presence_kitchen:
  friendly_name: "Room Presence Kitchen"
  device_class: occupancy
  value_template: >-
    {% if is_state('binary_sensor.kitchen_occupancy', 'on') or
     is_state('binary_sensor.garage_interior_door_contact', 'on') %}
      on
    {% else %}
      off
    {% endif %}

In the kitchen on the other hand, I have a motion sensor to automate lighting and a contact sensor on door that enters garage. This motion sensor has a very short reset of only 1 minute as kitchen tends to be a transit room for my family.

Binary sensors for all other rooms have similar rule sets either based on behaviors (ie. TV on), motion, or door contact sensors (bathroom light on and door closed is occupied else clear).

From there I have an overall room_presence template sensor which prioritizes Alexa media players based on these sensors:

sensor:
  - platform: template
    sensors:
      room_presence:
        friendly_name: "Room Presence"
        value_template: >-
          {% if is_state('binary_sensor.room_presence_kitchen', 'on') %}
            kitchen
          {% elif is_state('binary_sensor.room_presence_bathroom', 'on') %}
            bathroom
          {% elif is_state('binary_sensor.room_presence_east_bedroom', 'on') %}
            east_bedroom
          {% elif is_state('binary_sensor.room_presence_master_bedroom', 'on') %}
            master_bedroom
          {% elif is_state('binary_sensor.room_presence_foyer', 'on') %}
            cpanel_foyer
          {% elif is_state('binary_sensor.room_presence_living_room', 'on') %}
            living_room
          {% else %}
            unknown
          {% endif %}

Here though, I went an additional step further. I have a series of helper scripts for Alexa TTS depending on desired behavior. If I just want to have Alexa deliver a message to a single room with highest priority I use an "Alexa Speech" script:

sequence:
  - service: notify.alexa_media
    data:
      message: '{{ message }}'
      target: media_player.alexa_{{ where }}
      data:
        type: '{{ mode }}'
        method: speak
mode: restart
alias: Alexa Speech
variables:
  where: ''
  message: ''
  mode: tts
icon: mdi:text-to-speech

I tend to use this only for individual personalized TTS like a good morning report to my bedroom, or a good morning announcement in the kitchen on first occupancy detection.

However, in some cases I'd like Alexa to deliver a message to all media players where room presence is detected. In that case I have an "Alexa Announce" helper script:

sequence:
  - service: notify.alexa_media
    data:
      message: '{{ message }}'
      data:
        method: speak
        type: tts
      target: >-
        {%if is_state('binary_sensor.room_presence_living_room', 'on')
        %}media_player.alexa_living_room,{% endif %} {%if
        is_state('binary_sensor.room_presence_kitchen', 'on')
        %}media_player.alexa_kitchen,{% endif %} {%if
        is_state('binary_sensor.room_presence_bathroom', 'on')
        %}media_player.alexa_bathroom,{% endif %} {%if
        is_state('binary_sensor.room_presence_master_bedroom', 'on')
        %}media_player.alexa_master_bedroom,{% endif %} {%if
        is_state('binary_sensor.room_presence_east_bedroom', 'on')
        %}media_player.alexa_east_bedroom,{% endif %}
mode: restart
alias: Alexa Announce
variables:
  message: ''
icon: mdi:text-to-speech

The announce script provides an entity list to notify.alexa_media for each room where presence is currently detected. I use this for more generalized announcements like laundry being finished.

The one thing I haven't yet incorporated is logic for last_called and last_called_timestamp, however my plan is to integrate that into either the overall sensor.room_presence and/or each individual room where an Echo is located.

wmandra avatar Jan 20 '22 05:01 wmandra

Thanks for helping make this code better! This is the place to request enhancements, ask questions and other things. You can also ask questions via twitter or https://jeffreystone.net

welcome[bot] avatar Jan 20 '22 05:01 welcome[bot]