Showing only Day instead of date_time

May be simple enough, but not finding a way to show just the day (24, for example) instead of full date on main page.

Relevant section of configuration.yaml would be hugely appreciated.

Not sure where exactly you mean on the main page, do you have a custom card? I think you’ll need strftime

this will give you the day of the month: (use %d for a zero padded number)

    {{ now().strftime("%-d") }}

An existing yaml entry is as follows, please tell how should it look after incorporating your suggested modifications.

sensor:

  • platform: time_date
    display_options:
    • ‘time’
    • ‘date’

Apologies for being so ignorant. Characters are being messed up by html formatting.

There might be a better way, but I would use a template sensor for this, eg:

  - platform: template
    sensors:
      day_of_month:
        friendly_name: "Day"
        value_template: "{{ now().strftime('%-d') }}"

I am getting the following error. Not sure if it matters (I am almost certain it doesn’t), but I am running this on Windows 10.

2018-06-24 23:05:51 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.day_of_month fails
Traceback (most recent call last):
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\helpers\entity.py”, line 204, in async_update_ha_state
yield from self.async_device_update()
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\helpers\entity.py”, line 325, in async_device_update
yield from self.async_update()
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\asyncio\coroutines.py”, line 212, in coro
res = func(*args, **kw)
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\sensor\template.py”, line 184, in async_update
self._state = self._template.async_render()
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\helpers\template.py”, line 132, in async_render
return self._compiled.render(kwargs).strip()
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
File “C:\Users\comp\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\sandbox.py”, line 427, in call
return __context.call(__obj, *args, **kwargs)
ValueError: Invalid format string

And my yaml is as follows:

  - platform: template
    sensors:
      day_of_month:
        friendly_name: "Day"
        value_template: "{{ now().strftime('%0d') }}"

%0d is an invalid format type, try %d

Yes. Noticed after posting. Removed. Works fine. Thank you very much for all your help and support. Truly appreciated.

1 Like

fyi that value template will never update because you don’t have an entity_id attached to it. Templates only update when state changes occur to a states object. now() is not a states object. You need to add an entity_id to your template, something that updates every day. Doesn’t matter what:

  - platform: template
    sensors:
      day_of_month:
        friendly_name: "Day"
        value_template: "{{ now().strftime('%0d') }}"
        entity_id: light.some_light_that_turns_on_every_day

@mvdarend Also, as an FYI ‘%0d’ is a valid template. It will add a leading zero to days under 10, example 09 for the 9th day.

Are you sure? According to the documentation here %d has a leading zero and %-d doesn’t. (%0d isn’t listed)

Thanks for the tip on entity_id, I hadn’t thought of that.

yeah, that’s pythons strfttime. This strftime supported in the templates doesn’t support alot of pythons functionalities. For example the “%-d” doesn’t work in templates. That one really annoys the shit out of me because I use it all the time in python. Anyways, I have to always double check what the templates do with strftime because it’s different:

Capture

Capture

1 Like