Need help for overtime calculation sensor

I’m trying to create a sensor which calculates how many overtime I have worked. I’m using the history statistics sensor for this in combination with a sensor template, input boolean and zone:

Input boolean:

input_boolean:
  overtime:
    name: Overtime
    icon: mdi:factory 

Zone:

zone:
  - name: Work
    latitude: !secret zone_work_latitude
    longitude: !secret zone_work_longitude
    radius: 250
    icon: mdi:briefcase 

History statistics:

  - platform: history_stats
    name: Overtime
    entity_id: input_boolean.overtime
    state: 'on'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'  

Sensor template:

      overtime_week:
        value_template: '{{ states.sensor.overtime.attributes.value }}' 

Automation:

- alias: Work Log - Overtime [Begin]
  trigger:
    platform: time
    at: '17:00:00'
  condition:  
      condition: state
      entity_id: device_tracker.bob_s8
      state: 'Work'
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.overtime    
    
- alias: Work Log - Overtime [End]
  trigger:
    platform: zone
    entity_id: device_tracker.bob_s8
    zone: zone.work
    event: leave
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.overtime

Frontend:
image

The problem is that at our company overtime is only allowed after 30 minutes. Regular office hours start at 8am and end at 5pm. This means that if I leave at 5.20, no overtime must be registered but if I leave at 5.40, 40 minutes of overtime must be registered. In my current setup, overtime starts at 5pm regardless of the end time. What I’m trying to achieve is that at 5.30pm the ticker start ticking but also immediately adds 30 minutes of overtime.

Does anyone know how this can be done? As far as I know, there is no such option like set_value for a regular sensor. If this option was available, I could simply create an automation which turns the input_boolean to ‘on’ and adds 30 minutes to the sensor value.

I hope someone can help me out or point me in the right direction. I know it can be done by using a ‘dummy’ mqtt sensor but I don’t want to complicate thing unless it’s absolutely necessary. Also, the sensor value would probably reset on reboot and I must be able to calculate the time for the entire week.

Still haven’t figured this one out. No one can help?

So each day you have to have over 30 minutes already logged before turning on the switch?

Correct, the switch should be turned on at 17:30 and instantly add 30 minutes.

Keep these:

Input boolean:

input_boolean:
  overtime:
    name: Overtime
    icon: mdi:factory 

Zone:

zone:
  - name: Work
    latitude: !secret zone_work_latitude
    longitude: !secret zone_work_longitude
    radius: 250
    icon: mdi:briefcase 

automations:

- alias: Work Log - Overtime [End]
  trigger:
    platform: zone
    entity_id: device_tracker.bob_s8
    zone: zone.work
    event: leave
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.overtime

Change these:

History statistics:

  - platform: history_stats
    name: Overtime Minutes
    entity_id: input_boolean.overtime
    state: 'on'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'  

automations:

- alias: Work Log - Overtime [Begin]
  trigger:
    platform: time
    at: '17:30:00'
  condition:  
      condition: state
      entity_id: device_tracker.bob_s8
      state: 'Work'
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.overtime

template:

      overtime_week:
        value_template: '{{ states.sensor.overtime_minutes.attributes.value + states.sensor.overtime_count.attributes.value * 30 }}' 

Add this:

History statistics:

  - platform: history_stats
    name: Overtime Count
    entity_id: input_boolean.overtime
    state: 'on'
    type: count
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'  

So what this does is counts the number of on’s per week. And it counts the minutes from 5:30 on. For each count we have, there should be 30 minutes, meaning 30*count. So add the minutes from 5:30 on + 30 minutes * count.

@petro first of all: thanks a lot for helping me out, much appreciated!

If I understand correct, your suggestion counts the times the input_boolean went to “on” state. But how is it going to determine if I went home at 7:00PM, 8:00PM, 9:30PM etc? That obviously makes a lot of difference for the total time (every minute after 17:30 should be accounted for) . Or am I missing something?

Yes, it counts the number of times you are in the office from 5:30 pm til N:NN pm. The other sensor tracks the time from 5:30pm to N:NN pm.

So the count * 30 + sensor time tracker = total time

You’re right. Brilliant, thanks!

The value_template needs work, it now just duplicates the calculated value x 30, even when I add quotes:

Looks like it has trouble adding an integer to a time value. After adding the INT, they work independent of each other:

{{ (states.sensor.overtime_minutes.attributes.value) }} gives 1h 0m

{{ (states.sensor.overtime_count.attributes.value | int *30) }} gives 0

Combined gives nothing (also no error).

{{ (states.sensor.overtime_minutes.attributes.value) + (states.sensor.overtime_count.attributes.value | int * 30) }}