Template Sensor day

Hello, I know this is all wrong, but I have a sensor that gives me the day of the month ( 1, 2, 3, 4 etc.) im trying to get it to list (1st, 2nd, 3rd, 4th etc.)

what am I missing? thanks :slight_smile:

day_pretty:
  friendly_name: 'Day Pretty'
  value_template: >-
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '1') %}
        1st
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '2') %}   
        2nd
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '3') %}
        3rd
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '4') %}   
        4th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '5') %}
        5th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '6') %}   
        6th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '7') %}
        7th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '8') %}   
        8th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '9') %}
        9th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '10') %}   
        10th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '11') %}
        11th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '12') %}   
        12th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '13') %}
        13th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '14') %}   
        14th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '15') %}
        15th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '16') %}   
        16th 
     {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '17') %}
        17th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '18') %}   
        18th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '19') %}
        19th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '20') %}   
        20th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '21') %}
        21st
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '22') %}   
        22nd
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '23') %}
        23rd
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '24') %}
        24th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '25') %}
        25th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '26') %}   
        26th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '27') %}
        27th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '28') %}
        28th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '29') %}
        29th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '30') %}
        30th
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '31') %}
        31st                                
      {% endif %}
    {% else %}
      Unknown
    {% endif %}

I think this should do it:

day_pretty:
  friendly_name: 'Day Pretty'
  value_template: >-
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '1') %}
        1st
      {% elif is_state('sensor.day_us', '2') %}   
        2nd
      {% elif is_state('sensor.day_us', '3') %}
        3rd
      {% elif is_state('sensor.day_us', '4') %}   
        4th
      .
      .
      .
      {% elif is_state('sensor.day_us', '30') %}
        30th
      {% elif is_state('sensor.day_us', '31') %}
        31st                                
      {% endif %}
    {% else %}
      Unknown
    {% endif %}

strange, this is what I got.

pretty_day:
  value_template: >-
    {% if states.sensor.day_us.state %}
      {% if is_state('sensor.day_us', '1') %}
        1st
      {% elif is_state('sensor.day_us', '2') %}   
        2nd
      {% elif is_state('sensor.day_us', '3') %}
        3rd
      {% elif is_state('sensor.day_us', '4') %}   
        4th
      {% if is_state('sensor.day_us', '5') %}
        5th
      {% elif is_state('sensor.day_us', '6') %}   
        6th
      {% elif is_state('sensor.day_us', '7') %}
        7th
      {% elif is_state('sensor.day_us', '8') %}   
        8th            
      {% if is_state('sensor.day_us', '9') %}
        9th
      {% elif is_state('sensor.day_us', '10') %}   
        10th
      {% elif is_state('sensor.day_us', '11') %}
        11th
      {% elif is_state('sensor.day_us', '12') %}   
        12th
      {% if is_state('sensor.day_us', '13') %}
        13th
      {% elif is_state('sensor.day_us', '14') %}   
        14th
      {% elif is_state('sensor.day_us', '15') %}
        15th
      {% elif is_state('sensor.day_us', '16') %}   
        16th
      {% if is_state('sensor.day_us', '17') %}
        17th
      {% elif is_state('sensor.day_us', '18') %}   
        18th
      {% elif is_state('sensor.day_us', '19') %}
        19th
      {% elif is_state('sensor.day_us', '20') %}   
        20th
      {% if is_state('sensor.day_us', '21') %}
        21st
      {% elif is_state('sensor.day_us', '22') %}   
        22nd
      {% elif is_state('sensor.day_us', '23') %}
        23rd
      {% elif is_state('sensor.day_us', '24') %}   
        24th            
      {% if is_state('sensor.day_us', '25') %}
        25th
      {% elif is_state('sensor.day_us', '26') %}   
        26th
      {% elif is_state('sensor.day_us', '27') %}
        27th
      {% elif is_state('sensor.day_us', '28') %}   
        28th
      {% if is_state('sensor.day_us', '29') %}
        29th                    
      {% elif is_state('sensor.day_us', '30') %}
        30th
      {% elif is_state('sensor.day_us', '31') %}
        31st                                
      {% endif %}
    {% else %}
      Unknown
    {% endif %}

There’s no errors in the check config, so I restarted but the sensor will not show up.

But it also will not pass the in the template in the ui.

try leaving out the outer if statements and just leave the “if the day is 1 then 1st” statements.

1 Like

You can copy the macro and use it anywhere. Works with all numbers (int/string int) to infinity.

EDIT: I should mention that ‘unknown’ will result in 0th day. So that’s a thing. So you don’t need to check to see if the day_us sensor exists, but remember that 0th day is unknown.

pretty_day: 
  value_template: >
    {%- macro ordinal(num) %}
    {%- set d = {1:'st',2:'nd',3:'rd'} %}
    {%- set num = num | int %}
    {%- if 10 < num % 100 <= 20 %}
      {{- num }}th
    {%- else %}
      {{- num }}{{ d.get(num % 10, 'th') }}
    {%- endif %}
    {%- endmacro %}
    {{ ordinal(states('sensor.day_us')) }}

If you don’t like macros, this does the same without the macro.

pretty_day: 
  value_template: >
    {%- set num = states('sensor.day_us') | int %}
    {%- set d = {1:'st',2:'nd',3:'rd'} %}
    {%- if 10 < num % 100 <= 20 %}
      {{- num }}th
    {%- else %}
      {{- num }}{{ d.get(num % 10, 'th') }}
    {%- endif %}

3 Likes

@petro This is perfect, thank you my friend!

Also @finity thanks you for getting me started! I appreciate you guys!

1 Like