Predictive heating/cooling template construction?

I’m trying to write a template that compares the time to achieve a setpoint based on the current time and current temperature and a known achievable deltaT

This is what i have but i think it has trouble with the format of the time. I have tested the theory in a spreadsheet and need help writing the template correctly

{{states(‘sensor.ice_bath_esp_ice_bath_temperature’)|float - states(‘input_number.ice_bath_setpoint’)|float > states(‘sensor.time’)|float - states(‘input_datetime.ice_bath_target_time’)* states(‘input_number.ice_bath_deltat’) }}

1 Like

This part is probably not valid. If you are using the basic sensor.time, it’s state will be a time string like “10:00” which will produce an error when the float() filter is applied.

Is the input datetime time-only?

Keep in mind that all states are strings, so you need to apply an int() or float() filter to the state of the input number as well; or you will get a type error.

changed to this:
{{states(‘sensor.ice_bath_esp_ice_bath_temperature’)|float - states(‘input_number.ice_bath_setpoint’)|float > states(‘sensor.time’) - states(‘input_datetime.ice_bath_target_time’)* states(‘input_number.ice_bath_deltat’)}}

and receive this error now:

TypeError: can’t multiply sequence by non-int of type ‘str’

so i guess i need to convert the times and/or input_number to float? (integer wouldn’t work for the deltaT)

We can’t help if you don’t answer questions…

Also, what unit of time should be returned from the subtraction of the two time elements?

oh sorry!
Er yes, just time.

Ultimately what i’m trying to get out is a boolean on whether the time to achieve the target temperature is less than the time available, so i can delay running a chiller until it needs to turn on.

I guess it’s reading as (difference in temperature) > (achievable temperature in available time)

You’re going to need to use something like:

{% set temp_diff = states('sensor.ice_bath_esp_ice_bath_temperature')|float(0) - states('input_number.ice_bath_setpoint')|float(0) %}
{% set cooling_time = (today_at(states('input_datetime.ice_bath_target_time')) - now()).total_seconds() %}
{{ temp_diff > cooling_time * states('input_number.ice_bath_deltat') | float(0) }}

But you will need to divide cooling_time by the appropriate value to convert the seconds into whatever unit is used in your delta_t.

Also, be aware that this basic version of time math will not work across midnight because of the use of the today_at() function. If you need it to work across midnight you’ll need to make some changes to the target time.

Many thanks!!
It returns a boolean result.

The deltaT is in degrees with a resolution of 0.1 (it’s currently set at 1.8 degrees/hour)
I don’t need it to run over midnight for the next 6 months at least, but i suspect as outdoor temperatures start to rise next spring and definitely summer, it will.

What changes do i need to make to cross the midnight threshold? I realise if i want this to be active all the time i will need it to work over midnight. Otherwise i’ve got to only enable my automation after 00:00

{% set temp_diff = states('sensor.ice_bath_esp_ice_bath_temperature')|float(0) 
- states('input_number.ice_bath_setpoint')|float(0) %}
{% set today_dt = today_at(states('input_datetime.ice_bath_target_time')) %}
{% set target_dt = today_dt if today_dt > now() else today_dt + timedelta(days=1) %}
{% set cooling_time = (target_dt - now()).total_seconds() / 3600 %}
{{ temp_diff > cooling_time * states('input_number.ice_bath_deltat') | float(0) }}

EDIT: Fixed variable ID typo

Thank you,

Unfortunately that made it go unavailable. I’m reading and rereading it and nothing jumps out at me, but i’m a novice.

There was a typo in one of the variables, I have corrected it above.

yep, fine now thank you!