Little help or suggestion for time

Here is part of morning announcement which I will post but this is the part I’d like to figure out something different… The date is read from here:

Today is {{ now().strftime('%A')}}
          {% set cmonth = now().strftime('%m') %}  
          {% if cmonth == '1' %}
          January
          {% elif cmonth == '2'%}
          Feburary
          {% elif cmonth == '3'%}
          March
          {% elif cmonth == '4'%}
          April
          {% elif cmonth == '5'%}
          May
          {% elif cmonth == '6'%}
          June
          {% elif cmonth == '7'%}
          July
          {% elif cmonth == '8'%}
          August
          {% elif cmonth == '9'%}
          September
          {% elif cmonth == '10'%}
          October
          {% elif cmonth == '11'%}
          November
          {% elif cmonth == '12'%}
          December           
          {% endif %}
          {{ now().strftime('%d')}}

When the system says the date it will say:
“Today is December zero nine”

Question how can I get it to say
“Today is December 9th”

I put this in the template editor and it works but there has to be a better way so I don’t have to have 31 if statements :slight_smile: [I’m pretty sure I can shorten the month if statements as well]

{% set days = now().strftime('%d') %}
{% if days == "09" %}
9th
{% else %}
{% endif %}

Thanks in advance!!!

Interesting question and no direct answer but for the month you could use %B instead of paging through

Formatting Dates and Time (sendwithus.com)

1 Like

or nicer without the leading 0

{% set myDate = now().strftime('%d') %}
{% set date_suffix = ["th", "st", "nd", "rd"] %}
{% if myDate[-2] in [1, 2, 3] and myDate not in [11, 12, 13] %}
{{  now().strftime('%B %d').replace(' 0', ' ') + date_suffix[myDate % 10] }}
{% else %}
{{ now().strftime('%B %d').replace(' 0', ' ') + date_suffix[0] }}
{% endif %}
1 Like

Perfect!! Thank you !!

 Today is          
          {% set myDate = now().strftime('%d') %}
          {% set date_suffix = ["th", "st", "nd", "rd"] %}
          {% if myDate[-2] in [1, 2, 3] and myDate not in [11, 12, 13] %}
          {{  now().strftime('%B %d').replace(' 0', ' ') + date_suffix[myDate % 10] }}
          {% else %}
          {{ now().strftime('%B %d').replace(' 0', ' ') + date_suffix[0] }}
          {% endif %}

Says it perfectly now :slight_smile:

Here is the entire automation :slight_smile: I like to share.

- alias: 96 - Morning
  id: Morning
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_motion_sensor_occupancy
      from: "off"
      to: "on"
  condition:
    condition: and
    conditions:
      - condition: time
        after: "04:00:00"
        before: "11:30:00"
      - condition: state
        entity_id: input_boolean.run_once
        state: "on"
  action:
    - service: script.save_sonos_volume
    - service: sonos.snapshot
      data:
        entity_id: media_player.sonos_five
    - service: media_player.volume_set
      data:
        volume_level: 0.60
      target:
        entity_id: media_player.sonos_five
    - service: tts.cloud_say
      entity_id: media_player.sonos_five
      data_template:
        message: >
          {% if now().strftime("%H")|int < 12 %}
          {% set firstname = "John" %}
          Good morning {{firstname}}! 
          {% endif %}
          {% if is_state('sensor.season', 'spring') %} it's Spring!
          {{ ('name its Springr!') | replace("name", ("")) }}
          {% elif is_state('sensor.season', 'summer') %} 
          {{ ('name its Summer! Your favorite time of year!!') | replace("name", ("")) }} 
          {% elif is_state('sensor.season', 'autumn') %} 
          {{ ('name its Fall!') | replace("name", ("")) }}
          {% else %} {{ ('name its Winter.....hang in there Spring is coming!') | replace("name", ("")) }} 
          {% endif %}          


          Today is          
          {% set myDate = now().strftime('%d') %}
          {% set date_suffix = ["th", "st", "nd", "rd"] %}
          {% if myDate[-2] in [1, 2, 3] and myDate not in [11, 12, 13] %}
          {{  now().strftime('%B %d').replace(' 0', ' ') + date_suffix[myDate % 10] }}
          {% else %}
          {{ now().strftime('%B %d').replace(' 0', ' ') + date_suffix[0] }}
          {% endif %}
          and the time is currently {{ states('sensor.12hourtime') }}
          {% set cday = now().strftime('%A') %}
          {% set season = states('sensor.season') %}

          {% if (season == 'spring') and (cday == "Saturday") %}
          Since it's Spring and {{ cday }} you don't have to work.  Check the weather if it's nice lets go outside today!!!
          {% elif  season == ('summer') and (cday == "Saturday") %}
          Since it's Summer and {{ cday }} you don't have to work.  Let's not sit in the house....I"m sure there's something we can do today!!
          {% elif season == ('winter') and (cday == "Saturday") %}
          Since it's  Winter and {{ cday }} you don't have to work.  If it's cold stay in... you've had enough this week already I'm sure.
          {% elif season == ('autumn') and (cday == "Saturday") %}
          Since it's Fall and {{ cday }} you don't have to work.  If it's still nice out go out and get stuff done before it gets too cold.
          {% endif %}

          {% if (season == 'spring')  and (cday == "Sunday") %}
          Since it's Spring and {{ cday }} This is your day... to do whatever you want!  Check the weather if it's nice lets go outside today!!!
          {% elif  season == ('summer')  and (cday == "Sunday") %}
          Since it's Summer and {{ cday }} Well it's your day.  Let's not sit in the house....I"m sure there's something we can do today!!
          {% elif season == ('winter')  and (cday == "Sunday") %}
          Since it's  Winter and {{ cday }} If it's cold stay in... you've had enough this week already.
          {% elif season == ('autumn')  and (cday == "Sunday") %}
          Since it's Autumn and {{ cday }} you don't have to do anything if you don't want to...this is your day!
          {% endif %}
          The outside temperature is currently {{ state_attr('weather.dark_sky', 'temperature')  | round }} degrees and feels like {{ states('sensor.budd_st_weather_feels_like') | round }} degrees out. The inside
          temperature is currently {{ state_attr('climate.thermostat', 'current_temperature') | round }} degrees and the current Humidity indoors is at
          {{ state_attr('sensor.living_room_temp_sensor_humidity', 'humidity')  | round }}%.

          {# HVAC #}  
          {# AC #}          
          {% if states('climate.john_s_device') == 'cool' %} 
          the AC is ready
          {% else %}
          {% endif %}
          {# HEAT #}
          {% if states('climate.thermostat') == 'heat' %}
          {% set mode = state_attr('climate.thermostat', 'hvac_action') %}
          {% if state_attr('climate.thermostat', 'hvac_action') == 'heating' %}
          the heat is currently running
          {% else %}
          The heat is on and on standby mode          
          {% endif %}
          {% endif %}

          {#   UV Index   #}
          {% if states('sensor.budd_st_weather_uv')|int > 0 and states('sensor.budd_st_weather_uv')|int <= 10 %}
          The current UV index is {{ states('sensor.budd_st_weather_uv') }}.          
          {% endif %}
          {#  END UV Index   #}
          {% if is_state('sun.sun', 'below_horizon') %}
          You have
          {% set seconds = as_timestamp(states.sun.sun.attributes.next_rising)-now().timestamp() %}
          {% set hours = seconds / 60 %}
          {% if seconds / ( 60 * 60 ) > 1 %}
            {{ (seconds //  ( 60 * 60 )) | int }} hours
          {% else %}
            {{ (seconds // 60) | int }} minutes
          {% endif %}
          {{ [
            'until the sun rises.',
            'until the sun is up.',
            'before the sun officially rises. '
          ]|random }}
          {% else %}
          You have
          {% set seconds = as_timestamp(states.sun.sun.attributes.next_setting)-now().timestamp() %}
          {% set hours = seconds / 60 %}
          {% if seconds / ( 60 * 60 ) > 1 %}
            {{ (seconds //  ( 60 * 60 )) | int }} hours
          {% else %}
            {{ (seconds // 60) | int }} minutes
          {% endif %}
          {{ [
            'until the sun sets for the day.',
            'until the sun slips below the horizon.',
            'before the sun officially sets. '
          ]|random }}
          {% endif %}
          {% if now().strftime('%a') == 'Mon' %}           
            {{ [ 'It is Monday...Well here we fucking go again... ',
              'It is Monday it could always be worse...'
            ] | random }}           
          {% endif %}
          {% if now().strftime('%a') == 'Tue' %}           
            {{ [ 'Its Tuesday...Well we are one day closer.. ',
              'Well....how about that, its Tuesday'
            ] | random }}           
          {% endif %}
          {% if now().strftime('%a') == 'Wed' %}           
            {{ [ 'Do not forget tomorrow is Trash Day. ',
              ' Wednesday.... middle of the week..'
            ] | random }}           
          {% endif %}
          {% if now().strftime('%a') == 'Thu' %}           
            {{ [ 'It is Trash Day. ',
              ' The trash and recycle need to go out.'
            ] | random }}
          {% endif %}
          {% if now().strftime('%a') == 'Fri' %}           
            {{ [ 'Well it is Friday, we made it. Here comes another boring weekend. ',
              ' Happy TGIF '
            ] | random }}           
          {% endif %}
          {% if now().strftime('%a') == 'Sat' %}           
            {{ [ 'Another weekend.. plan work for next week John. ',
              'What shall we do on this fine Saturday?'
            ] | random }}           
          {% endif %}
          {% if now().strftime('%a') == 'Sun' %}           
            {{ [ 'Back to work tomorrow, are you ready?. ',
              'Is your wash done?  What"s is for dinner?'
            ] | random }}           
          {% endif %}

          The Daily weather summary......  {{states('sensor.dark_sky_daily_summary')}}.....
          The predicted precipitation today is {% if states('sensor.openweathermap_forecast_precipitation') == 0 %} zero inches. {% else %} {{ states('sensor.openweathermap_forecast_precipitation') }} inches. {% endif %}.....
          With a high of {{ state_attr('weather.dark_sky', 'forecast')[0].temperature }}° .....
          and a low of {{ state_attr('weather.dark_sky', 'forecast')[0].templow }}°

          {#   Closing Statement   #}  
          Here is your daily affirmation.....
          {{ [' I am inspired by things happening all around me....',
              'I am grateful for the people I have in my life....',
              'I grow and improve every day....',
              'I treat myself kindly and with compassion....',
              'I am able to find the silver linings in difficult situations....',
              'I always try to see the bright side of things....',
              'I am grateful for the things I have in life.....',
              'I appreciate the opportunities I have been given.....',
              'I am kind and help inspire others to be kind.....',
              'My life is full of potential.....',
              'I give myself permission to be myself.....',
              'I give myself permission to take time for myself.....',
              'I have the power to change.....',
              'I forgive those who have hurt me.....',
              'I have the ability to recover from difficulties.....',
              'I can control how I react to others.....',
              'My emotions have purpose and value.',
              'I trust myself and my feelings.....',
              'I accept myself just the way I am.....',
              'I feel safe and secure wherever I am.....',
              'I am calm and content.....',
              'I am allowed to say no and stand up to my negative thoughts.....',
              'I have the power to let things go.....',
              'I matter to a lot of people.....',
              'I like who I am.....',
              'I am allowed to feel and express my emotions.....',
              'I am powerful.....',
              'I am a good person.....',
              'I use setbacks to make me stronger.....',
              'Happiness is a choice, and I choose to be happy.....',] 
          | random }}  

          {{ states ('sensor.holiday_time')}}

          .... Have a great day!!!.....be safe...

    - delay: "00:01:40"
    - service: script.tts_restore_sonos_volume
    - service: sonos.restore
      data: {}
      entity_id: media_player.sonos_five
    - service: homeassistant.turn_off
      entity_id: input_boolean.run_once

Yes I’m sure I can shorten it in a few places but I’m happy that I figured out this much LOL.

Thank you all again!!!

:crazy_face: … glad it worked

1 Like

Yes sounds much better then today is december zero nine :joy:

strftime() supports removing leading zeros using -, so you could get rid of a few replace() functions…

{{ now().strftime('%B %-d') ...
2 Likes

How on earth would one know about this :slight_smile:

One gets frustrated and reads documentation and many forum posts… :laughing:

:rofl: :joy:

You guys are too funny