Add together two timestamps


I came that far …

But now I am not sure how the right syntax has to look like if I want to combine “Print Time Left” + “Current Time”?

This is quite painful right now. This is probably the safest way to get this value:

{% set t = states('sensor.octoprint_print_time_left').split(':') | map('float', none) | list %}
{% set hours, minutes, seconds = t if t | length == 3 else (0, 0, 0) %}
{% set remaining = timedelta(hours, minutes, seconds) %}
{{ now() + remaining }}

You can put that directly into a template sensor and it’ll tell you the timestamp. As always, this is not the only way to do it. There’s a number of other ways too.

1 Like

Here’s yet another way to do add the remaining time to the current time (suitable for use in a Template Sensor):

{% set t = strptime(states('sensor.octoprint_print_time_left'), '%H:%M:%S', today_at('00:00:00')).time() %}
{{ now() + timedelta(hours=t.hour, minutes=t.minute, seconds=t.second) }}
2 Likes

Perfect! I also formatted the output to display HH:MM

{% set t = strptime(states('sensor.octoprint_print_time_left'), '%H:%M:%S', today_at('00:00:00')).time() %}
{{ (now() + timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)).strftime("%H:%M") }}

… and wrapped it in a custom sensor in the config:

  # Octoprint ETA
  - platform: template
    sensors:
      octopi_print_eta:
        value_template: >
          {% set t = strptime(states('sensor.octoprint_print_time_left'), '%H:%M:%S', today_at('00:00:00')).time() %}
          {{ (now() + timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)).strftime("%H:%M") }}
        friendly_name: "Octoprint ETA"
2 Likes

Thanks for this @123 (far too often when I come across a correct answer it is yours!!!), and @pixelwave for the sensor. I have moved all my sensors to templates so thought I’d share my config entry for anyone else who comes across this:

 template:
     # Octoprint ETA
   - sensor:
       name: "Octoprint ETA"
       icon: mdi:timer-sand
       state: >
           {% set t = strptime(states('sensor.octoprint_print_time_left'), '%H:%M:%S', today_at('00:00:00')).time() %}
           {{ (now() + timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)).strftime("%H:%M") }}