Tomblarom
(Tomblarom)
January 2, 2025, 7:09pm
1
I have a sensor, filled with an alarm time from my iphone. It’s pushed with a shortcut . Now I want to use the last alarm time, to trigger the parabolic wakeup light , BUT with a 30min offset, so it ends when the last alarm goes off.
My idea was to do it like this. Snippet from my configuration.yaml
:
# helper: Push iPhone alarm to HA
template:
- sensor:
- name: "Alarm"
icon: mdi:clock
state: "{{ states('input_boolean.alarm') }}"
- sensor:
- name: "First Alarm"
icon: mdi:alarm-snooze
state: "{{ state_attr('input_datetime.first_alarm', 'timestamp') | timestamp_custom('%H:%M', None) }}"
- sensor:
- name: "Last Alarm"
icon: mdi:alarm-snooze
state: "{{ state_attr('input_datetime.last_alarm', 'timestamp') | timestamp_custom('%H:%M', None) }}"
- sensor:
- name: "Wakeup Light - 1st try"
icon: mdi:weather-sunset-up
state: "{{ states('input_datetime.last_alarm') - (timedelta(minutes=30).strftime('%H:%M')) }}"
- sensor:
- name: "Wakeup Light - 2nd try"
icon: mdi:weather-sunset-up
state: "{{ sensor.last_alarm - (timedelta(minutes=30).strftime('%H:%M')) }}"
My inspiration came from here: Using an input_datetime sensor offset value in an automation
None of them worked… Any further ideas? I’m always getting “Not Available” in the dashboard:
Mayhem_SWE
(Magnus Helin)
January 2, 2025, 7:20pm
2
States are always strings. This obviously goes for your input_datetime helpers as well.
You cannot perform calculations on strings, you must first convert them into a datetime object or a timestamp.
Use either of the as_datetime or as_timestamp filters on the state, or use the timestamp attribute to get it directly.
(Personally I consider calculations on datetime objects to be a hassle, much easier to only work with timestamps…)
123
(Taras)
January 2, 2025, 7:49pm
3
Currently, a Time Trigger can offset a sensor’s value (if the sensor’s device_class
is timestamp
).
triggers:
- trigger: time
at: sensor.whatever
offset: '-00:30:00'
So if the requirement is to simply trigger at a time before or after a sensor’s value, it can be done by a Time Trigger (i.e. the calculation doesn’t have to be performed by a Template Sensor).
There’s a PR in the Core repo that will enhance the Time Trigger to support Input Datetime helpers with an offset value.
home-assistant:dev
← Petro31:fix-input-datetime-time-offset
opened 04:05PM - 25 Nov 24 UTC
<!--
You are amazing! Thanks for contributing to our project!
Please, DO N… OT DELETE ANY TEXT from this template! (unless instructed).
-->
## Breaking change
<!--
If your PR contains a breaking change for existing users, it is important
to tell them what breaks, how to make it work again and why we did this.
This piece of text is published with the release notes, so it helps if you
write it towards our users, not us.
Note: Remove this section if this PR is NOT a breaking change.
-->
## Proposed change
<!--
Describe the big picture of your changes here to communicate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
Time triggers with offsets are restricted to only allow sensors. We have a user attempting to do an offset with an input datetime. They have 2 automations, 1 automation that triggers off the input datetime, then a second automation that should trigger 20 minutes before the input_datetime. Right now, the user would have to create 2 input datetimes because of this restriction.
This PR removes the restriction.
relevant thread: https://community.home-assistant.io/t/trigger-using-input-datetime-with-time-offset/799710
also it's a WTH too: https://community.home-assistant.io/t/wth-offset-input-datetime-automation-trigger-by-input-number/802293
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
- This PR fixes or closes issue: fixes #
- This PR is related to issue:
- Link to documentation pull request:
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [x] The code change is tested and works locally.
- [x] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [x] I have followed the [perfect PR recommendations][perfect-pr]
- [x] The code has been formatted using Ruff (`ruff format homeassistant tests`)
- [x] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [x] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools:
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr
So you will be able to do this:
triggers:
- trigger: time
at: input_datetime.whatever
offset: '-00:30:00'
The reason to not use this Time Trigger feature (offset) is if you want to display/record the computed offset time as apart of an entity (like a Template Sensor).
Tomblarom
(Tomblarom)
January 3, 2025, 9:39am
4
Eventough I appreciate your suggestion with using the PRs functionalities, I opted for a traditional solution, as suggested by Magnus:
- sensor:
- name: "Wakeup Light Time"
icon: mdi:weather-sunset-up
state: "{{ ((state_attr('input_datetime.first_alarm', 'timestamp') | int)
- (30 * 60)) | timestamp_custom('%H:%M', None) }}"
Using Developer Tools → Templates greatly helped with troubleshooting.
petro
(Petro)
January 3, 2025, 5:01pm
5
I recommend changing that to.
{{ states('input_datetime.first_alarm') | as_datetime | as_local - timedelta(minutes=30) }}
Then you can use it as a trigger…
- trigger: time
at: sensor.wakeup_light_time
It won’t show as a nice HH:MM in the UI, however it will be really easy to use with time triggers.
Lastly, I recommend avoid using the old school timestamp_custom
functions, the template engine has evolved into using datetime objects which are easier to use with timezones.
Tomblarom
(Tomblarom)
January 3, 2025, 5:30pm
6
Trying this in the Dev Tools results in
AttributeError: 'NoneType' object has no attribute 'tzinfo
But you’re correct. With my solution, I’m not able to use it as trigger… At least it does not show up as input_datetime
:
petro
(Petro)
January 3, 2025, 5:31pm
7
Change the blueprint to accept a sensor
with device_class
timestamp
and add device_class: timestamp
to your configuration.
- sensor:
- name: "Wakeup Light Time"
icon: mdi:weather-sunset-up
device_class: timestamp
state: >
{{ states('input_datetime.first_alarm') | as_datetime | as_local - timedelta(minutes=30) }}
blueprint
...
selector:
entity:
multiple: True
filter:
- domain: input_datetime
- domain: sensor
device_class: timestamp
...
Tomblarom
(Tomblarom)
January 5, 2025, 4:04pm
8
Sorry for the delay. I was not able to look into it earlier. I set it up exactly like you suggested, but the automation is not being triggered at all by the sensor.wakeup_light_time
. For example, when I set the alarm in the iphone to 5:30am, I expect the automation to be triggered at 5:00am, so it’s finished, when the first alarm goes off. Do you have some further ideas?
petro
(Petro)
January 5, 2025, 4:07pm
9
did you alter the template like I posted or did you leave it to your output format?
Tomblarom
(Tomblarom)
January 5, 2025, 4:08pm
10
Yes, I have done that. If I hadn’t done that, the screenshot would have looked different.
petro
(Petro)
January 5, 2025, 4:09pm
11
can you post the output of the sensor in developer tools → states page?
Tomblarom
(Tomblarom)
January 5, 2025, 4:10pm
12
It says “unavailable”.
This is my current configuration.yaml:
template:
- binary_sensor:
- name: "Alarm Status"
icon: mdi:clock
state: "{{ is_state('input_boolean.alarm', 'on') }}"
- sensor:
- name: "Alarm"
icon: mdi:clock
state: "{{ states('input_boolean.alarm') }}"
- sensor:
- name: "Erster Alarm"
icon: mdi:alarm-snooze
state: "{{ state_attr('input_datetime.first_alarm', 'timestamp') | timestamp_custom('%H:%M', None) }}"
- sensor:
- name: "Letzter Alarm"
icon: mdi:alarm-snooze
state: "{{ state_attr('input_datetime.last_alarm', 'timestamp') | timestamp_custom('%H:%M', None) }}"
- sensor:
- name: "Wakeup Light Time"
icon: mdi:weather-sunset-up
device_class: timestamp
state: >
{{ states('input_datetime.first_alarm') | as_datetime | as_local - timedelta(minutes=30) }}
This is the parabolic_alarm.yaml:
blueprint:
name: Parabolic Alarm Automation
description: Turn a light on based on detected motion
domain: automation
input:
alarm_start_time:
name: Start Time
description: Datetime helper for alarm to start. Use time only and Workday sensor
to determine what days to run.
selector:
entity:
multiple: True
filter:
- domain: input_datetime
- domain: sensor
device_class: timestamp
workday_sensor:
name: Workday Sensor
description: Binary Sensor for determining whether it should run. Typically
from Workday Integration
selector:
entity:
....
petro
(Petro)
January 5, 2025, 4:11pm
13
You don’t have unique_id’s are you sure you’re looking at the correct entity? It being unavailable is why it’s not working.
Is it a time only
input_datetime?
Tomblarom
(Tomblarom)
January 5, 2025, 4:14pm
14
I’m totally new to HA, but fast-learning. I just did it like described here and manually created helpers:
Yes it is a time-only field. No date.
petro
(Petro)
January 5, 2025, 4:18pm
15
ok, then the template is
{{ today_at(states('input_datetime.first_alarm')) - timedelta(minutes=30) }}
Tomblarom
(Tomblarom)
January 5, 2025, 4:20pm
16
working! Just the wrong format. %H:%M
is enough…
petro
(Petro)
January 5, 2025, 4:20pm
17
It won’t be that format, HA needs the state format in order to use it as a trigger.
If you look at it in the UI (not states page), it will say “xx ago” or “in xx minutes”
1 Like
Tomblarom
(Tomblarom)
January 5, 2025, 4:23pm
18
Do you happen to answer me one more question?
I want to hide the marked ones or gray them out, when alarm is off:
petro
(Petro)
January 5, 2025, 4:24pm
19
That will likely require an additional binary_sensor that compares the time to current time (now()) and then use that as a visibility condition on the card.