Count days off, working days, holidays in future

Hi,
I would like to create a sensor that would count days off and the number of working days.

Anyone have an idea how to do this?
The Workday service only shows whether the day is working or not

You mean count them as they go past, rather than project them into the future? (readers please note the topic title was edited to include “in future” after I asked this). Could perhaps do this with a history stats sensor watching the workday sensor:

or with a Counter helper and an automation:

trigger:
  - platform: state
    entity_id: binary_sensor.workday_sensor
    from: 'on'
    to: 'off'
condition:
  - condition: time
    before: "00:02:00"
action:
  - service: counter.increment
    entity_id: counter.days_off

The Time condition is there to prevent accidental increments on HA restarts etc. My workday sensor takes up to about 30s into the day to update, so two minutes should be fine.

I mean counting how many working days, days off, etc. there are in a given year. So generally in the future.

Write a script (or a suitably-timed automation with the script in the action) that calls the workday.check_date service for each date in turn within the range you care about, and count the false responses. Something like this, which returns 112 non-work days in 2024 off my England-configured sensor into the input_number you must first set up:

alias: Holiday counter
sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.days_off
    data:
      value: 0
  - repeat:
      for_each: >-
        {% set s=("%d-01-01T12"%now().year)|as_timestamp|int %}
        {{ range(s,s+31622400,86400)|map('timestamp_custom','%Y-%m-%d')|select('match','%s'%now().year)|list }}
      sequence:
        - service: workday.check_date
          target:
            entity_id: binary_sensor.workday_sensor
          data:
            check_date: "{{ repeat.item }}"
          response_variable: check_date
        - if:
            - condition: template
              value_template: "{{ not(check_date['binary_sensor.workday_sensor']['workday']) }}"
          then:
            - service: input_number.increment
              target:
                entity_id: input_number.days_off
              data: {}
mode: single

There’s probably a tidier way to generate the list of date strings in my for_each statement, but I’m pretty happy with it :sunglasses:.

2 Likes