Template for "in {n} days" / "Today" / "Tomorrow"

Hi,

I’m quite new here. I have a few sensor that contain dates (time part is irrelevant). Now I wan’t to display them in lovelace with values like “Today” or “in 5 days”. I’ve tried setting the device class to “timestamp” and that helped a little. But there are a few issues with that:

  • My iPad App says “Invalid date”
  • Because the Date also includes the time (always 00:00:00) it says “6 hours ago” at 6am. Instead it should say “Today” the whole day.

What is best practice to achieve this behaviour?

{% set update = states('sensor.date') %}
{% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
{% set event = '2020-11-03' | as_timestamp %}
{% set delta = ((event - midnight) // 86400) | int %}
{% if delta < 0 %}
  {{ -delta }} Days Ago
{% elif delta == 0 %}
  Today!
{% elif delta == 1 %}
  Tomorrow
{% else %}
  In {{ delta }} Days
{% endif %}

Just change the value of the “event” to whatever value you want or set it to an entity that results in a valid datetime object.

4 Likes

Thanks, that worked!

hay bro dont give your self the solution

@finity did the hard work

3 Likes