Help with Date Time format when creating sensor from attribute

I have created a sensor for the next set point in a room of my house. All works fine but I have spent ages reading and searching in how to change the date/time format. I am struggling to find the answer and hoping someone can help. I am very new to this and so far it has been a really long learning curve but well worth it.

This is the sensor

- platform: template
  sensors:
    climate_office_setpoint_mode_next:
      friendly_name: Snug Setpoint Next
      entity_id: 
        - climate.office
      value_template: '{{ states.climate.office.attributes.status.setpoints.next_sp_from }}'

The date and time format returns as

2023-09-18T19:00:00+01:00

I would like it to be something like 18-09-2023 19:00:00 or 18th Sept 2023 19:00

Thanks
John

See: The EPIC Time Conversion and Manipulation Thread! - #5 by finity and The EPIC Time Conversion and Manipulation Thread! - #2 by finity

Use device_class: timestamp

Blockquote
See: The EPIC Time Conversion and Manipulation Thread! - #5 by finity and The EPIC Time Conversion and Manipulation Thread! - #2 by finity

I have read this article several times but just not able to see how or what to add to the template.
John

Please could you advise how to add this to the template.
Thanks
John

 {{ states('climate.office','status').setpoints.next_sp_from.strftime('%Y-%m-%d %H:%M') }}

Look at the options in the second link I posted to change the format of the string. You can test it in Developer Tools → Template Editor.


- platform: template
  sensors:
    climate_office_setpoint_mode_next:
      friendly_name: Snug Setpoint Next
      entity_id: 
        - climate.office
      value_template: '{{ states.climate.office.attributes.status.setpoints.next_sp_from }}'
      device_class: timestamp

Thank you for your help.
I tried as your suggestion but the date and time remain in the same format.
Thanks
John

Thank you,
I have read through the article and tried as you suggested but keep getting the error

str object’ has no attribute ‘setpoints’

Thanks
John

I have spent most of the afternoon trying to get this working but unfortunately not able to do so.
As an update I note all the sensors messages etc are all in the same format eg. 2023-09-18T19:00:00+01:00

So it looks like further investigation is needed but just where do I look.
Thanks for all the help
John

Just to be sure: Did you reload the template integration?

Copy the following and paste it into Dev Tools:


{% set x = states.climate.office.attributes.status.setpoints.next_sp_from  |as_datetime |as_local %}
{{ x.strftime('%d. %b %Y %H:%M') }}

Thank you. Yes I did reload. Will give this a try tomorrow.
Thank you for your help. Much appreciated.

Using your suggestion returns 19 Sep 2023 19:00 which is just how I would like.
However I am struggling to add the line to the template in file editor and keep getting various errors.
Thanks
John

Try:

value_template: |
  {% set x = states.climate.office.attributes.status.setpoints.next_sp_from  |as_datetime |as_local %}
  {{ x.strftime('%d. %b %Y %H:%M') }}

pedolsky

Thank you so much for all your time and trouble. Very much appreciated.
This is the final version…

- platform: template
  sensors:
    climate_office_setpoint_mode_next:
      friendly_name: Snug Setpoint Next
      value_template: |
        {% set x = states.climate.office.attributes.status.setpoints.next_sp_from  |as_datetime |as_local %}
        {{ x.strftime('%d. %b %Y %H:%M') }}

This is how it looks. Again many thanks.
Final Version