Template Sensor for days remaining?

Hi,

How can I create a sensor which display how many days between today and (for example) my birthday 01/05/2023?

One easy option is to use the Anniversaries custom integration.

1 Like

Assuming your birthday is May 1st, this will report the number days between today and your birthday. If the current day is past your birthday, it will report the number of days until next year’s birthday.

template:
  - sensor:
      - name: My Birthday
        state: >
           {% set bd = today_at().replace(month=5, day=1) %}
           {% set bd = bd if bd > now() else bd.replace(year = now().year + 1) %}
           {{ (bd - now()).days }}

The following is a slightly more efficient version which only updates itself at the start of every new day (the other one updates itself every minute).

template:
  - trigger:
      - platform: time
        at: '00:00:01'
    sensor:
      - name: My Birthday
        state: >
           {% set bd = today_at().replace(month=5, day=1) %}
           {% set bd = bd if bd > now() else bd.replace(year = now().year + 1) %}
           {{ (bd - now()).days }}

Reference: Template Sensor

Great - many thanks!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.