# Calculating new values out of two attributes

I am currently strugling with doing a sum calculation for to values. but also just converting the time as a start

``````{{ (as_timestamp(state_attr('sensor.city1_to_city2', 'departure')) }}
{{ (as_timestamp(state_attr('sensor.city1_to_city2', 'departure'))  + as_timestamp(state_attr('sensor.city1_to_city2', 'delay')) }}

``````

the attributes I receive from a sensor

``````departure: "23:19"
arrival: "23:51"
transfers: 0
time: "0:26"
products:
- S
price: null
ontime: false
delay: 6
canceled: false
delay_arrival: 0
next: "23:34"
next_delay: 0
next_canceled: false
next_on: "23:49"
next_on_delay: 3
next_on_canceled: false

``````

any advise how to do this?

Thanks!

What are the units for `delay`? Is that 6 seconds, minutes, or hours? My guess is minutes but I would rather be sure before proceeding with a solution.

It is minutes. Thanks for asking!

``````{{ (today_at(state_attr('sensor.city1_to_city2', 'departure')) +
timedelta(minutes = state_attr('sensor.city1_to_city2', 'delay'))).strftime('%H:%M') }}
``````

Or it can be written like this using variables. It makes the last line a bit easier to understand.

``````{% set s = 'sensor.city1_to_city2' %}
{% set dt = state_attr(s, 'departure') %}
{% set d = state_attr(s, 'delay') %}
{{ (today_at(dt) + timedelta(minutes = d)).strftime('%H:%M') }}
``````
1 Like

Amazing this worked!
Which language is this script using?

Home Assistantâ€™s templating language is Jinja2.

#### Reference

Building templates

1 Like

Is there any way to get the values out of the â€śdeparturesâ€ť as there information is richer?

``````departure: "23:19"
arrival: "23:51"
transfers: 0
time: "0:26"
products:
- S
price: null
ontime: false
delay: 6
canceled: false
delay_arrival: 0
next: "23:34"
next_delay: 0
next_canceled: false
next_on: "23:49"
next_on_delay: 3
next_on_canceled: false
departures:
- departure: "23:19"
arrival: "23:51"
transfers: 0
time: "0:26"
products:
- S
price: null
ontime: false
delay: 6
canceled: false
delay_arrival: 0
- departure: "23:34"
arrival: "00:06"
transfers: 0
time: "0:32"
products:
- S
price: null
ontime: true
delay: 0
canceled: false
delay_arrival: 0
- departure: "23:49"
arrival: "00:21"
transfers: 0
time: "0:29"
products:
- S
price: null
ontime: false
delay: 3
canceled: false
delay_arrival: 0
``````

Yes but which one?

` departures` is a list containing three items.

All 3 of them? I would build templates accessing values. later I can put them into lovelace cards.