ESPHome / Check if date is in range

Hy all from Germany.

I want to check if a date is in a date-range…

For example: the date is 15.12.2023 and the date-range is 11.12.2023 - 19.12.2023

If the date is in the range, i want to write a text on a display in another color….

But how can i check this?


It does depend a bit on what sensors your dates are in.

I’d suggest reviewing this page and section.

I would say making your own lambda condition with your date range compares done in timestamps is probably a decent option.

Or you could make a template binary sensor etc.

So maybe share a bit more about use case and current config.

Fundamentally dates and time time in computer speak are represented by an integer which represents the seconds since 1.1.1970. So it is pretty easy to compare - just use if/then statements.

Good morning and thx for your answers :slight_smile:

Ok, the background:
I have fix dates when i have to clean the stairs in the house.
I insert the dates into the calendar in HA and made template_sensors to convert them

  - name: "Test Start"
    state: | 
        {{ as_timestamp(strptime(state_attr('calendar.test', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%d.%m.%Y") }}
  - name: "Test Ende"
    state: | 
        {{ as_timestamp(strptime(state_attr('calendar.test', 'end_time'), '%Y-%m-%d %H:%M:%S') - timedelta(days=1)) | timestamp_custom("%d.%m.%Y") }}

Then i import the new dates in ESPHome

  - platform: homeassistant
    name: "test_start"
    id: test_start
    entity_id: sensor.test_start
    internal: False
  - platform: homeassistant
    name: "test_ende"
    id: test_ende
    entity_id: sensor.test_ende
    internal: False

and write them on a display:

it.printf(2, 100, id(font3), id(my_white), TextAlign::BASELINE_LEFT, "  %s", id(test_start).state.c_str());
          it.printf(2, 120, id(font3), id(my_white), TextAlign::BASELINE_LEFT, "  %s", id(test_ende).state.c_str());

Now, i want to write the dates on the display in another color, if the todays date is in range (and i have to clean the stairs :smiley: )

Because you are using fixed dates you can check if the current date is in range by formatting all the dates fields in “YYMMDD” format and then check it in the display lambda. To make it as easy as possible you can add two extra sensors in HA with start & end date in YYMMDD format and ‘download’ them to the esp as well just for the IF statement, so you don’t have to manipulate the date sensors you are displaying on the screen.

if (id(test_start_YYMMDD).state <= id(today_date_YYMMDD).state && id(today_date_YYMMDD).state <= id(test_ende_YYMMDD).state) {
   // within range
}  else {
  // outside range

I would be tempted to create a similar template binary sensor on the HA side for this since they are already doing some templating there, and then import that binary sensor into and use that to drive things.

Typically I like to do things on the esp but if you do it on the HA side then it’s easier to access it for other alerts etc.

But either will work.

Thanks for the tip :slight_smile:

I will try…