TTS on returning home

How do I get this working? I wish to have TTS but only if I’ve been away for 3 hours or more. The automation executes fine when i call it, it just does not trigger

automation:
  - alias: "Colms Home"
    trigger:
      - platform: state
        entity_id: device_tracker.colmoneplus5_localdomain
        from: 'not_home'
        to: 'home'
    condition:
      - condition: state
        entity_id: device_tracker.colmoneplus5_localdomain
        state: 'not_home'
        for:
          minutes: 180
    action:
        service: tts.google_say
        entity_id: 
         - media_player.sitting_room_display
         - media_player.kitchen_speaker
        data_template:
          message: "Yay, welcome home colm, the house is {{states('sensor.hallway_sensor_temperature')}} degrees. You have taken {{states('sensor.google_fit_steps')}} steps today covering {{states('sensor.google_fit_distance')}} kilometers and burning {{states('sensor.google_fit_calories')}} calories"

Thats not going to work, the automation will only trigger when you are home, so your condition of checking if you are currently away for at least 3 hours will always block it.

The best that comes to mind at the moment is an automation to record the timestamp when you leave and then use a template sensor to calculate how long its been since that timestamp is greater than 3 hours.

Something using a trigger.last_state.attribute.last_changed might also be possible.

Change the trigger to be when you are away for more than 3 hours and the condition to be only when your device state changes from not_home to home.

That makes perfect sense, I’ll try that

That will only trigger once - when you are away for 3 hours. And because you are away, a condition of when you change from away to home would block it from ever firing.

Automation’s record when they last fire, you could create an automation that does something (or nothing, leave it blank) which fires when you (or create a group for everyone) leaves the house. You could use a trigger for when you arrive home, and you could then use a template condition that does this:

as_timestamp(now() | int) - as_timestamp(states.automation.leave_home.attributes.last_triggered | int | default(0)) > 10800

If now minus the time you left home’s automation last ran, is greater than 3 hours (in seconds), fire.

I’ve been trying this^^ and it’s not going to work as is. My device tracker is nmap so if my wifi drops the dummy automation gets triggered. How do i add a condition to this dummy automation that it must be in this state for 3 hours?

This is what I have

  - alias: "Colms away"
    trigger:
      - platform: state
        entity_id: device_tracker.colmoneplus5_localdomain
        from: 'home'
        to: 'not_home'
    condition: []
    action: []

add

      for: '03:00:00'

Though you may have the same issue, if the device keeps dropping in and out, and its state changes to away, the 3 hour timer resets and it wont then trigger for another 3 hours.

1 Like

I have solved this by using the Android app device tracker rather than nmap which relies on wifi

Ok, this is what I now have. Is this correct?

automation:
  - alias: "Colms Home"
    trigger:
      - platform: state
        entity_id: device_tracker.oneplus_a5000_2
        from: 'not_home'
        to: 'home'
    condition: 
      condition: template
      value_template: '{{ as_timestamp(now() | int) - as_timestamp(states.automation.colms_away.attributes.last_triggered | int | default(0)) > 1 }}'
    action:
        - delay: '00:03:00'
        - service: tts.google_say
          entity_id: 
            - media_player.sitting_room_display
            - media_player.kitchen_speaker
          data_template:
            message: "Yay, welcome home column. The house is {{states('sensor.hallway_sensor_temperature')}} degrees. You have taken {{states('sensor.google_fit_steps')}} steps today covering {{states('sensor.google_fit_distance')}} kilometers and burning {{states('sensor.google_fit_calories')}} calories"

  - alias: "Colms Away"
    trigger:
      - platform: state
        entity_id: device_tracker.oneplus_a5000_2
        from: 'home'
        to: 'not_home'
        for: '03:00:00'
    action:
      service: notify.mobile_app_oneplus_a5000
      data_template:
        title: "Away from home for 3 hrs"
        message: "You have been away for 3 hrs, your automation is working"

Without going and testing it myself, I think it looks like a good starter. Does it not work?

No, it’s not working. The away automation works but not the home one

If you put that in the development template page what does it show?

Unknown error rendering template

There’s something wrong with it, you’ll need to test each part individually.

Mine work fine as,

    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.automation.sequence_morning_alarm_motion_detection.attributes.last_triggered | default(0)) | int > 60 )}}'

With the conversion to int inside the brackets it doesnt like it for what ever reason, move it to outside the brackets,

{{ as_timestamp(now()) | int - as_timestamp(states.automation.colms_away.attributes.last_triggered 

Error rendering template: TemplateSyntaxError: unexpected end of template, expected ‘,’.

Your going to have to just play with it, the one I posted definately work.

create a helper that changes like this. This works for me. The helper changes state of the input_select to “not Home”, “just arrived”, “home” and “away” and perform action on that state

######   index status of Dave present detection    #################

- alias: Mark Dave as just arrived
  initial_state: true
  trigger:
    - platform: state
      entity_id: device_tracker.life360_dave
      from: 'not_home'
      to: 'home'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.dave_status_dropdown
        option: Just Arrived

- alias: Mark Dave as home
  initial_state: true
  trigger:
    - platform: state
      entity_id: input_select.dave_status_dropdown
      to: 'Just Arrived'
      for:
        minutes: 5
    - platform: state
      entity_id: input_select.dave_status_dropdown
      from: 'Just Left'
      to: 'Just Arrived'
  condition:
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.dave_status_dropdown
        option: Home

- alias: Mark Dave as just left
  initial_state: true
  trigger:
    - platform: state
      entity_id: device_tracker.life360_dave
      from: 'home'
      to: 'not_home'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.dave_status_dropdown
        option: Just Left

- alias: Mark Dave as away
  initial_state: true
  trigger:
    - platform: state
      entity_id: input_select.dave_status_dropdown
      to: 'Just Left'
      for:
        minutes: 2
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.dave_status_dropdown
        option: Away

one automation and this works

- alias: Dave or Linda Home turn dining light on
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.ecolink_door_window_sensor_sensor
  condition:
    - condition: template
      value_template: >
        {{ is_state('binary_sensor.ecolink_door_window_sensor_sensor','on') }}
    - condition: template
      value_template: >
        {{ is_state('sun.sun','below_horizon') }}
    - condition: template
      value_template: >
        {{ is_state('input_select.dave_status_dropdown','Just Arrived') or
          is_state('input_select.linda_status_dropdown','Just Arrived') }}
  action:
    - service: light.turn_on
      entity_id: light.leviton_dz6hd_1bz_decora_600w_smart_dimmer_level
      data:
        brightness_pct: 100
    - service: switch.turn_on
      entity_id: switch.wenzhou_mtlc_electric_appliances_enerwave_zwn_rsm1_plus_smart_single_relay_switch_module_switch

this is what is in my configuration.yaml (the helper) for the input_setect


> input_select:
> 
>   dave_status_dropdown:
>     name: Dave
>     options:
>       - Home
>       - Just Arrived
>       - Just Left
>       - Away
>     initial: Home