Automation Help: Switch on if arriving home after > 24 hours away

Hi all,

Due to the shifts I work, I want to switch on a particular outlet when I arrive home, but based on a couple of conditions set out below.

I’d like to get opinions on the most elegant way to set up an automation that does the following:

  • If I’ve been away for more than 24 hours
  • And it’s between 6:00 and 11:00

Then turn on the switch.

I’ve got the time condition and the switch action sorted. I just need some suggestions about the 24 hours part.

I’ve toyed with a couple of ideas, including making a binary sensor based on last_changed of the device_tracker I’ve got set up for myself.

binary_sensor:
  - platform: template
    sensors:
      was_away_24hrs:
        value_template: '{{ as_timestamp(now()) - as_timestamp(states.device_tracker.my_s10.last_changed) | float > 86400 }}'

And using that as the trigger, then setting the time and state change to “home” as the conditions, but this seems like it might be really resource intensive?

I’ve also thought about using the state change as a trigger and the sensor as a condition, but in both approaches I’m concerned about the asynchronous nature of trying to use my device_tracker’s state change from not_home to home, while also using the not_home state in the binary_sensor’s 24 hour counter

I’d be keen to hear any ideas. Thanks.

We track away states including an ‘extended away’ of more than 24 hours, but simply for dashboard display in an input_select.

I’d probably just create a suitable input_boolean, and automate turning it on using something like:

- id: presence_status_extendedaway
  alias: "Away for 24 hours"
  trigger:
    - platform: state
      entity_id: device_tracker.my_s10
      to: "not_home"
      for:
        hours: 24
  action:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.away_24plus
`

Then reset it on return to ‘home’ status with a suitable automation.

Probably not the most elegant solution, but I find specific flags for key states much easier to debug as I expand my automations.

1 Like

You will need a input_datetime that has both date and time, add that to your configuration.yaml

input_datetime:
  YOUR_DATETIME:
    name: YOUR_DATETIME
    has_date: true
    has_time: true

Then create a new automation that triggers on your device tracker, just that nothing more.
Then the condition should be away. By some reason I have much better success with this method opposed to just using it as the trigger. Don’t ask me why.

And the action is update the input_datetime with the current datetime.


  trigger:
  - entity_id: device_tracker.YOUR_DEVICE TRACKER
    platform: state
  condition:
  - condition: state
    entity_id: device_YOUR_DEVICE TRACKER
    state: away
  action:
  - data:
      datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'  # I don't know why this is repeating but it works....
    data_template:
      datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'
    entity_id: input_datetime.YOUR_DATETIME
    service: input_datetime.set_datetime

Then in your automation condition you can use something like this too see if it’s past 24 hours.

{{ as_timestamp(now()) - as_timestamp(states.input_datetime.YOUR_DATETIME.state) > 86400 }}

This will calculate the number of seconds since last datetime change and check if it’s more than 86400 (24 hours).

1 Like

My suggestion would be to have 2 automatons.

  1. turn on automation 2 (with automation_turn.on action) if you are away for 24 hours. Use the trigger as away for 24 hours. Automation 1 can then have 2 primed for your trigger of arriving home ie
    trigger:
      - platform: state
        entity_id: person.seana
        to: 'not_home'
        for:
          hours: 24
  1. the automation you have now. With the last step in the automation to disable itself with automation.turn_off.

I use a similar approach to let me know if my kids return home, but only if they have been away for more than an hour (to handle false not_home from tracking and for going for a jog etc).

Phil

1 Like

Thanks - those are all much more workable than my ideas. I like the idea of the input boolean as then it’s also a no-brainer to be add to a dashboard if needed.

As a bonus, I’ve also learned about adding a condition to an action as a result of this. I moved the time condition from the condition block to the action block because while I always want to turn the boolean off when I return home, I only want to turn the switch on between certain hours.

For anyone interested, here’s where I am at present (testing with notifications instead of turning the switch on at present).

automations.yaml:

- id: '1589436412117'
  alias: Away for 24 Hours
  description: ''
  trigger:
  - entity_id: device_tracker.my_s10
    for:
      hours: 24
    platform: state
    to: not_home
  condition: []
  action:
  - data: {}
    entity_id: input_boolean.away_24hrs
    service: input_boolean.turn_on

- id: '1589442878882'
  alias: Home After 24 Hours
  trigger:
  - entity_id: device_tracker.my_s10
    platform: state
    to: home
  condition:
  - condition: state
    entity_id: input_boolean.away_24hrs
    state: 'true'
  action:
  - entity_id: input_boolean.away_24hrs
    service: input_boolean.turn_off
  - data:
      message: Welcome home
      title: 24 hour boolean on
    service: notify.mobile_app_sm_g975f
  - after: '6:00'
    before: '11:00'
    condition: time
  - data:
      message: Welcome home
      title: Time condition correct
    service: notify.mobile_app_sm_g975f
1 Like

The states for an input_oolean are on / off, rather than true / false

I found that out this morning when the automation didn’t fire and I had to do a bit of digging :slightly_smiling_face: