After playing quite some time with my ‘time topic’ without success, I am reaching out to the swarm intelligence of the homeassistant forum.
What I want to achieve:
Create a template binary_sensor that indicates whether it’s nighttime, I want to use it in my automations as a general guard whether the automation should run, or not.
pseudo code: begin_nighttime <= now < end_nighttime
or in a bit more realistic but still pseudo code: (begin_nighttime <= now() ) && ( now() < end_nighttime)
to avoid troubles caused by the reset of the hours at midnight we could rephrase the condition by checking for daytime and then negating it: ! ( ( now() >= end_nighttime) && (begin_nighttime > now() ) )
For easier maintenance via a dashboard card (WAF = Wife Acceptance Factor) I’ve created input_datetime helpers that only contain the time.
input_datetime.nighttime_start
input_datetime.nighttime_end
I want to combine them with now() to create the template binary_sensor.
But I am miserably failing. And would appreciate any help.
wow @Didgeridrew
thanks a lot for the very fast reply.
your solutions works if midnight is between start and end.
I wasn’t aware of today_at
but I think we need to care for the cases when midnight is not between start and end, just because people define start to be after midnight - might be the case on weekends… and people would have to sets of start/end pairs: one for workdays and one for non-workdays.
Now I am combining your hint with an if-statement.
It seems to work
{% set ts = today_at(states('input_datetime.nighttime_start_workday')) %}
{% set te = today_at(states('input_datetime.nighttime_end_workday')) %}
{% set tn = now() %}
{% if (ts > te) %} ## midnight not crossed
{% set night = not ((tn >= te) and (ts > tn)) %}
{% else %} ## midnight crossed
{% set night = ((tn < te) and (ts < tn)) %}
{% endif %}
Nighttime: {{ night }}
Schedule helper can only differentiate different days of the week, but is not aware of holidays.
I use the workday integration to have a sensor to indicate a working day, it is aware of local holidays and weekends of course.
I think this one works
{% if (states('binary_sensor.workday_sensor') == 'on') %}
{% set te = today_at(states('input_datetime.nighttime_end_workday')) %}
{% set ts = today_at(states('input_datetime.nighttime_start_workday')) %}
{% else %}
{% set te = today_at(states('input_datetime.nighttime_end_non_workday')) %}
{% set ts = today_at(states('input_datetime.nighttime_start_non_workday')) %}
{% endif %}
{% set tn = now() %}
{% if (ts > te) %} ## midnight not crossed
{% set night = not ((tn >= te) and (ts > tn)) %}
{% else %} ## midnight crossed
{% set night = ((tn < te) and (ts < tn)) %}
{% endif %}
{{ night }}
and it’s only possible because of your hint about today_at
I am often traveling and need to know the time of my phone (iOS device). I have the timezone of my phone in the (string) format “Europe/Berlin”. How do I template that into something that gives a UNIX timestamp (e.g. "now() in the time zone my phone is in)?
I have various hi/lo data collected from a local weather station via MQTT (example data below), and I would like to convert the time component to 12 hour time (such as 0:03 displayed as 12:03 am). I can display the time as received from MQTT but have had no luck converting to the preferred format.
this worked, in that now it shows ‘October 9, 2024 at 5:10 AM’ within home assistant.
is there a way to format to show only ‘5:10AM’? My end goal is to show the hi/lo temperature for the day on an e-ink display, and include the times for the hi and lo temperatures (only at 5:10am, as an example).
Week number is counted by first week thats hold 4 days.
That’s the norm in EU and adopted by countries eg. Sweden, Denmark, Germany
now().strftime(“%V”) is the right to be used for week number.
If you’re on a windows system, you need to click the all options link on that page because strftime.org only shows you what is available for the OS it detects.
My question is about durations.
one of my rest sensors returns an ISO 8601 compliant duration, e.g. PT3H25M, which means the duration is 3 hours and 25 minutes.
I would like to get this into a sensor of device_class duration.
What do I need to do? Is there an existing convenient way to convert the ISO duration into a HA compliant format?
Add (or update) your rest sensor with these fields:
value_template: "{{ (value | as_timedelta).total_seconds() }}"
availability: "{{ value | as_timedelta is not none }}"
device_class: duration
unit_of_measurement: s
It will result in a duration sensor.
You will need to wait for a bug fix in 2025.5 for this to work properly at all times. The good news is 2025.5 comes out tomorrow.
I am wondering how to make the UI, e.g. entity card or entities card showing the duration as as d HH:MM:SS. Where d is days, HH is hours, MM is minutes and SS is seconds.
of course I could create a template helper containing the right formatting but wouldn’t expect that it is needed to have a helper only for displaying an existing entity.