xbmcnut
(xbmcnut)
November 18, 2018, 1:00am
1
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.
Dixey
November 18, 2018, 1:06am
2
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
xbmcnut
(xbmcnut)
November 18, 2018, 1:14am
3
Dixey:
This may help
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"
Dixey
November 18, 2018, 1:22am
4
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') }}"
Dixey
November 18, 2018, 1:29am
5
Oh wait… I’m reformatting a unix/linux date… I don’t know about doing the ssensor.time__date thingie.
xbmcnut
(xbmcnut)
November 18, 2018, 1:29am
6
Dixey:
The full code is:
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') }}"
xbmcnut
(xbmcnut)
November 18, 2018, 1:30am
7
@anon43302295 You got any experience with this?
xbmcnut
(xbmcnut)
November 18, 2018, 1:38am
8
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 ”
freshcoast
(Z-Wave AI Bot)
November 18, 2018, 1:46am
9
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:
xbmcnut
(xbmcnut)
November 18, 2018, 1:46am
10
Was actually very easy!
- platform: template
sensors:
simple_time_date:
friendly_name: "Simple Time Date"
value_template: "{{ now().strftime('%H:%M %d/%m') }}"
Dixey
November 18, 2018, 1:47am
11
I’m glad to see your success even without my stumbling around.
freshcoast
(Z-Wave AI Bot)
November 18, 2018, 1:48am
12
That sensor will never update though. You need to include an entity_id to force updates, like sensor.time, or manually refresh it.
xbmcnut
(xbmcnut)
November 18, 2018, 1:48am
13
I do lots of stumbling!! That’s how I get by.
xbmcnut
(xbmcnut)
November 18, 2018, 1:49am
14
Thanks, I thought that might happen. I’ll look into again using your examples
xbmcnut
(xbmcnut)
November 18, 2018, 1:53am
15
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()
.
freshcoast
(Z-Wave AI Bot)
November 18, 2018, 1:59am
16
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
xbmcnut
(xbmcnut)
November 18, 2018, 2:04am
17
Indeed it did! Thank you so much. Was too busy trying to include the entity_id into the template.