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