Help obtaining current month

Hi Guys,

Would someone be able to assist with an issue I am having. My code below seems to be outputting “May” instead of April

  - platform: template
    sensors:
      current_month:
        value_template: "{{ ['January','February','March','April','May','June','July','August','September','October','November','December'][now().month] }}"
        entity_id: sensor.time
1 Like

Have you tried this?

{{ ['January','February','March','April','May','June','July','August','September','October','November','December'][now().month - 1] }}

Array indexing starts with 0, not 1, so the 1st month is the 0th element of the array

Your Template Sensor contains two mistakes:

  • The first and most relevant one has already been identified by rak and ritchieframe (lists are zero-indexed).
  • The second one is that entity_id was deprecated for Template Sensors several versions ago and plays no part in the template’s evaluation.

FWIW, this also reports the current month:

  - platform: template
    sensors:
      current_month:
        value_template: "{{ now().timestamp() | timestamp_custom('%B') }}"

This is one of those things that has been too hard for years. If 2022 is going to be the year that the UX gets tighter, it would be wonderful to have built-in sensors to provide many common date functions to make automations very easy to set up in the UI. Thanks for considering this.

1 Like

What would you consider to be “common”?

Another option is to use strftime():

{{ now().strftime('%B') }}

Current day of week? Current month of year? Current year?

1 Like

Copy-paste this into the Template Editor:

Weekday: {{ now().weekday() }}

Month: {{ now().month }}

Year: {{ now().year }}

Obviously, there are many other date and time values but it’s impractical to make sensors of all of them by default and triggering by time and/or date is already handled by the Time Trigger.

However, if for some reason you need it as a sensor, you could simply make a Template Sensor. There’s also the Time & Date integration which creates a few date and time sensors (personally I don’t use it because now() provides the same and more).