In Home Assistant, I created a process that reads the water temperature and, based on that, sets a number of hours for the pump to run. It starts a timer with the right duration. Now I want the start time to be set so that the pump finishes 30 minutes after sunrise or no later than 7:00.
I was looking for a way of doing it and I found the Times of the Day that could be nice but I cannot make it dynamic Times of the Day - Documentation
I’ve checkd the documentation but if I create the helper and set the state as:
Generally speaking, you cannot overwrite YAML configuration from the frontend… once the TOD sensor is configured, there’s no way to make it any more dynamic that what is built in.
The description of what you are trying to do isn’t very clear to me. If you can post the current automation and describe the goal more clearly, we can probably help you reach it.
Automation trigger 30 min after sunrise → it reads the temperature
based on temperature it identifies the required duration (from 1h to 10h 30m)
today it switches on the pump and starts a second timer with the defined duration in point 2.
This means that the pump is working during th day
What I want to achieve is:
3) evaluate next sunrise
4) calculate back the correct starting point based on the defined duration in 2)
5) at the correct point in time start the pump and the timer for the correct duration.
In this way the pump will be working during night time.
In addition to that I should check that the point at 4) is calculated so that is not before sunset and after 19:00 in any case
Example:
a) duration 8 hours
sunrise 06:00
sunset 21:00
start point 22:00
b) duration 16 hours
sunrise 06:00
sunset 21:00
start point should be 14:00 but for the 19:00 rule → 19:00
c) duration 10 hours
sunrise 06:00
sunset 21:00
start point should be 20:00 but for the sunset rule → 21:00
I would use a calendar, just add an action to the automation where you are calculating the duration like:
triggers:
- trigger: sun.sunrise
options:
offset:
minutes: 30
offset_type: after
actions:
- # Whatever you are doing to calculate the time.
# You haven't shared how that value is output,
# so I'm going to assume it's a positive float indicating hours, stored in a variable named "duration"
- variables:
end: "{{ states('sensor.sun_next_rising') }}"
start: |
{% set max_start = today_at("22:00") %}
{% set t_start = end|as_datetime|as_local - timedelta(hours=duration) %}
{{ (max_start if t_start <= max_start else t_start).isoformat() }}
- action: calendar.create_event
target:
entity_id: calendar.pompa
data:
summary: "Pump On"
start_date_time: "{{ start }}"
end_date_time: "{{ end }}"
Then you have an automation that turns the pump on when the Calendar event starts and off when it ends.
I’ve got a table from some websites on how much to run the pump based on water temperature to avoid algae and issues.
I think I can use a proporitional
Okay, so the automation to set the calendar value and notification would be something like the following (I’ve guessed at the min and max values for the variables so you will probably need to play with those in the Template editor tool to tune them…)
Under what circumstances would you be forcing it to start at 19:00?
Is it something you would be doing manually?
Would it always be done at least 15 minutes before 19:00?
I’m asking because there are a few quirks with calendars in HA…
The main issue that comes to mind is that there isn’t currently an option to edit or delete a calendar event programmatically. We could alter the action that creates the calendar to include the duration in the event description… so if you decide to change it you would have the duration to use right there in the edit flow.
If surise is at 04.30 and duration is 10.30 hours the start time should be 18.00… so I must force it to be 19.00 and then terminate not at 4.30 but at 5.30
I would not do it manually, is all in the automation
start: |
{%- set sunset = states('sensor.sun_next_setting')|as_datetime %}
{%- set price_start = today_at("19:00") %}
{%- set calculated_start = end | as_datetime - timedelta(hours=duration) %}
{{ max(calculated_start, max(price_start,sunset)).isoformat() }}
That should guarantee that it’s always the later of 19:00, the calculated value, and sunset. I’ve updated the automation in the earlier post to show these changes.
2026-08-17T03:38:25.100946+00:00 Test Calendar
temperature 24.3°C
duration 378.0 min
start 2026-08-17 19:00:00+02:00
end 2026-08-18 01:18:00+02:00
DEBUG
end_s 2026-08-17 06:23:23+02:00
max_start 2026-08-17 19:00:00+02:00
t_start 2026-08-17 00:05:23+02:00
This is not correct:
As the duration is 378 minutes (6 hours and 18 min) and the sunrise time is 2026-08-17 06:23:23+02:00 the correct start time should have been 2026-08-17 00:05:23+02:00 not 19:00
If you derive end from start, then end will only be sunrise when the duration is greater than or equal to the time span between start and sunrise.
But, if you derive start from end, then end will always be sunrise and start will only be max_start when the duration is greater than or equal to the time span between max_start and sunrise.
I did all manual test and they worked correctly
BUT
in the real world it does give me the wrong info.
Look at these two lines:
2026-08-19T03:40:52.078501+00:00 Test Calendar | temperature 23.6°C | duration 336.0 min | start 2026-08-19 19:00:00+02:00 | end 2026-08-20 00:36:00+02:00 |DEBUG| sunrise 2026-08-19 06:25:52+02:00 | calculated_start 2026-08-19 00:49:52+02:00 | price_start 2026-08-19 19:00:00+02:00 | max 1 2026-08-19 19:00:00+02:00
2026-08-20T03:42:06.123596+00:00 Test Calendar | temperature 25.3°C | duration 438.0 min | start 2026-08-20 19:00:00+02:00 | end 2026-08-21 02:18:00+02:00 |DEBUG| sunrise 2026-08-20 06:27:06+02:00 | calculated_start 2026-08-19 23:09:06+02:00 | price_start 2026-08-20 19:00:00+02:00 | max 1 2026-08-20 19:00:00+02:00
The result is Wrong. The Max 1 reports the wrong time.
In both case it should have NOT selected the 19:00
But I have discovered the issue is in the SUN function
If I ask which i next_sun_raise BEFORE the sunrise has happened it reports TODAY sunrise, if I ask it after sunrise it reports TOMORROWS sunrise