# Convert String value for time to an integer (duration_in_traffic)

I’m trying to do an automation using the google travel sensor. I want to do some basic math based on two attributes. duration_in_traffic and duration. The value is something like

54 mins, 1 min, or 5 hours 26 mins, etc.

For me most the time the value is less than an hour, so my sensor should work, but today it was longer than an hour and did not work.

Here is an what works for me with a value of less than an hour

``````{{ state_attr('sensor.pauls_time_to_work_google','duration_in_traffic') | replace(" mins","") | replace(" min","") | int}}
``````

Ultimately I’d love just to get total minutes.

Any ideas how to convert a text string like this to minutes?

Maybe the split command can be used to split at ’ hours’ then follow up with what you already have to parse out the " mins", and do some math to get the desired format (hrs or min)

Only problem is I’m not sure how it handles input without " hours" in it… guessing you would have a 1x array in that case. This could be used; ie if variable[1] doesn’t exist, variable [0] must be minutes, otherwise it is [0] hrs and [1] min. This won’t work though, if the variable is not initialized every time. Not in front of a computer so can’t test a template in dev tools.

[edit: Sure enough, works as planned (tested in HA jinja template page):

``````{% set variable1 = "1 hour 15 mins" %}
{% set list1 = variable1.split(' hour') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}

Output:
The grass is 1 and the boat is  15 mins

{% set variable1 = "15 mins" %}
{% set list1 = variable1.split(' hour') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}

Output:
The grass is 15 mins and the boat is
``````

So looks like the variable is ‘reset realtime’ so if no “hours” you won’t get a list[1] value. Some more homework to deal with the second case, and it will hit a snag if google pluralizes hour, like “2 hours”, the added s screws it up (pretty sure it does pluralize). The latter can be handled by replacing hours with hour first. The former will require an if statement to check for (in this case) the existance of list1[1], then handle those 2 cases differently.

1 Like