Automation based on Family Group

Hi all

Have been trying various methods for this but nothing is successful so am hoping someone can help point me in the right direction.

At present my automation is skipping both conditions, think it might be my group causing the issue. Really want the group to have a from and to state. E.g. group.family from not_home to home

This is my code:

alias: Welcome Home Lighting (FamilyGroup)
description: ""
triggers:
  - trigger: zone
    entity_id: person.he
    zone: zone.home
    event: enter
  - trigger: zone
    entity_id: person.she
    zone: zone.home
    event: enter
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: group.family
            state: not_home
        sequence:
          - action: automation.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: automation.shm_ambient_lighting
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - action: automation.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: automation.shm_ambient_lighting
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: group.family
                state: home
              - condition: sun
                after: sunset
              - condition: or
                conditions:
                  - condition: template
                    value_template: >-
                      {{ now() - state_attr('automation.night_time',
                      'last_triggered') < timedelta(hours=7)}}
                  - condition: template
                    value_template: >-
                      {{ now() -
                      state_attr('automation.3_shm_lights_off_when_not_home',
                      'last_triggered') < timedelta(hours=7) }}
        sequence:
          - action: script.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: script.welcome_home_late_lights
mode: single

Have also attached a trace screenshot

Many thanks.

Well in this particular case the automation will have skipped the choose actions because the one for the family coming home requires it to be after sunset - which it obviously isn’t at 11:58am

To be more helpful with debugging though - click the 3 dots in the top right of the trace screen and choose download trace.

Attach that in a code block (the same way you do to post the automation)

I’d also recommend posting a simple walkthrough of what you are trying to achieve, so we can identify if the code as posted will actually do what you expect. For example if we can better understand what you are trying to achieve with the code in the OR block. Timedelta is being used incorrectly here for example, Timedelta only generates the delta, you need to add or subtract that to a time in order to do anything useful with it.

Thanks,

Am trying to achieve this:

If no one was home and someone arrives - Turn on ambient lights
If someone was home and another person arrives - Turn on home late lights

The home late lights have some other conditions such as sunset and checking to see when 2 other automations were run.

This is my trace code:

{
  "trace": {
    "last_step": "action/0/choose/1/conditions/0/conditions/1",
    "run_id": "5ddaa32af876b7f72ad583b165231c7a",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-12-14T11:58:37.402224+00:00",
      "finish": "2024-12-14T11:58:37.403007+00:00"
    },
    "domain": "automation",
    "item_id": "1734176451480",
    "trigger": "person.charlotte entering Home",
    "trace": {
      "trigger/1": [
        {
          "path": "trigger/1",
          "timestamp": "2024-12-14T11:58:37.402258+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.welcome_home_lighting_familygroup",
              "state": "on",
              "attributes": {
                "id": "1734176451480",
                "last_triggered": "2024-12-14T11:41:30.462603+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Welcome Home Lighting (FamilyGroup)"
              },
              "last_changed": "2024-12-14T11:47:45.291838+00:00",
              "last_reported": "2024-12-14T11:47:45.291838+00:00",
              "last_updated": "2024-12-14T11:47:45.291838+00:00",
              "context": {
                "id": "01JF2FXZ0BEVQQ0FQCRVTJHHJP",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "1",
              "idx": "1",
              "alias": null,
              "platform": "zone",
              "entity_id": "person.charlotte",
              "from_state": {
                "entity_id": "person.charlotte",
                "state": "not_home",
                "attributes": {
                  "editable": true,
                  "id": "charlotte",
                  "device_trackers": [
                    "device_tracker.charlotte_s_iphone",
                    "device_tracker.charlotte_s_iphone_2"
                  ],
                  "latitude": 51.03203498279328,
                  "longitude": 1.0027229385962284,
                  "gps_accuracy": 2,
                  "source": "device_tracker.charlotte_s_iphone_2",
                  "user_id": "fb5088572f3d4cb3969a5829f4d97d35",
                  "entity_picture": "/api/image/serve/5283d0f865c59ae902516549f510e47e/512x512",
                  "friendly_name": "Charlotte"
                },
                "last_changed": "2024-12-14T08:37:37.001149+00:00",
                "last_reported": "2024-12-14T11:57:47.292253+00:00",
                "last_updated": "2024-12-14T11:57:47.292253+00:00",
                "context": {
                  "id": "01JF2GGAWWBSCJ6K279CPVGCSY",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "person.charlotte",
                "state": "home",
                "attributes": {
                  "editable": true,
                  "id": "charlotte",
                  "device_trackers": [
                    "device_tracker.charlotte_s_iphone",
                    "device_tracker.charlotte_s_iphone_2"
                  ],
                  "latitude": 51.035161339820384,
                  "longitude": 1.0096596680941905,
                  "gps_accuracy": 2,
                  "source": "device_tracker.charlotte_s_iphone_2",
                  "user_id": "fb5088572f3d4cb3969a5829f4d97d35",
                  "entity_picture": "/api/image/serve/5283d0f865c59ae902516549f510e47e/512x512",
                  "friendly_name": "Charlotte"
                },
                "last_changed": "2024-12-14T11:58:37.398036+00:00",
                "last_reported": "2024-12-14T11:58:37.398036+00:00",
                "last_updated": "2024-12-14T11:58:37.398036+00:00",
                "context": {
                  "id": "01JF2GHVTPE41JYHPD002DKZ8Q",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "zone": {
                "entity_id": "zone.home",
                "state": "3",
                "attributes": {
                  "latitude": 51.035796876353544,
                  "longitude": 1.009477043990046,
                  "radius": 100,
                  "passive": false,
                  "persons": [
                    "person.admin",
                    "person.alex",
                    "person.charlotte"
                  ],
                  "editable": true,
                  "icon": "mdi:home",
                  "friendly_name": "Home",
                  "friendy_name": "Home"
                },
                "last_changed": "2024-12-14T11:58:37.398234+00:00",
                "last_reported": "2024-12-14T11:58:37.398234+00:00",
                "last_updated": "2024-12-14T11:58:37.398234+00:00",
                "context": {
                  "id": "01JF2GHVTPPYNXTM02H12R31FD",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "event": "enter",
              "description": "person.charlotte entering Home"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-12-14T11:58:37.402430+00:00",
          "changed_variables": {
            "context": {
              "id": "01JF2GHVTTKAFWB3VMMEWFB5YY",
              "parent_id": "01JF2GHVTPE41JYHPD002DKZ8Q",
              "user_id": null
            }
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2024-12-14T11:58:37.402630+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2024-12-14T11:58:37.402649+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0/entity_id/0": [
        {
          "path": "action/0/choose/0/conditions/0/entity_id/0",
          "timestamp": "2024-12-14T11:58:37.402667+00:00",
          "result": {
            "result": false,
            "state": "home",
            "wanted_state": "not_home"
          }
        }
      ],
      "action/0/choose/1": [
        {
          "path": "action/0/choose/1",
          "timestamp": "2024-12-14T11:58:37.402707+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0",
          "timestamp": "2024-12-14T11:58:37.402719+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1/conditions/0/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0/conditions/0",
          "timestamp": "2024-12-14T11:58:37.402730+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/1/conditions/0/conditions/0/entity_id/0": [
        {
          "path": "action/0/choose/1/conditions/0/conditions/0/entity_id/0",
          "timestamp": "2024-12-14T11:58:37.402740+00:00",
          "result": {
            "result": true,
            "state": "home",
            "wanted_state": "home"
          }
        }
      ],
      "action/0/choose/1/conditions/0/conditions/1": [
        {
          "path": "action/0/choose/1/conditions/0/conditions/1",
          "timestamp": "2024-12-14T11:58:37.402764+00:00",
          "result": {
            "wanted_time_after": "2024-12-14T15:48:53.486475+00:00",
            "result": false
          }
        }
      ]
    },
    "config": {
      "id": "1734176451480",
      "alias": "Welcome Home Lighting (FamilyGroup)",
      "description": "",
      "triggers": [
        {
          "trigger": "zone",
          "entity_id": "person.alex",
          "zone": "zone.home",
          "event": "enter"
        },
        {
          "trigger": "zone",
          "entity_id": "person.charlotte",
          "zone": "zone.home",
          "event": "enter"
        }
      ],
      "conditions": [],
      "actions": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "state",
                  "entity_id": "group.silver_family",
                  "state": "not_home"
                }
              ],
              "sequence": [
                {
                  "action": "automation.turn_on",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "automation.shm_ambient_lighting"
                  }
                },
                {
                  "delay": {
                    "hours": 0,
                    "minutes": 0,
                    "seconds": 5,
                    "milliseconds": 0
                  }
                },
                {
                  "action": "automation.turn_on",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "automation.shm_ambient_lighting"
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "and",
                  "conditions": [
                    {
                      "condition": "state",
                      "entity_id": "group.silver_family",
                      "state": "home"
                    },
                    {
                      "condition": "sun",
                      "after": "sunset"
                    },
                    {
                      "condition": "or",
                      "conditions": [
                        {
                          "condition": "template",
                          "value_template": "{{ now() - state_attr('automation.night_time', 'last_triggered') < timedelta(hours=7)}}"
                        },
                        {
                          "condition": "template",
                          "value_template": "{{ now() - state_attr('automation.3_shm_lights_off_when_not_home', 'last_triggered') < timedelta(hours=7) }}"
                        }
                      ]
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "action": "script.turn_on",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "script.welcome_home_late_lights"
                  }
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01JF2GHVTTKAFWB3VMMEWFB5YY",
      "parent_id": "01JF2GHVTPE41JYHPD002DKZ8Q",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Welcome Home Lighting (FamilyGroup)",
      "message": "triggered by person.charlotte entering Home",
      "source": "person.charlotte entering Home",
      "entity_id": "automation.welcome_home_lighting_familygroup",
      "context_id": "01JF2GHVTTKAFWB3VMMEWFB5YY",
      "domain": "automation",
      "when": 1734177517.4022844
    }
  ]
}

Well dealing with the last automation run code first -

{{ state_attr('automation.night_time','last_triggered') + timedelta(hours=7) < now() }}

is what you want, if you are trying to determine if the automation last triggered + 7 hours is before now

Got some help previously with that template, is should check if the automation ran in the last 7 hours.

The idea is to check if all the lights have been switched off, which is what the 2 automations in the templates do (I think)

OK - so first - this is why we ask what you are actually trying to achieve.
For example:

{{ states.light|selectattr('state','eq','off')|list|count == 0 }}

Is a quick way to check if ANY lights are on.
True if all the lights are off, False otherwise

In your original automation your choose might be testing no one home, depending on the group setting for all, which might never be true when someone enters home.

But either way, your automation can be greatly simplified. zone.home has a state with a person count. So zone home going from 0 to something else is the first person (or maybe more people at once) entering. Zone home reaching 2 or higher means some one else entered beside or after person 1. Personally I’d create two automations, because both the triggers and the actions are different for the two cases.

Ok yes that simplifies it significantly and I can get rid of those other templates.

Ok thank you I wasnt aware the zone.home had a counter for how many were there.

Have been trying to condense my automations and not split them, may have to potentially with this one.

So with the zone counter the automation would be something like
Trigger: zone.home from 0 to 1
Action: ambient lights

Trigger: zone.home from 1 to 2
Action: Late night lights

Which type of trigger would you recommend:

description: ""
mode: single
triggers:
  - trigger: numeric_state
    entity_id:
      - zone.home
    above: 0
  - trigger: state
    entity_id:
      - zone.home
    from: "0"
conditions: []
actions: []

Numeric state is a threshold trigger, so it will only ever trigger once until the threshold is not met and then is met again - for example:

No one is home, (state 0) 2 people arrive home (state 2, triggered), 1 person leaves (state 1, no trigger), everyone has left (state 0), one person arrives home (state 1, triggered)

triggers:
  - trigger: numeric_state
    entity_id:
      - zone.home
    above: 1
    id: more people home
  - trigger: numeric_state
    entity_id:
      - zone.home
    above: 0
    id: someone came home
  - trigger: numeric_state
    entity_id:
      - zone.home
    below: 1
    id: no one home

That’s the way I would do it,
then you can use “triggered by” in your choose to figure out which trigger was responsible for starting the automation.

This also means that because all the triggers are on the entity zone.home then you can if you need to use {{ trigger.from_state.state|int(0) }} to determine what the previous state was before the trigger fired.

Eg:

{{ trigger.from_state.state|int(0) < 3 }}

To detect if 0 - 2 people were home before the trigger fired.

{{ ["1","2"] in trigger.from_state.state }}

To know that either 1 or 2 were previously home, BUT not 0 people.

1 Like

Hi
I am trying to write a similar automation but I don’t even know where to begin. Can I please see this automation to get some ideas? Thank you for your help

I ended up having multiple triggers and it then chose what to do as an action. This is my automation which seems to be working well for me.

alias: Welcome Home Lighting (HomeZone)
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - zone.home
    above: 1
    id: Someone Else Arrived
  - trigger: numeric_state
    entity_id:
      - zone.home
    above: 0
    id: All Away to Home
conditions: []
actions:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - Someone Else Arrived
              - condition: sun
                after: sunset
              - condition: or
                conditions:
                  - condition: template
                    value_template: >-
                      {{ now() - state_attr('automation.night_time',
                      'last_triggered') < timedelta(hours=7)}}
                  - condition: template
                    value_template: >-
                      {{ now() -
                      state_attr('automation.3_shm_lights_off_when_not_home',
                      'last_triggered') < timedelta(hours=1) }}
        sequence:
          - action: script.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: script.welcome_home_late_lights
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - Someone Else Arrived
              - condition: sun
                after: sunset
        sequence:
          - action: notify.notify
            metadata: {}
            data:
              message: Someone arrived. Ambient lights now on.
              title: Ambient Home Lights
          - action: script.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: script.whole_house_ambient_lights
            enabled: false
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
            enabled: false
          - action: script.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: script.whole_house_ambient_lights
            enabled: false
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - All Away to Home
              - condition: sun
                after: sunset
        sequence:
          - action: notify.notify
            metadata: {}
            data:
              message: Everyone arrived. Ambient lights now on.
              title: Ambient Home Lights
          - action: script.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: script.whole_house_ambient_lights
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - action: script.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: script.whole_house_ambient_lights
mode: single

Thank you. Gives me some inspiration.