Adding time

Hi,

I was playing with this last night and I could get it to work correctly :grimacing:

I have a timer which display the time remaining as HH:MM ie 02:43, I want to add this to the current time to give the time it will be ready.

I have tried converting the ā€˜time remainingā€™ to a time stamp and adding them together, but it didnā€™t work.
I have tried getting the now() time as HH:MM and adding this to the ā€˜time remainingā€™ time, but that didnā€™t work.

I know this should be straight forward but I cannot fathom it out :frowning:

Can anyone help?

The code below is the code for the ā€˜time remainingā€™

  {% set tempsum = (states.input_number.hot_tub_required_temp.state | int) - (states.sensor.outside_hot_tub_ds18b20_2_temperature.state) | int %}
  {%set tempsum = tempsum / (states.input_number.hot_tub_heating_rate.state | float) %}
  {% set tempsum = (tempsum * 3600)|timestamp_custom("%H:%M", False) %}

Try this:

  {% set tempsum = states('input_number.hot_tub_required_temp') | int - states('sensor.outside_hot_tub_ds18b20_2_temperature') | int %}
  {% set tempsum = tempsum / states('input_number.hot_tub_heating_rate') | float) %}
  {% set tempsum = now() + timedelta(hours = tempsum | int ) %}
  {{ tempsum }}

Iā€™m not sure if the timedelta hours takes a floating point number, try it without | int as well , if it works it will be more accurate.

Just checked and yes it does. :+1:

1 Like

Thank you for the reply but it will still not give the correct time

The first value is now(), the second is the ā€˜tempsumā€™ value before itā€™s added to the current time, the third is the value of ā€˜tempsumā€™. The expected value would be 17:55 (17:01 + 54 min)

2021-03-27 17:01:00.016121+00:00
  00:54
  2021-03-27 17:01:00.015764+00:00

What does this give as output in the template editor?

  {% set tempsum = states('input_number.hot_tub_required_temp') | int - states('sensor.outside_hot_tub_ds18b20_2_temperature') | int %}
  {% set tempsum = tempsum / states('input_number.hot_tub_heating_rate') | float) %}
  Hours: {{ tempsum }}
  {% set tempsum = now() + timedelta(hours = tempsum ) %}
  Now: {{ now() }}
  Time:  {{ tempsum }}

Itā€™s a bit of a different approach but I do something similar to set the time that the food Iā€™m cooking in my smoker is going to done.

Here is the template to set the value of an input_datetime to the correct time:

- service: input_datetime.set_datetime
  entity_id: input_datetime.smoker_food_done
  data:
    timestamp: >
      {% if states('sensor.smoker_food_temp_rate') | float > 0.0 %}
        {{ (((states('input_number.smoker_food_setpoint') | float - states('sensor.smoker_food_temperature') | float) / states('sensor.smoker_food_temp_rate') | float) * 60) + as_timestamp(now()) }}
      {% else %}
        {{ as_timestamp(now()) + 86400 }}
      {% endif %}

I use the else to give me a far off date just in case the calculated rate of change of the food temp stalls out (which happens when smoking on a smoker - the infamous ā€œstallā€) so I donā€™t get an erroneously optimistic time.

if you canā€™t get the timedelta to work then you can use this template and it should do what you want.

Hi the output of the above is

Hours: 0.9090909090909091

Now: 2021-03-28 21:13:28.951385+01:00
Time: 2021-03-28 22:08:01.678253+01:00

Thank you it works!

The code in your Solution post is identical to the code in your first post. So what exactly was the solution if the code is unchanged?