Automation + script + variables to send notify for any devices presence only after n seconds

Hi all, I’m new, I recently installed HA, and I love it. Even if programming is still a bit arduous.
Here what I’m trying to accomplish. An automation for any device that checks if state didn’t revert back after a window of time.

  trigger:
  - entity_id: device_tracker.my_device, device_tracker.her_device    
    platform: state
    from: '{{ trigger.from_state.state }}'
    to: '{{ trigger.from_state.state }}'
  condition: []
  action:
  - service: script.presence_spt
    data_template:
      variables:
        state: '{{ trigger.to_state.state }}'
        device_name: '{{ trigger.to_state.attributes.name }}'

And the script:

 alias: Presence Script
 sequence:
   - delay: 00:00:15
   - service: notify.telegram
     data_template:
       message: >
         '{{ state }}'
#          {% if {{ state }} == home %}
#            "{{ device_name }}"
#          {% else %}
#           "{{ device_name }}"
#         {% endif %}

The automation as a whole works but sends both strings void. I tried to explicit to: and from: that I didn’t use at first. But still nothing. What I’m missing?

I simplified it, but still I’m unable to pass the value to the script. The telegram message with the “state” is sent void. Any suggestions?

try this:

  trigger:
  - entity_id: device_tracker.my_device, device_tracker.her_device    
    platform: state
  action:
  - service: script.presence_spt
    data_template:
      device_state: '{{ trigger.to_state.state }}'
      device_name: '{{ trigger.to_state.attributes.name }}'
 alias: Presence Script
 sequence:
   - delay: 00:00:15
   - service: notify.telegram
     data_template:
       message: "{{ device_name }} is {{ device_state }}"

I will try it this evening. Back from work.
Thanks!

Thanks, It works but returns a lot of errors into the log file. It seems like the trigger fires more than expected. So the log file is filled with these errors. Have to check what is the cause.

ERROR (Thread-17) [homeassistant.components.telegram_bot] Error sending message: Can’t parse entities: can’t find end of the entity starting at byte offset 37. Args: (42037104, ‘me is not_home, device_tracker.andy_device’), kwargs: {‘reply_markup’: None, ‘parse_mode’: ‘Markdown’, ‘disable_notification’: False, ‘disable_web_page_preview’: None, ‘timeout’: None, ‘reply_to_message_id’: None}

WARNING (MainThread) [homeassistant.components.script] Script script.presence_spt already running.

yeah, its going to fire every state change. You can reduce the triggers by using the from: or to: properties.

oh, I naively thought it had only two states. I was thinking to try to use a condition inside the script. I should check if state is home / not_home and return true as result. It should cut away the other states.

My final goal is to limit the false positive I get from Nmap tracker, so I should also check within the script if the actual state is still the same of nn seconds before. Here is the reason for the delay: property, the intention is to wait and then add a condition.

Well, home and not_home are going to be the two states but it will also update when attributes inside the state object update. Like lat and lon. So that’s why it’s updating so much.