Is this a reliable time template to accomplish identifying the upcoming midnight?

I want to calculate the number of hours left prior to the next upcoming midnight. Since midnight falls on the next day, if I use today_at("00:00:00") naturally I get midnight for tomorrow’s date. So in this example, today it would result in:

2023-08-24 00:00:00-07:00 which has already passed.

If I use:

{{today_at("00:00:00") + timedelta( days = 1) }}

the resulting string is actually what I’m looking for:

2023-08-25 00:00:00-07:00

but I don’t know if there’s a cleaner way to do it.

Basically, if I tap a button on the UI and I want it to disappear while a timer is running, then re-appear at Midnight, without using another boolean entity to track it, I need to log the time the button was pressed, calculate the number of seconds remaining before midnight, then set a timer to run for that length of time. When the timer goes idle, the button appears. When the timer is running, the button isn’t visible.

This is being used for daily tasks as a visual indicator that something has or hasn’t been done yet today that’s regular and repeats every day, but rather than running a static timer set to 24 hours, the daily tasks, which may be completed at any time during the day, would re-appear at midnight, and it’s a recurring task that doesn’t need to be duplicated like task management programs do as if it’s not completed that day, the daily cycle still resets.

It seems rather Rube-Goldberg to go to all the trouble of making timers and setting times until midnight when you could just reset a bunch of boolean helpers at midnight with a time triggered automation.

Is there any reason you don’t want to use the boolean helpers?

1 Like

I could, and may, but it’s one less entity created for each task on the UI.

For each task I have:

input_button to track when it’s pressed
input_select to define the preferred frequency (daily, monthly, etc.)
sensor to store values to help calculate remaining time until preferred frequency repeats
timer to run the countdown

I could add a tap action on each task button (if I use picture elements) which would assign the current value of a single input select to an attribute of the sensor so I wouldn’t need more than one input select total.

If I could create entities on the fly using variables through NodeRED I could streamline the whole thing with a single flow, but it seems that if I use NodeRED to create the entities, I’ll have a pretty large bowl of spaghetti on my hands.

You’re on the right track for determining the remaining time until the end of the current day (i.e. until the start of tomorrow).

All of these will report a datetime object representing today’s starting time.

{{ today_at("00:00:00") }}
{{ today_at("00:00") }}
{{ today_at() }}

So if you want to know the number of seconds remaining between the start of tomorrow and now, for use in a timer’s duration, you can do it the way you had proposed (add 1 day to today’s starting time):

- service: timer.start
  target:
    entity_id: timer.example
  data:
    duration: "{{ (today_at() + timedelta(days=1) - now()).total_seconds() | int(0) }}"
1 Like

That’s exactly what I did but I replaced the “now()” section with the input_button state. I guess it would probably be the same length in time either way.