Time string to relative time

Hi. I have a sensor that outputs this in attributes:

'0':
  destination_name: Wimbledon
  scheduled: '06:47'
  estimated: '06:47'
  time_to_station: 1m 8s
  platform: Westbound - Platform 3
'1':
  destination_name: West Croydon
  scheduled: '06:50'
  estimated: '06:50'
  time_to_station: 3m 28s
  platform: Westbound - Platform 3
'2':
  destination_name: Wimbledon
  scheduled: '06:50'
  estimated: '06:50'
  time_to_station: 4m 12s
  platform: Westbound - Platform 3
LastUpdate: '2022-02-03T06:40:33.036074'
remaining: '0:1:8'
scheduled: '06:47'
estimated: '06:47'
destination_name: Wimbledon
platform: Westbound - Platform 3
station_name: East Croydon
data:
  - title_default: To $title
    line1_default: at $time
    line2_default: $studio
    line3_default: ''
    line4_default: ''
    icon: mdi:train
  - title: Wimbledon
    airdate: '2022-02-03T06:47:41Z'
    fanart: >-
      https://tfl.gov.uk/tfl/common/images/logos/London%20Tramlink/Roundel/TramlinkRoundel.jpg
    flag: true
    studio: Westbound - Platform 3
  - title: West Croydon
    airdate: '2022-02-03T06:50:01Z'
    fanart: >-
      https://tfl.gov.uk/tfl/common/images/logos/London%20Tramlink/Roundel/TramlinkRoundel.jpg
    flag: true
    studio: Westbound - Platform 3
  - title: Wimbledon
    airdate: '2022-02-03T06:50:45Z'
    fanart: >-
      https://tfl.gov.uk/tfl/common/images/logos/London%20Tramlink/Roundel/TramlinkRoundel.jpg
    flag: true
    studio: Westbound - Platform 3
icon: mdi:train
friendly_name: Wimbledon

I like to take the 3 values from the estimated values and create a text notification that says something along the lines of “the next train is in x minutes, then the following trains are in y and z minutes.”

I know how to do the notification but I can’t find a way to get this value converted to minutes until. By playing around I did see I could do this to get the string with the time but I am stuck after this.

{{states.my_train_sensor.attributes['0'].estimated}}, {{states.my_train_sensor.attributes['1'].estimated}}, {{states.my_train_sensor.attributes['2'].estimated}}

you can use {{ today_at( <estimated time> ) - now() }}
But it needs some formatting.
I’m not sure how to do that but it’s a start I guess.

This seems to work but I can’t get the formatting righ!

Neither can I.
I spent a while trying to fix it but I couldn’t do it.

I figured it out now…

{{ ((as_timestamp(today_at("11:47")) - now().timestamp()) / 60) | int }}

Just change the 11:47 with the estimated time.

1 Like

Thank you!! This works

Hellis81’s answer is perfectly fine. I just wanted to provide an alternative for general understanding:

{{ (((today_at("11:47")) - now()).total_seconds() / 60) | int }}

Subtracting datetimes results in a timedelta. Also see datetime — Basic date and time types — Python documentation.

1 Like

@sdgsdg you should keep Hellis81’s answer as the solution, as it was the first working solution (and a fine one). :slight_smile: