Help templating... my Hassos will serve me nice bath water... every morning

I have a solar panel for raise my water bath temperature, this panel have an electric resistance to increase temperature when need it

So, I already have a sonoff TH16 to control it.
Temperature sensor inside water, and the same sonoff control de electric resistance

I want to define the time to bath, and the water temperature at that time
I already know the temperature raise 0,25ºC every minute when the resistance is on…

So, I’m sure me HA can do it… but I’m not so good templating HA

my first step is take the HH.MM needed to put resistance on

actual T= 58,4ºC ; objective T=65ºC

so (65-58,4)/0,25 = 26,4 »» 00:26 (26min on)

automations.yaml

- id: '1586516921111'
  alias: Update Tempo Aquecer Banho
  description: ''
  trigger:
  - minutes: /1
    platform: time_pattern
    seconds: '0'
  condition: []
  action:
  - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.tempo_aquecer_banho
      time: '{% set o = states('input_number.banho_temp') %} {% set r = states('sensor.sonoff_1000cf529b_temperature')
        %} {% set t = (o-r)/0.25 %} {{ t|timestamp_custom("%H:%M",True) }}'

Every minute HA will check and calculate for how long it need to be on.
AND THIS IS NOT WORKING, what’s wrong?

… after that the strategy is subtract this time to target HH:MM bath
and trigger on the resistance…

Any other ideas?
any help…

THANKS
GP

Try this version of the time template:

  - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.tempo_aquecer_banho
      time: >
        {% set o = states('input_number.banho_temp') | float %}
        {% set r = states('sensor.sonoff_1000cf529b_temperature') | float %}
        {% set t = (o-r)/0.25 %}
        {{ (t if t > 0 else 0) | timestamp_custom("%H:%M:%S", False) }}

Screenshot from 2020-10-07 17-56-27

I think you need to multiply the value by 60 (to get minutes) because the result, a timestamp, is in seconds.


EDIT

Enhanced last line of the template to compensate for negative values.

Thank you 123 Taras
with your code it works great.
i only did a IF beaucause of the negative value… to force Zero when the diference result in negative value…

thank you again

If there’s a possibility of the calculation producing a negative value, change the template’s last line to this:

        {{ (t if t > 0 else 0) | timestamp_custom("%H:%M:%S", False) }}

I have updated my previous post with this enhancement.