Formatting datetime in scripts

Hi. I’m rather new to Home Assistant, so please excuse what is probably a really basic question!

I have a script which sets the time that the AC will start and then updates a string in a Lovelace Picture Elements Card to say something along the lines of “AC will start at 17:30”

I’ve got it working with the default long time format, but cannot work out how to make it a different date format HH:MM.

service: input_text.set_value
entity_id: input_text.ac_status_message
data:
  value: 'AC starting at: {{ states(''input_datetime.ac_start_time'') }}'

I’ve read numerous posts about date formatting, but whenever I try adding more complexity into the value line it throws up syntax errors.

Do I need to break it into more than one line? Is there a modifier that will apply time formatting to this value? Help! I’m lost!

Thanks

Try this:

service: input_text.set_value
entity_id: input_text.ac_status_message
data:
  value: 'AC starting at: {{ states(''input_datetime.ac_start_time'')[-8:-3] }}'

It means to slice the string starting from the 8th character from the end to the 3rd character from the end.

2020-11-16 10:14:15
           ^    ^
          -8   -3
1 Like

Thanks. That worked perfectly!

1 Like