The EPIC Time Conversion and Manipulation Thread!

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.

For Input datetimes that are time-only and a “night time” that crosses midnight use an or:

{{ today_at(states('input_datetime.nighttime_end')) > now() or
 today_at(states('input_datetime.nighttime_start')) <= now()  }}

wow @Didgeridrew
thanks a lot for the very fast reply.

your solutions works :slight_smile: 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 }}

Those users would likely be better off just using a Schedule Helper to create the binary sensor.

:hushed:
didn’t think about schedule helper. shame on me.

let me check how it could be edited in a lovelace card.

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

Thanks again!

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

easiest way is install the custom component GitHub - pnbruckner/ha-entity-tz: Home Assistant Entity Time Zone Sensor which does all that for you and much more

1 Like

Amazing, thanks!

1 Like

Hi all,

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.

any suggestions on how to achieve this?

“hltempin”: [
66.2,
69.1,
“5:10”,
“0:03”,
75.2,
66.2,
87,
58
],

make a timestamp device_class sensor. Your template would be {{ today_at(value_json.hltempin[2]) }} and {{ today_at(value_json.hltempin[3]) }}

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