Insert leaving and arrival time in message

Hi guys,

I do my presence detection via ubiquity where I have a simple entity called device_tracker.cellphoneX.
When someone leaves and comes back a script fires a notification “John Doe arrived”.
I would like to insert the time of departure and coming back in the message.
Like "John left 08.30 and went back now (10.20).

My idea was to check for the “last triggered” event when the device tracker changed state to “absent”.
But it seems that I cannot get that value :frowning: Any hints?

This is what I have so far:

message: >-
   John left at {{
    state_attr('device_tracker.cellphoneX,'last_triggered') and came back at now() }} 

Do I have to check for the "last change of the status from home to absent? How do I do that?

1 Like

Have you tried copying the template into the developer tools? I see an error when I do so.

John left at {{ state_attr('device_tracker.cellphoneX,'last_triggered') and came back at now() }}

You’re missing some essential quotes and brackets.

You probably need this:

message: >-
  John left at 
  {{ state_attr('device_tracker.cellphoneX','last_triggered') }}
  and came back at
  {{ now() }}

Go to Developer Tools > States, find device_tracker.cellphonex and look at the Attributes column. Do you see a last_triggered attribute?

If you don’t, consider using last_changed. It’s not an attribute but a property of the entity’s state object.

message: >-
   John left at {{ states.device_tracker.cellphonex.last_changed.timestamp() | timestamp_custom('%H.%M') }} and came back at {{ now().timestamp() | timestamp_custom('%H.%M') }}

If you are triggering on the device tracker changing to “home”, the last_changed property will be based on the change to “home”… you will need something more sophisticated if your goal is to to have a single message that states when they left and when they arrived. I would probably use helpers to store the time the device trackers turned to “not_home” because they would survive restarts, which the last_changed property does not.

1 Like

Good point.

The simplest way would be to use an automation with a State Trigger monitoring the device_tracker’s change from home to not_home. The automation can then reference trigger.from_state.last_changed (to send a notification immediately and/or to store in a Helper for later use).

First of all - thanks for the great replies. You are really experts :slight_smile:

My ideas: Indeed I should not track “last changed” as it will not be very precise (restarts etc.)
I created a new helper (type: date). I created then an automation which always stores the time, everytime the device tracker variable changes from “home” to “not_home”. So when I leave, the timestamp is set into the helper.
I triggered the automation for testing but no value showed up :frowning: I even tried it within the developers tool.
Any ideas what Iam doing wrong?

alias: Setting helper variable time in case of absence

trigger:
  - platform: state
    entity_id:
      - device_tracker.cellphoneX
    from: home
    to: not_home
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      time: "{{ now().timestamp() | timestamp_custom('%H.%M') }}"
    target:
      entity_id: input_datetime.uhrzeit_helperfriend
mode: single

How did you trigger it?

There’s only one way to properly exercise the State Trigger and that’s to make the state value of device_tracker.cellphonex change from home to not_home. You can do that by making the physical device, representing the device_tracker, leave the home zone or you can force device_tracker.cellphonex’s value to not_home using the Set State button in Developer Tools > States.

In addition, I believe the format for the time option should contain colons not periods (according to the example in the documentation).

alias: Setting helper variable time in case of absence
trigger:
  - platform: state
    entity_id: device_tracker.cellphoneX
    from: home
    to: not_home
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      time: "{{ now().strftime('%H:%M:%S') }}"
    target:
      entity_id: input_datetime.uhrzeit_helperfriend

I’m assuming that when you created input_datetime.uhrzeit_helperfriend you configured it to store time only. If it is configured to store time and date then the template must be modified.

Thank you Guys!

I actually created two helpers. Each storing the time leaving and arrival. The Message works great!

I tried to remove the Seconds but it didnt Work. I removed the %S, but it seems the Seconds still get shown. Any ideas?

Thanks again for the group effort!

That’s the default way the UI displays a time-only Input Datetime.

If you store the time value in an Input Text, the UI doesn’t consider it to be time but just a text string so it will display it exactly as you stored it.

1 Like

Thank you! I created a new variable in text format. It didnt work to write it.
So I analyzed and googled around. I tried to use a different service. But didnt work :&
I tried to use the set value input text service. What have I done wrong?

  - service: input_text.set_value
    data:
      time: "{{ now().strftime('%H:%M') }}"
    target:
      entity_id: input_text.xxxx_departure

You need to use the key value: for that service instead of time:

  - service: input_text.set_value
    data:
      value: "{{ now().strftime('%H:%M') }}"
    target:
      entity_id: input_text.xxxx_departure

From the documentation for Input Text

OK, I might seem stupid.
I Changed it and debugged all afternoon because it didnt worked.

Code:

trigger:
  - platform: state
    entity_id:
      - device_tracker.xxx
    from: home
    to: not_home
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      value: "{{ now().strftime('%H:%M') }}"
    target:
      entity_id: input_text.xxx_departure
mode: single

Error Log for the Automation:

Stopped because an error was encountered at 19. Oktober 2022 um 06:08:31 (runtime: 0.03 seconds)

must contain at least one of date, time, datetime, timestamp.

:frowning:

Do you want to use an Input Datetime or an Input Text?

This is the service call for an Input Datetime.

  - service: input_datetime.set_datetime
    data:
      time: "{{ now().strftime('%H:%M:%S') }}"
    target:
      entity_id: input_datetime.uhrzeit_helperfriend

This is the service call for an Input Text.

  - service: input_text.set_value
    data:
      value: "{{ now().strftime('%H:%M') }}"
    target:
      entity_id: input_text.xxxx_departure