Templating - How to cut up attributes string

Hello!

I have a states.calendar.work.attributes.start_time = 2016-12-15 12:00:00

What is the best way to cut out the date, because i only need the time.

My current template setup:

work_start_time:
      value_template: '{{ states.calendar.work.attributes.start_time }}'

Thanks in advance, guys!

Have you tried strptime(string, "%H%M") ?

Thanks!

I havent tried anything, cause im very new in this part of HA.
Do you know where should i start learning about strptime, etc.

1 Like

Please forgive my inexperience!

I tried

work_start_time2:
      value_template: '{{ strptime("2016-12-15 12:00:00", "%H%M") }}'

but the output is the same (2016-12-15 12:00:00)

I couldn’t get strptime to work for me, either.

This is hacky, but you could use

{{states.calendar.work.attributes.start_time.split(’ ')[1]}}

That’s “split on spaces” to get a list, and then take 1 (zero based lists).

Templating is based on Jinja. Also, use the template dev tool (button at bottom of HASS sidebar) to debug templates.

2 Likes

Thank you! That’s how I wanted to workaround this issue.

If strtime not working, is there any other way to change time formats?
2016-12-10 to Dec 10, 2016. (Google maps api takes that format…)

Edit: I know strtime isn’t exactly format changer. :blush:

I wonder if it’s not working there because of the data type of what’s being passed. I was able to get strptime to work on a timestamp (last_triggered) for an automation. What if you try:

{{ states.calendar.work.attributes.start_time.strptime("%H:%M") }}

(again - play around with this in the template dev tool - you can debug in real time)

Updated - I had strftime in my example - meant strptime.

If strptime is not working, you should post a bug report here: https://github.com/home-assistant/home-assistant/issues/new

Should strptime work on a string?

Thanks.
Intrestingly, the dev tool debugger shows the correct output (12:00:00) for your template, but hass --script check_config shows an error:

 while parsing a flow mapping
  in "/home/hass/.homeassistant/configuration.yaml", line 136, column 24
expected ',' or '}', but got '['
  in "/home/hass/.homeassistant/configuration.yaml", line 136, column 79

Can you post what you’ve got on line 136?

You might try this alternate syntax:

value_template: >
  {{ states.calendar.work.attributes.start_time.strptime("%H:%M") }}