Figure out time remaining

Good Evening,

I am building a dashboard for my utility room which has my Samsung Smarthings washing machine and tumble dryer on as an image of the actual device. What I would love to do is where the screen is on the picture is to display the remaining time of the current job however I have no idea how to convert the CompletionTime attribute to a hours and minutes remaining.

Example of the attribute: 2021-09-25T19:58:48Z

Is anyone able to help me to convert this datetime string into a time remaining (using current time as comparison).

I guess this would set this into a string helper and then have a conditional element on my picture element that if machine state = run show the remaining time?

Thanks in advance.

I don’t have a Samsung washing machine to test but, together, I think we can make this work.

Copy-paste this into the Template Editor and modify the first line to reference your washing machine.

{% set ct = state_attr('switch.your_washer', 'CompletionTime') | as_datetime %}
{{ '00:00' if now() > ct else (ct - now()).total_seconds() | timestamp_custom('%H:%M', false) }}

If the value of CompletionTime is in the future then it should indicate the number of hours and minutes remaining. If it’s in the past, it’s hard-coded to report 00:00.

If that works, it can be used to create a Template Sensor that reports the remaining time.

NOTE
It’s possible that the as_datetime filter isn’t needed if Home Assistant already understands that CompletionTime is a datetime object (as opposed to the datetime string).

1 Like

This works! Is there anyway you can convert the hours:minutes remaining into a percentage remaining? eg, 1 hour timer at 30 mins left reports 50%. at 10 minutes remaining in timer = 83%

To calculate percentage you have to know the maximum duration. You gave an example of a “1hour timer” but how can the template determine it’s 1 hour? Where can it get that value?

sorry! this is the sensor + attribute i get the time from.

entity - sensor.living_room_echo_next_timer
state - 2021-09-26T10:57:34-05:00
attribute -
prior_value: 2021-09-26T10:57:34-05:00

and this is the code you provided :slight_smile:

{% set ct = state_attr('sensor.living_room_echo_next_timer', 'prior_value') | as_datetime %}
{{ '00:00' if now() > ct else (ct - now()).total_seconds() | timestamp_custom('%H:%M', false) }}

I get the feeling that we are no longer discussing what you described in your first post, namely an entity, representing a washing machine, containing an attribute named CompletionTime'.

What is sensor.living_room_echo_next_timer and what do its state and prior_value attribute represent?

Had to modify a little as its not actually an attribute it is its own sensor but looking good thanks!

{% set ct = states('sensor.tumble_dryer_dryer_completion_time') | as_datetime %}
{{ '00:00' if now() > ct else (ct - now()).total_seconds() | timestamp_custom('%H:%M', false) }}

So next I guess I just need to create a text helper and an automation that whenever a minute changes to update the text helper?

1 Like

I can’t answer that question because you overlooked to explain why you even need an input_text. The only thing anyone knows is:

That’s been resolved so what else were you planning to do?

sorry, i am not the OP. thanks for trying to help anyways

Sorry missed that part.

I have a picture elements card and I want to show the time remaining on that.

I have managed to do it just frustrating me that I cannot get the text white now…

  - type: state-label
    entity: input_text.dryer_finished_in
    style:
      top: 4%
      left: 77%
      width: 80%
      '--primary-text-color': '#ffffff'

That’s a separate discussion from the question in your first post. Ideally, keep each topic focused on one issue. It makes it easier for others to find answers to questions. Your Picture Elements question is now buried in a thread whose topic and original question have nothing to do with it.

replace primary color with this
color: ‘#FFA500

Thank you, I figured it out literally the same time you posted!

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution post. All of this helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

I have tried to edit this to my washing machine but I just get an error:
TypeError: float() argument must be a string or a real number, not 'NoneType'

My code is:

{% set ct = state_attr('sensor.tvattmaskin_remaining_program_time', 'CompletionTime') | as_datetime %}
{{ '00:00' if now() > ct else (ct - now()).total_seconds() | timestamp_custom('%H:%M', false) }}

What am I doing wrong here?

Based on the error message, I believe the value produced by the state_attr function cannot be converted by the as_datetime filter into a datetime object.

This can happen if the entity sensor.tvattmaskin_remaining_program_time doesn’t exist, or its attribute CompletionTime doesn’t exist or it has a value that cannot be converted to a datetime object.

Copy-paste this into the Template Editor and see what is produced:

{{ state_attr('sensor.tvattmaskin_remaining_program_time', 'CompletionTime') }}

I get null for that one.
Here are the attributes for that one:

That explains why you got the error message. The attribute doesn’t exist.

Perhaps what you want is the state value of sensor.tvattmaskin_remaining_program_time?

{% set ct = states('sensor.tvattmaskin_remaining_program_time') | as_datetime %}
{{ '00:00' if now() > ct else (ct - now()).total_seconds() | timestamp_custom('%H:%M', false) }}

Thank you! That works.

Now i just need to perfect it.
I use google to read this with Text to speech, and now the response is 1and55.

How could I get this to say ”1hour and 55 minutes”?