After having issues getting the built-in ‘Workday Sensor’ to do what I want, I recoded it using a template, and that works as I need. I put the holidays that I don’t have to work in an array and check to see if the date (with an optional offset) is either in that array, or on a weekend:
- platform: template
sensors:
workday_today:
friendly_name: 'Workday Today'
value_template: >
{% set offset = 0 %}
{% set holidays = [
'2022-01-17',
'2022-05-30',
'2022-07-04',
'2022-09-05',
'2022-11-24',
'2022-11-25',
'2022-12-26'] %}
{{ not((now() + timedelta(days=offset)).isoweekday() in [6, 7] or (now() + timedelta(days=offset)).date()|string in holidays)}}
This works fine, but I need to duplicate the code for any other offset (I have 3: for workday today, tomorrow and 2 days ahead) which is inefficient:
- platform: template
sensors:
workday_tomorrow:
friendly_name: 'Workday Tomorrow'
value_template: >
{% set offset = 1 %}
{% set holidays = [
'2022-01-17',
'2022-05-30',
'2022-07-04',
'2022-09-05',
'2022-11-24',
'2022-11-25',
'2022-12-26'] %}
{{ not((now() + timedelta(days=offset)).isoweekday() in [6, 7] or (now() + timedelta(days=offset)).date()|string in holidays)}}
- platform: template
sensors:
workday_twodays:
friendly_name: 'Workday Two Days'
value_template: >
{% set offset = 2 %}
{% set holidays = [
'2022-01-17',
'2022-05-30',
'2022-07-04',
'2022-09-05',
'2022-11-24',
'2022-11-25',
'2022-12-26'] %}
{{ not((now() + timedelta(days=offset)).isoweekday() in [6, 7] or (now() + timedelta(days=offset)).date()|string in holidays)}}
Is there a way to declare an array (preferably in it’s own yaml file) that I can use in my template sensors, something like this:
holiday_list:
- '2022-01-17'
- '2022-05-30'
- '2022-07-04'
- '2022-09-05'
- '2022-11-24'
- '2022-11-25'
- '2022-12-26'
My Yaml skills aren’t very strong yet, I can usually fudge my way through something that has been created, but not very good at writing from scratch. I’m a lot better with templates than with Yaml.
I’d also like to put all of the logic into a separate ‘function’ (for want of a better word) that I can just call for each sensor, passing just the offset value needed if that’s possible.
Any assistance to point me in the correct direction would be greatly appreciated.