I have this template sensor:-
- sensor:
- name: "Central Heating Calculated Setpoint"
unique_id: central_heating_calculated_setpoint
state: >
{% set normal = states('input_number.central_heating_normal_set_point') | float(0) %}
{% set morning = states('input_number.central_heating_morning_temperature_adjust') | float(0) %}
{% set evening = states('input_number.central_heating_evening_temperature_adjust') | float(0) %}
{% set morning_until = today_at(states('input_datetime.central_heating_morning_is_until')) %}
{% set evening_starts = today_at(states('input_datetime.central_heating_evening_starts_at')) %}
{% set adj = states('sensor.central_heating_weather_setpoint_adjust') | float(0) if is_state('input_boolean.central_heating_external_temperature_adjust','on') else 0 %}
{% if now() < (morning_until-timedelta(seconds=1)) %}
{{ normal + morning + adj }}
{% elif now() >= (evening_starts-timedelta(seconds=1)) %}
{{ normal + evening + adj }}
{% else %}
{{ normal + adj }}
{% endif %}
I have an automation that fires at exactly the defined morning_until and also evening_starts times to read the value from the sensor.
My original time evaluations of now() in the if statement did not include the timedeltas that you can see in the current implementation, because for some reason, the template returned incorrect values when my automation requests the value on EXACTLY the morning_ends or evening_starts time. (If called manually just a second later, the template rendered the correct values)
Is there a more elegant way of dealing with this edge case ?