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 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?
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.
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
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 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
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.
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.
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?
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.