Help with Automation based on time in state (home/not_home)

So I have this automation, and it works, but I’ve found that my devices go offline a bit more than I realized. So I end up getting a lot of alerts. I do want to know if a device goes offline and if it goes online, but I really only want to know if they have been offline for an extended period of time (like maybe 30 minutes)

- alias: "Notify if HA Device Offline"
  trigger:
  - platform: state
    entity_id:
      - device_tracker.amazon_dot_ashleys_room
      - device_tracker.amazon_dot_bedroom
      - device_tracker.amazon_dot_living_room
      - device_tracker.amazon_dot_office
      - device_tracker.appletv
      - device_tracker.ecobee_downstairs
      - device_tracker.ecobee_upstairs
      - device_tracker.fire_tv_theater_room
      - device_tracker.front_doorbell_camera
      - device_tracker.harmony_hub_living_room
      - device_tracker.harmonyhub_frog
      - device_tracker.hue_hub
      - device_tracker.office_pc
      - device_tracker.plexserver
      - device_tracker.slingbox
      - device_tracker.sonos_playbar_living_room
      - device_tracker.sonos_sub_living_room
      - device_tracker.sony_tv_living_room
      - device_tracker.tivomini_bedroom
      - device_tracker.tivomini_theater_room
      - device_tracker.vizio_tv_bedroom_lan
      - device_tracker.vizio_tv_cast_bedroom_wireless
      - device_tracker.vizio_tv_theater_room_lan
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 10
  action:
  - service: notify.pushover
    data_template:
      target: Paul
      title: "Device Status" 
      message: "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}"

I could do something like

to: 'not_home"
for: 30 minutes

but that does not cover when it comes back online
I could do two triggers one for home and one for not home, but then I’d end up getting a lot of messages for items coming online. I think I need a condition that the state of the trigger device has not changed in 30 minutes, along with the for: 30 minutes in the trigger??

Just looking to see what others have done for stuff like this

Maybe this can help. To be honest I am not 100% sure how it works but it might give you some idea’s.
If my garage is left open for 30 minutes I get an alert, every 30 minutes. When it closes I get another alter saying that it has closed.
I use PushBullet for my notifier.

alert:
 garage_door:
   name: Garage is open
   done_message: Garage is closed
   entity_id:  cover.mqtt_cover
   state: 'open'
   repeat: 30
   can_acknowledge: true
   skip_first: true
   notifiers:
     - PB_NOTIFIER

You can just change the parameter for each device tracker to “consider_home: 1800”. This won’t set to not_home if the offline duration is smaller than 30 minutes.

see Device tracker - Home Assistant

1 Like

I need to read about the device_tracker component because I don’t understand its application for tracking Amazon Echo Dots, Philips Hue Hubs, Ecobee Thermostats, etc.

Can I set each device individually? I did not realize that, I’ll have a look. I have a few presense sensors using the same device tracker (wi-fi) that I dont want to set to 30 minutes.

They are just connected to my wif-fi for presence, and I want to know if they fall off the network, not much more than that.

1 Like

So if you are mainly concerned with being notified when they drop out, why not just include the for: x time in your trigger? You could create another automation to let you know that it has come back online where it has a condition to restrict it notifying you unless the device had been offline for x time as well. That way any short dropout wont create a notification

- alias: "Notify if HA Device Offline"
  trigger:
  - platform: state
    entity_id:
      - device_tracker.amazon_dot_ashleys_room
      - device_tracker.amazon_dot_bedroom
      - device_tracker.amazon_dot_living_room
      - device_tracker.amazon_dot_office
      - device_tracker.appletv
      - device_tracker.ecobee_downstairs
      - device_tracker.ecobee_upstairs
      - device_tracker.fire_tv_theater_room
      - device_tracker.front_doorbell_camera
      - device_tracker.harmony_hub_living_room
      - device_tracker.harmonyhub_frog
      - device_tracker.hue_hub
      - device_tracker.office_pc
      - device_tracker.plexserver
      - device_tracker.slingbox
      - device_tracker.sonos_playbar_living_room
      - device_tracker.sonos_sub_living_room
      - device_tracker.sony_tv_living_room
      - device_tracker.tivomini_bedroom
      - device_tracker.tivomini_theater_room
      - device_tracker.vizio_tv_bedroom_lan
      - device_tracker.vizio_tv_cast_bedroom_wireless
      - device_tracker.vizio_tv_theater_room_lan
    to: 'not_home'
    for:
      minutes: 20
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 10
  action:
  - service: notify.pushover
    data_template:
      target: Paul
      title: "Device Status" 
      message: "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}"

- alias: "Notify if HA Device Online"
  trigger:
  - platform: state
    entity_id:
      - device_tracker.amazon_dot_ashleys_room
      - device_tracker.amazon_dot_bedroom
      - device_tracker.amazon_dot_living_room
      - device_tracker.amazon_dot_office
      - device_tracker.appletv
      - device_tracker.ecobee_downstairs
      - device_tracker.ecobee_upstairs
      - device_tracker.fire_tv_theater_room
      - device_tracker.front_doorbell_camera
      - device_tracker.harmony_hub_living_room
      - device_tracker.harmonyhub_frog
      - device_tracker.hue_hub
      - device_tracker.office_pc
      - device_tracker.plexserver
      - device_tracker.slingbox
      - device_tracker.sonos_playbar_living_room
      - device_tracker.sonos_sub_living_room
      - device_tracker.sony_tv_living_room
      - device_tracker.tivomini_bedroom
      - device_tracker.tivomini_theater_room
      - device_tracker.vizio_tv_bedroom_lan
      - device_tracker.vizio_tv_cast_bedroom_wireless
      - device_tracker.vizio_tv_theater_room_lan
    to: 'home'
  condition:
    - condition: numeric_state
      entity_id: sensor.ha_runtime_in_minutes
      above: 10
    - condition: template
      value_template: "{{ is_state('trigger.entity_id','not_home')   ###  I MAY HAVE THIS TEMPLATE A BIT WRONG, BUT YOU GET THE IDEA   ###
      for:
        minutes: 20
  action:
  - service: notify.pushover
    data_template:
      target: Paul
      title: "Device Status" 
      message: "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}"

Yes, you can set it per device_tracker entity.