Hello.
I am trying to save the date and time of my next alarm from my Android phone’s sensor.myphone_next_alarm to a date/time helper input_datetime.wake_up_time within an automation. If I try to set the date/time helper using the UI, there is no option (that I can see) to set it to the value of another entity, which is what I am trying to do. How can I do this in YAML?
Thanks
You can do this in the UI but you need to write in a template for the datetime that you want to set your input helper to. If you use an entity state trigger, you can set the action to this date & time so that you don’t have to refer to the sensor entity_id in two different locations:
{{ trigger.to_state.state | as_datetime | as_local }}
If you use some other trigger, then you’d need to call out the sensor name:
{{ states('sensor.myphone_next_alarm') | as_datetime | as_local }}
Note that in both cases I didn’t perform any error handling, with the assumption that you’d only trigger when the state of the next alarm got updated to a real value and not to unavailable or unknown. You can do that by restricting the trigger states.
The entire automation would be this:
alias: datetime automation
description: ""
triggers:
- trigger: state
entity_id:
- sensor.myphone_next_alarm
not_to:
- unavailable
- unknown
conditions: []
actions:
- action: input_datetime.set_datetime
data:
datetime: "{{ trigger.to_state.state | as_datetime | as_local }}"
target:
entity_id: input_datetime.wake_up_time
mode: single
This worked like a charm, thank you!
I had tried a version of
but I was unable to get the right syntax.
Triggering upon state change was going to be my next question, so thanks for that as well!