Template sensor to return value of attribute

I have a washing machine with an attribute remain_time showing the time remaining in a cycle. The attribute value format is HH:MM:SS.

I’m trying to create a template sensor to return the value of that attribute but I’m failing.

I’ve tried the below but it just returns 0. Can anyone give me any help on this please? I also only want the result of the template to return the time in minutes.

- platform: template
  sensors:
    wash_time_left:
      friendly_name: "Wash Time Remaining"
      device_class: duration
      unit_of_measurement: 'min'
      value_template: "{{ state_attr('sensor.washer_dryer','remain_time')|int(0)}}"

Remove |int(0).
That is a string value and when you cast it to int it fails and returns your specified default 0

1 Like

Thanks, that’s resolved that part of the issue as I just entered your suggestion into the template editor and it does return the actual time in the format H:MM:SS.

{{ state_attr('sensor.washer_dryer','remain_time')}}

How would I then convert the result into minutes? For example, if my the actual remaining time is 1:22:00 I would want the result as 82 minutes?

Sorry didn’t see that.

Assuming it’s not running for days:

{% set dt = strptime(state_attr('sensor.washer_dryer','remain_time'), "%H:%M:%S") %}

{{ dt.hour *60 + dt.minute }}

Brilliant, thanks for your help @Hellis81

1 Like

as_timedelta was specifically built for this.

{{ (state_attr('sensor.washer_dryer','remain_time') | as_timedelta).total_seconds() // 60 }}

same with today_at

{% set dt = today_at(state_attr('sensor.washer_dryer','remain_time')) %}

{{ dt.hour *60 + dt.minute }}

@c0ntax keep in mind, if your remaining time is longer than 24 hours, hellis81’s template and the today_at template won’t work. I doubt this will happen, but if it does, you should use the as_timedelta template.

1 Like