Roster Sensor

I work a 2 days swing shift 2 nights then 4 days off
so I want home assistant to know when Im working not working

after finding

this got me thinking

only rules are

  1. need to know the roster
  2. day 1 of the roster only needs tobe set once
  3. need to know the patten

download form here

just put my_shift.py into your python_scripts folder

must have

python_script:

in your configuration.yaml

Script arguments

key required type description
name: True string Name of the date (eg. StePhan)
shiftpatten: True string Type of Shift
firstdayshift: True string Date, in format DD/MM/YYYY day 1 of shift patten
patten: True string What the Patten DDNN = (doing 2 days then 2 nigths)

patten: is what shift patten are you doing
eg
DDNN = work 2 days then 2 nites

DDDDD = work 5 days in a row

firstdayshift: only has tobe full out once as it just uses maths to work out weather you on/off

shiftpatten patten
‘4x4x12’ 4 days ON x 4 days off x 12 hour days and it a 8 day cycle
‘4x2x12’ 4 days ON x 2 days off x 12 hour days and it a 6 day cycle
‘5x2x8’ 5 days ON x 2 days off x 8 hour days and it a 7 day cycle
‘6x1x8’ 6 days ON x 1 day off x 8 hour days and it a 7 day cycle

must be small x

I have tested the above shiftpatten if someone has a differance patten it should work

if you are a monday-friday worker try this workday Binary Sensor
as this look at the public hoildays in your country

so i work a 4x4x12

name: StePhan
shiftpatten: 4x4x12
patten: DDNN
firstdayshift: 20/10/2019

date must be in DD/MM/YYYY format

so well need to add a automation that fires once a day and when HA restarts

automation:
  - alias: set the shift patten on/off
    trigger:
      - platform: time
        at: '00:00:01'
      - platform: homeassistant
        event: start
    action:
      - service: python_script.my_shift
        data:
          name: StePhan
          shiftpatten: 4x4x12
          patten: DDNN
          firstdayshift: 20/10/2019

it will create a sensor

call sensor.stephan_4x4x12

which looks like this

image

and using lovelace secondary_info

image

  - entity: sensor.stephan_4x4x12
    secondary_info: >-
      Next Off: [[ {entity}.attributes.next_off_date ]] <br> Next start: [[
      {entity}.attributes.next_start_date ]]
    type: 'custom:secondaryinfo-entity-row'

here is one of my automation
I wake at 4am on my days

but need to read the attributes of this_day as this will say Day or Night or Off

so looking at the template

so

- id: 'Wake me up roster only morning'
  alias: Wake me up roster morning
  trigger:
  - at: '4:00:00'
    platform: time
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: sensor.stephan_4x4x12
      state: 'on'
    - condition: template
      value_template: >-
        {{states.sensor.stephan_4x4x12.attributes["this_day"] == 'Day'}}
  action:
  - data:
      entity_id: light.his_side
    service: light.turn_on
  - delay: '00:10:00'
  - data:
      entity_id: light.his_side
    service: light.turn_off      

logic look and feel right
just waiting for my next days on LOL

if you want you can add to the HACS

goto HACS setting and paste ‘https://github.com/MYLE-01/my_shift’ into the ADD CUSTOM REPOSTORY and make the category python_script

still reading up on how to add it the HACS

3 Likes

Thank god for Monday to Friday 9-5, and the workday sensor :slight_smile:

1 Like

yeap that’s the wife one

Hi,
nice work, although it didn’t work first go. The logs threw an error message about “none type” data on line 14 of the script. I know zero about python but have enough programming knowledge to figure out that the “holiday_start” and “holiday_end” data has to be declared in the automation. Example:

    action:
      - service: python_script.my_shift
        data:
          name: StePhan
          shiftpatten: 4x4x12
          patten: DDNN
          firstdayshift: 20/10/2019
          holiday_start: 25/12/2021
          holiday_end: 02/01/2022

After that, it works fine.

Cheers

Russ