Get Entity State x Minutes Ago

what I’m trying to accomplish.
Trigger: I arrive home
Condition: If no one was home 5 minutes ago
Action: Turn on lights

Current code:

- id: Anybody_home
  alias: Anybody Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.whoishome
      to: 'Me'
    - platform: state
      entity_id: sensor.whoishome
      to: 'Her'
    - platform: state
      entity_id: sensor.whoishome
      to: 'Us'

  action:


    - choose:
        - alias: "IF suns down, lights on."
          conditions:
            - condition: state
              entity_id: sun.sun
              state: below_horizon
          sequence:
            - service: homeassistant.turn_on
              data:
                entity_id: group.all_lights
    - service: alarm_control_panel.alarm_disarm
      target:
        entity_id: alarm_control_panel.home
    - service: notify.notify_household
      data_template:
        message: >
          Your home security has been deactivated.

Instead of checking if no one was home five minutes ago, would it be fair to say you only want the automation’s action to be executed for the first person to arrive home and not if someone is already home? In other words, the action should only occur when the home’s state changes from unoccupied to occupied.

I assume sensor.whoishome is a Template Sensor. Please post its configuration so we can see if there’s potential for using it differently in the State Trigger.

well more specifically i’m aiming for the following:
if “Me” changes from away to home for 10 mins, play audio 1
if “Her” changes from away to home for 10 mins, play audio 2
if “Us” changes from away to home for 10 mins, play audio 3

template sensor:


sensor:
  - platform: template
    sensors:
      whoishome:
        value_template: >
         {% if is_state('person.me', 'home') and is_state('person.her', 'home') %}
           Us
         {% elif not is_state('person.me', '"home') and is_state('person.her', 'home') %}
           Her
         {% elif not is_state('person.her', '"home') and is_state('person.me', 'home') %}
           Me
         {% else %}
          Nobody
         {% endif %}

Based on what the Template Sensor does, the automation in your first post simply needs a different State Trigger:

  trigger:
    - platform: state
      entity_id: sensor.whoishome
      to: 
        - 'Me'
        - 'Her'

Now it will trigger only if either of you arrive home but no one is already home. In other words, the action is executed only for the first person who arrives home.

If you want that incorporated into the first automation then it’s an entirely new requirement and changes everything (because now it’s no longer limited to detecting only the first person to arrive). If it doesn’t need to be part of the first automation, it’s easily done in a separate automation that employs a State Trigger with a for: '00:10:00' option and the action refers to trigger.to_state.state to determine which audio track to play.

I tweaked it but logically this should still work?:

- id: Announce_Anyone
  alias: Announce Anyone
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.whoishome
      from: 'Nobody'
      for:
        minutes: 10


  action:
    - choose:
        - alias: "Who is hoome."
          conditions:
            - condition: state
              entity_id: sensor.whoishome
              state: Both
          sequence:
            - service: tts.google_translate_say
              entity_id: media_player.audiogroup
              data:
                message: Welcome home Her and Me!
            - condition: state
              entity_id: sensor.whoishome
              state: Me
          sequence:
            - service: tts.google_translate_say
              entity_id: media_player.audiogroup
              data:
                message: Welcome home Me!
            - condition: state
              entity_id: sensor.whoishome
              state: Her
          sequence:
            - service: tts.google_translate_say
              entity_id: media_player.audiogroup
              data:
                message: Welcome home Her!

But then this leaves the second dilemma of if one person is home and the other arrives, I would still like the tts audio

I can’t say for sure because it’s no longer clear to me what you are attempting to achieve.

I’ll step aside and let someone else assist you. Good luck.

I’m not sure, if I understand it correct, but i think I have an idea what you mean. But, a warning ahead, that may be that last glass of this fantastic Austrian red wine. :smiley: :smiley:

The problem is, “US” in the way you think about it is not really “defined”. But I assume you have that handled, as you’re working with a sensor state “Both”.

What you’re doing is not logically wrong, you’re just not using the choose correctly.

- id: Announce_Anyone
  alias: Announce Anyone
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.whoishome
      from: 'Nobody'
      for:
        minutes: 10


  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: sensor.whoishome
              state: Both
          sequence:
            - service: tts.google_translate_say
              entity_id: media_player.audiogroup
              data:
                message: Welcome home Her and Me!
        - conditions: # <-- a new condition needs a new "conditions" block
            - condition: state
              entity_id: sensor.whoishome
              state: Me
          sequence:
            - service: tts.google_translate_say
              entity_id: media_player.audiogroup
              data:
                message: Welcome home Me!
        - conditions: # <-- a new condition needs a new "conditions" block
            - condition: state
              entity_id: sensor.whoishome
              state: Her
          sequence:
            - service: tts.google_translate_say
              entity_id: media_player.audiogroup
              data:
                message: Welcome home Her!

You need to have choose -> first condition _and_ sequence (the IF / required) -> second condition _and_ sequence (the IF ELSE / optional) -> default (the ELSE / optional)

Yeah sorry, the ‘Us’ in the original message is the ‘Both’ in the actual program. I had changed the pronouns around in the original message to remove the names and make it easier to follow. #fail