Binary sensor based on now() and two datetime helper

Hi all,

I wanted to create a binary_sensor which indicates if we are on vacation. I created the following helpers:

input_datetime.vakantie_start_datum
input_datetime.vakantie_eind_datum

both set to date only at the moment (preferably they would be date and time)

I create a helper based on a template with the following template:

template:
  - binary_sensor:
      - name: "vakantie_status"
        state: >
          {{ states('input_datetime.vakantie_eind_datum') | as_datetime | as_local <= now() 
          < states('input_datetime.vakantie_start_datum') | as_datetime | as_local }}

The Developer Tools template checkers show a correct functionality (returning true or false), but the helper itself does not function at all (always returns OFF).

Can anyone help me with this or let me know what to do?

If you want to check if the current date and time is between start and end then the format is like this:

start <= now < end

Here’s your corrected Template Binary Sensor:

template:
  - binary_sensor:
      - name: "vakantie_status"
        state: >
          {{ states('input_datetime.vakantie_start_datum') | as_datetime | as_local <= now()
            < states('input_datetime.vakantie_eind_datum') | as_datetime | as_local }}

Thanks for your help. I clearly oversaw the mixup of start and end. I did start initially with the template as you suggested.

I also used it for creating a new one for testing purposes but this is driving me crazy right now. The Developer Tools template checker returns a True or False based on the settings of the input helpers. The sensor remains showing “Off”.

Any thoughts on this?

Show your template binary sensor config exactly as it is now.

Copy-paste the following template into the Template Editor and report the results:

{{ now() }}

{{ states('input_datetime.vakantie_start_datum') }}
{{ states('input_datetime.vakantie_start_datum')
  | as_datetime | as_local}}
{{ states('input_datetime.vakantie_eind_datum') }}
{{ states('input_datetime.vakantie_eind_datum')
  | as_datetime | as_local }}

{{ states('input_datetime.vakantie_start_datum') | as_datetime | as_local <= now()
  < states('input_datetime.vakantie_eind_datum') | as_datetime | as_local }}
2023-09-12 17:29:00.408583+02:00

2023-09-04
2023-09-04 00:00:00+02:00
2023-09-14
2023-09-14 00:00:00+02:00

True

The results show that the corrected template is working properly.

After you modified your Template Binary Sensor’s configuration, with the corrected template, did you execute Developer Tools > YAML > Reload Template entities (or restart Home Assistant)?

I just tried the reload template entities => no change

Afterwards rebooted HA => no change

Check the Log for related error messages.

In addition, do you have one template: key in configuration.yaml or more than one? You can only have one.

Ok guys… Here is the fix:

Changed the code from:

template:
  - binary_sensor:
      - name: "vakantie_status"
        state: >
          {{ states('input_datetime.vakantie_start_datum') | as_datetime | as_local <= now()
            < states('input_datetime.vakantie_eind_datum') | as_datetime | as_local }}

To:

{{ states('input_datetime.vakantie_start_datum') | as_datetime | as_local <= now()
  < states('input_datetime.vakantie_eind_datum') | as_datetime | as_local }}

And it now works like a charm!

I don’t see any difference between those two templates.

Also that is the answer Taras provided, please mark their post as the solution.

Removing the following part was the issue:

template:
  - binary_sensor:
      - name: "vakantie_status"
        state: >

The original template returns a string in the Developer Tool template checker. Changing it as indicated in my previous post returns a boolean which is needed for the binary sensor.

I see. Yeah, if you are using the UI you only supply the template.