LorDHaZarD / home-assistant-config

repo for collection of automations and helpers for home assistant
0 stars 0 forks source link

Time at work tracker #2

Open LorDHaZarD opened 3 years ago

LorDHaZarD commented 3 years ago

track how many hours per day a person is at work and time spent in transit between work and home

LorDHaZarD commented 3 years ago

https://community.home-assistant.io/t/track-hours-im-at-work/33651/4

or https://community.home-assistant.io/t/monitoring-working-hours-at-home-based-on-device-tracker/140388

sensor:
  - platform: history_stats
    name: Time at work (this week only)
    entity_id: device_tracker.me
    state: 'Work'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'
- alias: "Leave any zone"
  trigger:
  - platform: state
    entity_id: device_tracker.paul_all
    to: 'not_home'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1  
  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.leave_any_zone.attributes.last_triggered) | int > 120 }}'
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.commute_start_time
      value: '{{ as_timestamp(now()) }}'
  - service: input_text.set_value
    data_template:
      entity_id: input_text.commute_start_zone
      value: '{{ trigger.from_state.state }}'

- alias: "Enter any zone"
  trigger:
  - platform: state
    entity_id: device_tracker.paul_all
    from: 'not_home'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1  
  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.enter_any_zone.attributes.last_triggered) | int > 120 }}'
  - condition: template
    value_template: "{{ states('input_text.commute_start_zone') != states('device_tracker.paul_all') }}"
  action:
  - service: ifttt.trigger
    data_template:
      event: CommuteLog
      value1: '{{ states("input_text.commute_start_zone") }}'
      value2: '{{ states("device_tracker.paul_all") }}'
      value3: '{{ ((as_timestamp(now()) - (states("input_number.commute_start_time")|int))/60)|round }}'
  - service: script.sms_notify_with_images
    data_template:
      service: notify.sms_paul
      condition: "{{ is_state('input_boolean.notify_paul','on') }}"
      title: "Commute Logged"
      message: "Your commute from {{ states('input_text.commute_start_zone') }} to {{ states('device_tracker.paul_all') }} took {{ ((as_timestamp(now()) - (states('input_number.commute_start_time')|int))/60)|round }} minutes"