Help with automation... just run once per person

Hey there.

I got this automation that I need to run only once per person.
The automation checks if someone arrives (“anlände”) and the state of this is set to 10minutes and then via sonos welcomes the person home.

I got two persons in the automation so I want it to run if someone else arrives.

Using Phils presence detection: https://philhawthorne.com/making-home-assistants-presence-detection-not-so-binary/

Plz help.

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: 'Anlände'
          - condition: state
            entity_id: sensor.sara_status
            state: 'Anlände'
    action:
      - service: script.welcome_home
        data_template:
          speech_message: >
            {% if is_state('sensor.niclas_status', 'Anlände') and is_state('sensor.sara_status', 'Anlände') %}
              {{ [
              "Välkommen hem Sara och Niclas" ,
              "Kungen och Drottningen är hemma"
              ] | random }}
            {% elif is_state('sensor.niclas_status', 'Anlände') %}
              {{ [
              "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.45
          message: "{{ speech_message }}"
      
  sonos_playing:
    alias: "Sonos Playing"
    sequence:
      - service: sonos.snapshot
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: 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_translate_say
        data_template:
          entity_id: "{{ sonos_entity }}"
          message: "{{ message }}"
      - delay: '00:00:03'
      - service: 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_translate_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.25
      - service: media_player.select_source
        data_template:
          entity_id: media_player.kok
          source: >
            {% if is_state('sensor.niclas_status', 'Anlände') and is_state('sensor.sara_status', 'Anlände') %}
              STAR FM 96.3 (60-tal)
            {% elif is_state('sensor.niclas_status', 'Anlände') %}
              Raubtier
            {% elif is_state('sensor.sara_status', 'Anlände') %}
              NRJ Sverige 105.1 (Populärmusik)
            {% else %}
            {% endif %}

Not sure what is wrong.
This automation doesn’t work or something else?
What do you want to achieve?

Maybe I was unclear about the automation.

I got the Phils presence dectection for every member in the household. So it check when ever someone arrives/leaves home.

This automation checks if the front door is opened (sensor.ytterdorr_status) within a certain time and if Me or the wife (sensor.niclas_status or sensor.sara_status) is set to arrived (Anlände) then Sonos plays a message to welcome us home and puts on some music.

The problem with this is: the status arrived is set to be arrived for 10minutes, so if I open and close the door several times the welcome home message will be played as many time as i open and close the door within theese 10minutes (guests, kids etc).

I want within this automation for example set sensor.niclas_status to Home if I have the state Arrived after the automation has played the welcome home message for the first time.

See this:

That prevents the automation to fire if my wife gets home within 10minutes of me.

Cant I just set either sensor.niclas_status or sensor.sara_status within the automation if?
For example: Me or my wife comes home seperatly and the automation runs and sensor.XXX_status sets to Home

For example: Both me and my wife comes home and the automation runs and the sensors sets to Home

I believe the problem is your current trigger.
Instead of the door being the trigger, it should be the persons arriving home. Then do a wait until the door is opened to play your welcome message. It will only trigger again, once a person left and came back.

AlmostSerious you tell me that I have only to change place between sensor.ytterdorr_status and me and my wife?

Nope, that didnt work. Realized that both triggers must be fulfilled to trigger the automation. So explain more plz…

I didn’t see that you are using Phils presence detection.
Originally i was thinking of just doing the automation with multiple triggers. Anyway, you using Phils Presence Detection makes things much easier.
Just add to your script the service call to set your input_select of the person that arrived to home.

But how to do that with two persons that arrives at the same time?

The same way you are currently doing it in your script. Don’t change around the trigger and the condition. Simply check which of the persons have arrived, and then set them manually to home.
i.e.:
If niclas_status = Anlände then change this relating input_select to Home
If niclas_status & sara_status = Anlände, then change both relating input_selects to Home

Thanks.

Will try!

Tried but got stuck. Dont know how to change input_select on two at the same time…

Got this far…

      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if is_state('sensor.niclas_status', 'Anlände') and is_state('sensor.sara_status', 'Anlände') %}
              ---WHAT TO WRITE HERE OR COMPLETELY WRONG???---
            {% elif is_state('sensor.niclas_status', 'Anlände') %}
              input_select.niclas_status_dropdown
            {% else %}
              input_select.sara_status_dropdown
            {% endif %}
          option: Hemma

hm, not really sure if this is possible to call 2 at the same time with this format. I’m usually doing all my automations in NodeRed so nowadays i’m a bit out of practice with this .yaml format. Probably somebody else can be of much more help. Anyways, maybe just duplicate the service call like this:

      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if is_state('sensor.niclas_status', 'Anlände') and is_state('sensor.sara_status', 'Anlände') %}
              input_select.niclas_status_dropdown
            {% elif is_state('sensor.niclas_status', 'Anlände') %}
              input_select.niclas_status_dropdown
            {% else %}
              input_select.sara_status_dropdown
            {% endif %}
          option: Hemma
      - service: input_select.select_option
        data_template:
          entity_id: >
            {% if is_state('sensor.niclas_status', 'Anlände') and is_state('sensor.sara_status', 'Anlände') %}
              input_select.sara_status_dropdown
            {% endif %}
          option: Hemma

Sorry for the lack of activity. Other things to deal with! :tired_face:

Someone else that can comfirm that this is the best way to do it!?