Like most people in the world I work a lot of the days that the Python holiday module calls a holiday. I have tried a number of times to include the remove_holiday section to my configuration.yaml and it does not work.
Which is basically a cut and paste from the documentation on the workday sensor with the dates changed and when I run this through Check config it says everything is ok but when I restart HA there is just no more workday sensor. If use add_holiday and I take out the remove_holiday it works fine.
Thatâs the problem, if you put holiday in the excludes list it excludes days like Presidents day which is a work day for me and most other people. If you remove holiday from the excludes list there are no holidays, not even days like Christmas or Thanksgiving even if you put them in a add_holiday list.
Your missing the point Iâm trying to make. If you put remove_holiday in the sensor setup you will no longer have a workday sensor at all. HA does not recognize that in the config and it does not create the sensor at all even though it says this is a valid part of the setup in the documentation for the workday sensor.
Is this bad documentation or is this something that is supposed to work but does not. If you look at the code for the workday sensor on Github it says that it added the function to remove workdays.
Iâm trying to show the setup that does not work even thought it follows the documentation exactly. And this is my real configuration that does not work.
I tried this workday sensor and found it lacking. I wrote my own sensor that uses a manually updated array of holidays. Yes, itâs manual, but it also only needs updated once a year with my next yearâs non-weekend holidays.
date_workday:
value_template: >
{% set ct = states('sensor.date_time') %}
{% set ct = as_timestamp(strptime(ct,'%Y-%m-%d, %H:%M')) %}
{% set date = states('sensor.date') %}
{% set holidays = {
'2021-01-01' : '1',
'2021-05-31' : '1',
'2021-07-05' : '1',
'2021-09-06' : '1',
'2021-11-25' : '1',
'2021-12-23' : '1',
'2021-12-24' : '1',
'2022-01-01' : '1'} %}
{% if 'Saturday' in ct | timestamp_custom("%A") %}
off
{% elif 'Sunday' in ct | timestamp_custom("%A") %}
off
{% elif holidays[date] == '1' %}
off
{% else %}
on
{% endif %}
I created some time ago, so Iâm sure it could be streamlined / improved - but, âIf it ainât broke, donât fix itâ
It can be optimized a bit, not sure if itâs for the better. Changing the dictionary into a list definitely makes âadding a dateâ easier in the future. As_timestamp can now analyze strings directly too.
date_workday:
value_template: >
{% set date = states('sensor.date') %}
{% set today = as_timestamp(date) | timestamp_custom("%A") %}
{% set holidays = [
'2021-01-01',
'2021-05-31',
'2021-07-05',
'2021-09-06',
'2021-11-25',
'2021-12-23',
'2021-12-24',
'2022-01-01',
] %}
{% if today == 'Saturday' %}
off
{% elif today == 'Sunday' %}
off
{% elif date in holidays %}
off
{% else %}
on
{% endif %}
Thank you for the help. I like this idea much better since I donât get off most âholidaysâ anyway, plus I need to edit it manually to add vacation days. My question is how to add it and how to use it.
I tried to add it to my configuration.yaml under the binary_sensor section and outside of the sensor section but no matter where I put it it does not seem to like it.
Also, to use it I assume I just check if date_workday is on or off, correct?
Seems adding remove_holidays with any valid non-holiday day causes the integration not to load and pukes an error:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 200, in _async_setup_platform
await asyncio.shield(task)
File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/workday/binary_sensor.py", line 105, in setup_platform
obj_holidays.pop(date)
File "/usr/local/lib/python3.8/site-packages/holidays/holiday_base.py", line 158, in pop
return dict.pop(self, self.__keytransform__(key))
KeyError: datetime.date(2021, 3, 18)
Testing on latest version of HA OS, i might not have enough skills to pinpoint the exact problem but seems to have issues out of the box. Havenât filed a bug yet or searched for an existing one.