Help with reformatting sensor.time__date

Hi there,

Wondering if you coding gurus can help me reformat the time and date presented by sensor.time__date which is currently 13:55, 2018-11-18 using a template sensor to give me something like this 13:55 18/11?

I’ve scoured the forums and tried every variation I’ve found without success.

This may help: https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

and this is my favorite that I use a lot:

1213_event_begin:
  value_template: "{{ as_timestamp(states.calendar['1213'].attributes.start_time) | timestamp_custom('%a, %b %d at %I:%M %p') }}"

It looks like this in lovelace: Sun, Nov 18 at 03:00 PM

Thanks for the prompt reply. I’ve taken your code and made this:
"{{ as_timestamp(states.sensor.time__date) | timestamp_custom('%a, %b %d at %I:%M %p') }}" but in the template editor I get "None"

The full code is:

  sensor:
    - platform: template
      sensors:
        1213_event_begin:
           value_template: "{{ as_timestamp(states.calendar['1213'].attributes.start_time) | timestamp_custom('%a, %b %d at %I:%M %p') }}"

Oh wait… I’m reformatting a unix/linux date… I don’t know about doing the ssensor.time__date thingie.

Thanks, but I’m trying to use your code just to tweak the date in the sensor sensor.time__date but when I do that I get “None” with:

"{{ as_timestamp('sensor.time__date') | timestamp_custom('%a, %b %d at %I:%M %p') }}" or
"{{ as_timestamp(states.sensor.time__date) | timestamp_custom('%a, %b %d at %I:%M %p') }}"

@anon43302295 You got any experience with this?

I think I have to update the entity under the new system to get the time/date to display. I’ve researched strftime and come up with this:

"{{ now().strftime('%H:%M %d/%m') }}"

Which gives me “14:34 18/11

Do you mean sensor.time_date? There should only be one underscore.

You probably just want to use sensor.time along with now() and timestamp_custom() template extensions. timestamp_custom() is like strftime().

This has an example:

Was actually very easy!

- platform: template
  sensors:
    simple_time_date:
      friendly_name: "Simple Time Date"
      value_template: "{{ now().strftime('%H:%M   %d/%m') }}"

I’m glad to see your success even without my stumbling around. :slight_smile:

That sensor will never update though. You need to include an entity_id to force updates, like sensor.time, or manually refresh it.

I do lots of stumbling!! That’s how I get by. :yum:

Thanks, I thought that might happen. I’ll look into again using your examples

My sensor for time_date

- platform: time_date
  display_options:
    - 'time'
    - 'time_date'

shows up in HA with two underscores sensor.time__date. Not sure how to use that in conjunction with now().

Well, that’s surprising to me. Anyways, you don’t really need to use that particular sensor for your template. You just need a sensor who’s state will change every minute, which is true of sensor.time. If you configure the template sensor with sensor.time as the entity id, your template will also update very minute.

Something like this I think should work.

- platform: template
  sensors:
    simple_time_date:
      friendly_name: "Simple Time Date"
      entity_id: sensor.time
      value_template: "{{ as_timestamp(now()) | timestamp_custom('%H:%M   %d/%m') }}"

now().strftime('%H:%M %d/%m') seems to work fine as well, I don’t really know when it’s preferred to use filters.

4 Likes

Indeed it did! Thank you so much. Was too busy trying to include the entity_id into the template.