To update your Home Assistant YAML code snippet to use local time instead of UTC (which as_timestamp returns), you'll need to convert timestamps to local time using the timestamp_custom and/or use as_local() where appropriate.
However, as_local() and timestamp_custom() behave slightly differently and can't be mixed directly with math operations. The best and cleanest approach is to work with datetime objects using as_local() instead of timestamps (as much as possible).
Here is your updated YAML code using local time-aware date handling:
variables:
sunrise: "{{ as_local(state_attr('sun.sun', 'next_rising')) }}"
sunset: "{{ as_local(state_attr('sun.sun', 'next_setting')) }}"
noon: "{{ as_local(state_attr('sun.sun', 'next_noon')) }}"
morning_phase_end: >
{% set sunrise = as_local(state_attr('sun.sun', 'next_rising')) %}
{% set noon = as_local(state_attr('sun.sun', 'next_noon')) %}
{{ sunrise + ((noon - sunrise) / 2) }}
afternoon_phase_start: >
{% set noon = as_local(state_attr('sun.sun', 'next_noon')) %}
{% set sunset = as_local(state_attr('sun.sun', 'next_setting')) %}
{{ noon + ((sunset - noon) / 2) }}
Explanation:
as_local(...) converts the datetime object (next_rising, next_setting, next_noon) into your local timezone.
- We no longer use
as_timestamp(...) because it's in UTC.
- The datetime math (like
(noon - sunrise) / 2) works on datetime objects, and results are still timezone-aware.
Optional Formatting:
If you want to format the output as a string (e.g., human-readable like HH:MM), add something like:
{{ (sunrise + ((noon - sunrise) / 2)).strftime('%H:%M') }}
Let me know if you need this used inside a specific automation or script!