Time now + x hours:minutes

Hi everybody,

I already read this, but don’t currently understand how to modify it to fit my needs.

I have a template sensor to convert remaining ETA from octoprint sensor in seconds to hours:minutes. This is the sensor

sensor:
  - platform: template
    sensors:
      mk3s_minutes:
        friendly_name: "Prusa MK3S Time Remaining Time"
        unit_of_measurement: "Restzeit"
        value_template: "{{ states('sensor.prusa_mk3s_time_remaining') | float | timestamp_custom('%R', false) }}"

The current value of this sensor is 07:33 Restzeit, and the sensor will update accordingly (meaning it works as expected).

Question

How can I add this to something like {{ now().hour}}:{{ now().minute }} to display not in how much time the print will be done, but when? I believe I’d know the solution if this was decimal math, but this is hours and minutes, so I have no clue how to add this.

Can you please help me out? This should currently display something like 22:33 (it’s about 3pm and about 7h33m are left).

Thank you for your help :slight_smile:

No, go back to finity’s EPIC time manipulation thread.
You want timestamp of now() (which will return the current second since 1970/01/01 00:00:00) then add to it the number of seconds in the future you need

1 Like

Try it in the template tool.
I believe something like:

{{ as_timestamp(states.sensor.prusa_mk3s_time_remaining.state) + as_timestamp(states.sensor.time.state) | timestamp_format("%H:%M) }}

But this is written from my phone so there could be an error.
And of course there is probably a better solution but this is what I can do…

Edit:
as_timestamp(states.sensor.time.state) can also be now().timestamp()

1 Like

Thank you. I tried {{ as_timestamp(states.sensor.prusa_mk3s_time_remaining.state) + now().timestamp() | timestamp_format("%H:%M") }} and {{ as_timestamp(states.sensor.prusa_mk3s_time_remaining.state) + as_timestamp(states.sensor.time.state) | timestamp_format("%H:%M") }}, but both return Error rendering template: TemplateAssertionError: no filter named 'timestamp_format'.

When I directly copy and paste your solution, I get Error rendering template: TemplateSyntaxError: unexpected char '"' at 102, but I assume this is because of a missing " after "%H:%M…?

Sorry timestamp_custom not timestamp_format.
And yes sorry about the missing ".

As I said written on the phone

Edit oh!
Forgot … Timestamp_custom("%H:%M", false)…

1 Like

Thank you. This {{ as_timestamp(states.sensor.prusa_mk3s_time_remaining.state) + now().timestamp() | timestamp_custom("%H:%M") }} will return something like (my output is in German) unknown error while rendering template (Unbekannter Fehler beim Rendern der Vorlage).

@Mutt I got this {{ states('sensor.prusa_mk3s_time_remaining') | float + now().timestamp() }} working; however, no matter what I pipe it to (for example | timestamp_custom('%R', false') or | timestamp_custom("%H:%M) , I will not get any valid output. Do you know what I need to pipe it to?

I believe you need to add ,false

But what is the output in the template tool? Is it a very large integer?

1 Like

sensor.prusa_mk3s_time_remaining is currently 23690. I have this {{ states('sensor.prusa_mk3s_time_remaining') | int + now().timestamp()}}, which will output 1589747254.681977.

No matter if I pipe it to timestamp_custom("%H:%M", false) or "%R", false, or (without actually knowing what it does, took it from the other thread) | timestamp_custom('%I:%M %p', false) , I get a render error.

Almost there:

{{ 1589747254.681977 | float | timestamp_custom("%R", false) }} does work (will display 20:27). This is equal to the output from before, but if I pipe that to the timestamp_custom command, it will throw an error.

{{ states('sensor.prusa_mk3s_time_remaining') | float + now().timestamp() | int timestamp_custom("%R", false) }}

If I understand correctly then it’s probably

{{ (states('sensor.prusa_mk3s_time_remaining') | int + now().timestamp() | int ) timestamp_custom... }}

I believe it’s because we need to int cast both then sum them in the () to make sure that is done before the timestamp custom.

I don’t understand why there is floats, timestamps should be integers if I’m not mistaking

1 Like

Thanks! I believe this is it {{ (states('sensor.prusa_mk3s_time_remaining') | float + now().timestamp() | int) | timestamp_custom("%R", false) }}.

You don’t really need to int a time stamp (but it doesn’t hurt and you’ll just lose the fraction of the second)

:+1:

1 Like

Nice!

I’m sure someone else knows but I’m guessing:
I believe this time will re-evaluate every second or something.
Maybe if you make this a input.datetime and have an automation set it every 10 minutes or so will be a little lighter on the system.

But that is purely a guess. But since it uses now() it probably evaluates quite often.

1 Like

Ahh! So that is what it is. I was thinking that too.
But yeah not needed in this case unless you really really eager to get that print :slight_smile:

1 Like

Thank you. I will try implementing this tomorrow. If I understand correctly, octoprint will not submit the remaining seconds every second, but it’s still a good idea to query this only so often instead of all the time :slight_smile:

1 Like

I actually don’t think that will matter.
Since it uses now() the finish time will update every second anyways.
Lets assume the finish time is 11:20:55 then the next second HA will evaluate with a new now() time and output 11:20:56 and so on.
Then after some time the printer will give it a new time which will (if everything is perfect) roll the time back to 11:20:55 again.

Assumix x is the timestamp
If you use x//60*60 it will truncate it to the nearest (round down) minute
Then you could just set the update to 1/minute by specifying entity id: sensor.time
(though I think I’d add 30 secs before the truncation for a correct round)
now() (except in some VERY rare circumstances) will NOT update a template.

What’s 59 seconds between friends ?