rzanten
(rzanten)
September 12, 2023, 10:08am
1
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?
123
(Taras)
September 12, 2023, 1:10pm
3
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 }}
rzanten
(rzanten)
September 12, 2023, 2:09pm
4
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?
tom_l
September 12, 2023, 3:13pm
5
Show your template binary sensor config exactly as it is now.
123
(Taras)
September 12, 2023, 3:27pm
6
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 }}
rzanten
(rzanten)
September 12, 2023, 3:29pm
7
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
123
(Taras)
September 12, 2023, 3:34pm
8
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)?
rzanten
(rzanten)
September 12, 2023, 4:20pm
9
I just tried the reload template entities => no change
Afterwards rebooted HA => no change
123
(Taras)
September 12, 2023, 4:35pm
10
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.
rzanten
(rzanten)
September 13, 2023, 2:27pm
11
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!
tom_l
September 13, 2023, 3:02pm
12
I don’t see any difference between those two templates.
Also that is the answer Taras provided, please mark their post as the solution.
rzanten
(rzanten)
September 13, 2023, 4:36pm
13
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.
tom_l
September 13, 2023, 4:40pm
14
I see. Yeah, if you are using the UI you only supply the template.