Sonos and presence detection - Working

WelcomeMessage thru Sonos with presence detection
Using packages

If frontdoor opens under certain conditions a welcomemessage will play depending on which person who comes home. If music already plays on Sonos the music will pause and welcomemessage will play and music will resume after that. If no music plays the favorite for that person will be played.

For presence detection Im using https://philhawthorne.com/making-home-assistants-presence-detection-not-so-binary/

automation:
  - alias: 'Välkommen Hem'
    trigger:
      platform: state
      entity_id: sensor.ytterdorr_status
      to: 'Öppen'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '09:00:00'
          before: '18:00:00'
        - condition: or
          conditions:
          - condition: state
            entity_id: sensor.niclas_status
            state: 'Kom nyss hem'
          - condition: state
            entity_id: sensor.sara_status
            state: 'Kom nyss hem'
    action:
      service: script.welcome_home
      data_template:
        speech_message: >
          {% if is_state('sensor.niclas_status', 'Kom nyss hem') %}
            {{ [
            "Välkommen hem Niclas" ,
            "Kungen Niclas är hemma"
            ] | random }}
          {% else %}
            {{ [
            "Välkommen hem Sara" ,
            "Drottningen är hemma. Välkommen hem Sara" ,
            "Husets sötnos Sara är hemma"
            ] | random }}
          {% endif %}
           

script:
  welcome_home:
    alias: "Välkomst meddelande"
    sequence:
      - service_template: script.sonos_{{'playing' if states.media_player.kok.state == 'playing' else 'notplaying'}}
        data_template:
          sonos_entity: media_player.kok
          volume: 0.65
          message: "{{ speech_message }}"
      
  sonos_playing:
    alias: "Sonos Playing"
    sequence:
      - service: media_player.sonos_snapshot
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: media_player.sonos_unjoin
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ sonos_entity }}"
          volume_level: "{{ volume }}"
      - delay: '00:00:05'
      - service: tts.google_say
        data_template:
          entity_id: "{{ sonos_entity }}"
          message: "{{ message }}"
      - delay: '00:00:03'
      - service: media_player.sonos_restore
        data_template:
          entity_id: "{{ sonos_entity }}"
          
  sonos_notplaying:
    alias: "Sonos Not PLaying"
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ sonos_entity }}"
          volume_level: "{{ volume }}"
      - delay: '00:00:05'
      - service: tts.google_say
        data_template:
          entity_id: "{{ sonos_entity }}"
          message: "{{ message }}"
      - delay: '00:00:03'
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.kok
          volume_level: 0.45
      - service: media_player.select_source
        data_template:
          entity_id: media_player.kok
          source: "{{ 'Raubtier' if states.sensor.niclas_status.state == 'Kom nyss hem' else 'P3 Star' }}"
1 Like

Code - Presence detection

##################################################
## Customize
##################################################

homeassistant:
  customize:
    sensor.niclas_status:
      entity_picture: /local/niclas.jpg
    sensor.sara_status:
      entity_picture: /local/sara.jpg
    sensor.hugo_status: 
      entity_picture: /local/hugo.jpg
    # group.gustav:
      # entity_picture: /local/gustav.jpg
  

##################################################
## Group
##################################################
group:
  familj:
    view: yes
    name: Familj
    icon: mdi:home-variant
    entities:
      - sensor.niclas_status
      - sensor.sara_status
      - sensor.hugo_status


  niclas:
    name: Niclas
    view: no
    entities:
      - sensor.niclas_status
      - device_tracker.niclas_iphone
      - device_tracker.niclas_iphone_owntracks
      - device_tracker.niclas_iphone_nmap

  sara:
    name: Sara
    view: no
    entities:
      - sensor.sara_status
      - device_tracker.sara_android_owntracks
      - device_tracker.sara_mobil_nmap

  hugo:
    name: Hugo
    view: no
    entities:
      - sensor.hugo_status
      - device_tracker.hugo_android_owntracks
      - device_tracker.hugo_mobil_nmap

  # gustav:
    # name: Gustav
    # entities:
      # - device_tracker.gustav_mobil_nmap
      
input_select:
  niclas_status_dropdown:
    name: Niclas
    options:
      - Hemma
      - Kom nyss hem
      - Lämnade nyss huset
      - Borta
      - Borta längre än 24H

  sara_status_dropdown:
    name: Sara
    options:
      - Hemma
      - Kom nyss hem
      - Lämnade nyss huset
      - Borta
      - Borta längre än 24H
      
  hugo_status_dropdown:
    name: Hugo
    options:
      - Hemma
      - Kom nyss hem
      - Lämnade nyss huset
      - Borta
      - Borta längre än 24H

##################################################
## Sensors
##################################################

sensor:
  - platform: template
    sensors:
      niclas_status:
        value_template: '{{ states.input_select.niclas_status_dropdown.state }}'
        friendly_name: 'Niclas'
      sara_status:
        value_template: '{{ states.input_select.sara_status_dropdown.state }}'
        friendly_name: 'Sara'
      hugo_status:
        value_template: '{{ states.input_select.hugo_status_dropdown.state }}'
        friendly_name: 'Hugo'


##################################################
## Automations
##################################################

automation:
  - alias: Sätt person 'Kom nyss hem'
    trigger:
      - platform: state
        entity_id: group.niclas
        from: 'not_home'
        to: 'home'
      - platform: state
        entity_id: group.sara
        from: 'not_home'
        to: 'home'
      - platform: state
        entity_id: group.hugo
        from: 'not_home'
        to: 'home'
    action:
      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if trigger.entity_id == 'group.niclas' %}
              input_select.niclas_status_dropdown
            {% elif trigger.entity_id == 'group.sara' %}
              input_select.sara_status_dropdown
            {% else %}
              input_select.hugo_status_dropdown
            {% endif %}
          option: >
            {% if trigger.entity_id == 'group.niclas' %}
              {% if states.input_select.niclas_status_dropdown.state == 'Lämnade nyss' %}
                Hemma
              {% else %}
                Kom nyss hem
              {% endif %}
            {% elif trigger.entity_id == 'group.sara' %}
              {% if states.input_select.sara_status_dropdown.state == 'Lämnade nyss' %}
                Hemma
              {% else %}
                Kom nyss hem
              {% endif %}
            {% else %}
              {% if states.input_select.hugo_status_dropdown.state == 'Lämnade nyss' %}
                Hemma
              {% else %}
                Kom nyss hem
              {% endif %}
            {% endif %}
  - alias: Sätt person 'Hemma'
    trigger:
      - platform: state
        entity_id: input_select.niclas_status_dropdown
        to: 'Kom nyss hem'
        for:
          minutes: 10
      - platform: state
        entity_id: input_select.sara_status_dropdown
        to: 'Kom nyss hem'
        for:
          minutes: 10
      - platform: state
        entity_id: input_select.hugo_status_dropdown
        to: 'Kom nyss hem'
        for:
          minutes: 10
      - platform: state
        entity_id: input_select.niclas_status_dropdown
        from: 'Lämnade nyss huset'
        to: 'Kom nyss hem'
      - platform: state
        entity_id: input_select.sara_status_dropdown
        from: 'Lämnade nyss huset'
        to: 'Kom nyss hem'
      - platform: state
        entity_id: input_select.hugo_status_dropdown
        from: 'Lämnade nyss huset'
        to: 'Kom nyss hem'
    action:
      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if trigger.entity_id == 'input_select.niclas_status_dropdown' %}
              input_select.niclas_status_dropdown
            {% elif trigger.entity_id == 'input_select.sara_status_dropdown' %}
              input_select.sara_status_dropdown
            {% else %}
              input_select.hugo_status_dropdown
            {% endif %}
          option: Hemma

  - alias: Sätt person 'Lämnade nyss huset'
    trigger:
      - platform: state
        entity_id: group.niclas
        from: 'home'
        to: 'not_home'
      - platform: state
        entity_id: group.sara
        from: 'home'
        to: 'not_home'
      - platform: state
        entity_id: group.hugo
        from: 'home'
        to: 'not_home'
    action:
      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if trigger.entity_id == 'group.niclas' %}
              input_select.niclas_status_dropdown
            {% elif trigger.entity_id == 'group.sara' %}
              input_select.sara_status_dropdown
            {% else %}
              input_select.hugo_status_dropdown
            {% endif %}
          option: Lämnade nyss huset

  - alias: Sätt person 'Borta'
    trigger:
      - platform: state
        entity_id: input_select.niclas_status_dropdown
        to: 'Lämnade nyss huset'
        for:
          minutes: 10
      - platform: state
        entity_id: input_select.sara_status_dropdown
        to: 'Lämnade nyss huset'
        for:
          minutes: 10
      - platform: state
        entity_id: input_select.hugo_status_dropdown
        to: 'Lämnade nyss huset'
        for:
          minutes: 10
    action:
      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if trigger.entity_id == 'input_select.niclas_status_dropdown' %}
              input_select.niclas_status_dropdown
            {% elif trigger.entity_id == 'input_select.sara_status_dropdown' %}
              input_select.sara_status_dropdown
            {% else %}
              input_select.hugo_status_dropdown
            {% endif %}
          option: Borta

  - alias: Sätt person 'Borta längre än 24H'
    trigger:
      - platform: state
        entity_id: input_select.niclas_status_dropdown
        to: 'Borta'
        for:
          hours: 24
      - platform: state
        entity_id: input_select.sara_status_dropdown
        to: 'Borta'
        for:
          hours: 24
      - platform: state
        entity_id: input_select.hugo_status_dropdown
        to: 'Borta'
        for:
          hours: 24
    action:
      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if trigger.entity_id == 'input_select.niclas_status_dropdown' %}
              input_select.niclas_status_dropdown
            {% elif trigger.entity_id == 'input_select.sara_status_dropdown' %}
              input_select.sara_status_dropdown
            {% else %}
              input_select.hugo_status_dropdown
            {% endif %}
          option: Borta längre än 24H