Tutorial for noobs: how to get the current month in a sensor/entity

I spent most of the day struggling to create a simple entity/sensor that would output the current month name. Several dozen browser tabs later, I was none the wiser. Every discussion I found provided instructions that were either outdated (thanks to recent HA updates) or weren’t detailed enough (i.e. I had no idea how to implement their solutions).

I accidentally got it working, so I figured I’d explain how here, in excruciating detail, in case someone else new to Home Assistant is also struggling with this.

  1. Go to Settings.
  2. Go to Devices and services.
  3. Go to the “Helpers” tab.
  4. Click the “create helper” button.
  5. Choose “template” from the list.
  6. Choose “template a sensor”. Do not choose “template a binary sensor”.
  7. In the next window that appears, enter a name for the entity, e.g. “CurrentMonthName”.
  8. In the “state template” box, enter the following:

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

Don’t alter anything else. The preview at the bottom should show the name of the current month. Click submit to save it.

Then you can go to Settings > Devices > Entities tab and search for the name you gave it in step 7, e.g. “CurrentMonthName”, and it should be there. You should also then be able to use it in automations, such as "only do this task if CurrentMonthName = January.

Side note: yes, I know you can use the time and date option for triggers in the new automation UI, but that doesn’t help when you want to use it as a condition (“and if” in the new automation creation UI) rather than as a trigger.

Further reading: this uses the strftime Python method. Using the information there (and also here), it is possible to obtain other values, such as day, time, day name, week number of the year, year, etc. Note that the current month name is denoted by “%B” in the above code.

The strftime method takes any time input and converts it to whatever format is specified; the template for use is {{ input_time().strftime(‘format’) }}, if that makes it easier to understand… so in the above example, we’re providing “now” as the time for input, and %B as the format for output (meaning the current month name).

If, for instance, you want the current month expressed as a number, you can use %m or %-m (use %-m if you don’t want the leading zero, e.g. “1” instead of “01” for January). This then provides a numerical value to the entity, so you can then use less than or more than conditions when creating automations.

(The alternative, using %B as above, results in a string (an alphanumerical value) rather than a number, which becomes more cumbersome when creating automations.)

Keep in mind that - when in the template sensor creation window, you can experiment with the format and you can see immediately the output example in the preview below. This makes it faster to figure out if you’re going to have the desired output.

Hope this helps someone else save all the time I just wasted today trying to figure this out. :slight_smile:

3 Likes

Or use {{ now().month }} which returns 1 to 12.

Use in a template condition (this one will only work in June):

image

But if you’re creating a template sensor to store the month, the sensor state is always a string so you’d need to remember to cast it to int before working with it: otherwise February ("2") is greater than December ("12"); and you’ll get comparison errors comparing str with int.

There is a lot of generally good official documentation: the real “tutorial for noobs” should be to read it. This bit is all in the TIME section of the Templating documentation:

1 Like

Thank you! It got me started.

I use this one to display the current day and date for my wall mounted tablet dashboard:

{% set weekdays = ["maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag", "zondag"] %}
{% set months = ["januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"] %}
{{ "Het is " + weekdays[now().weekday()] + "  " + now().strftime("%-d") + " " + months[now().month - 1] + " " +  now().strftime("%Y")}}

The output will be: Het is donderdag 13 juni 2024

Just add a new Helper > choose Template > Template a sensor. Under ‘state template’ add the code from above.

After saving, you will get a entity-id, which you can use on your dashboard card yaml.

1 Like