How to show day of week/month as names in entities?

Hello,
I am trying to create 2 new entities for my HomeAssistant instance which should display the (named) month/day of the week so that I can use these for my automations.

Following several guides and forum posts I managed to write below code in my configuration.yaml file.
Both entities show an ‘Off’ state and I figured this is because somewhere earlier in my configuration.yaml file I have written “binary_sensor:” along with some other configurations.

What sensor type would I need to use to get the values as names?
Also pointers towards documentation for this is very much appreciated as I couldn’t find it
Other feedback is also more than welcome. :slight_smile:

Thank you.

  - platform: template
    sensors:
      weekday:
        friendly_name: "Weekday"
        value_template: "{ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][now().weekday*()] }}"
  - platform: template
    sensors:
      month:
        friendly_name: "Month"
        value_template: "{ ['January','February','March','April','May','June','July','August','September','October','November','December'][now().month - 1] }}"

You can just use strftime to convert the time into day and month:

{{ now().strftime('%A') }}  .... Gives you Monday, Tuesday, etc
{{ now().strftime('%B') }}  .... Gives you January, February, etc

So putting the following in templates.yaml:

- trigger:
    - platform: time
      at: "00:00:00"
    - platform: homeassistant
      event: start
  sensor:
    - name: Day of Week
      unique_id: week_day
      state: >-
        {{ now().strftime('%A') }}

Note that I put these sort of things on a trigger that only update once a day or when HA restarts. Otherwise HA would update the sensor every minute, which is reasonably unlikely to change throughout the day. Therefore if you just paste this in and use Developer Tools to reload “Template Entities”, it will stay undefined until midnight or you restart HA; it will be fine from then on.

1 Like

Above is correct but to answer some of your questions…

the type would be sensor vice binary_sensor as you say you currently have.
However… you are using the Legacy format.

You would do well to convert it to the modern Template format which @michaelblight is using above.

In that case the integration is actually template not sensor. So if you were to put the above into configuration.yaml you would simply add template: above and indent everything else two spaces.

template:
  - trigger:
      - platform: time
        at: "00:00:00"
      - platform: homeassistant
        event: start
    sensor:
      - name: Day of Week
        unique_id: week_day
        state: >-
          {{ now().strftime('%A') }}
      - name: Month
        unique_id: month_of_year
        state: >-
          {{ now().strftime('%B') }}

Here is some more reading on Splitting up the Configuration for further education and to clarify what was posted above regarding templates.yaml