Working hours

Morning,
question, i would create a calender in where i can see my working hours based on a device_tracker.
does anybody thinks that this is possible?
it would be nice to show that in an new calender.
looking forward to get some tips

If you create a zone called “Work”, then something like this should work:

trigger:
  - platform: state
    entity_id:
      - device_tracker.me
    from: Work
condition: []
action:
  - service: calendar.create_event
    target:
      entity_id: calendar.work
    data:
      summary: At work
      description: At work
      start_date_time: "{{ trigger.from_state.last_changed }}"
      end_date_time: "{{ trigger.to_state.last_changed }}"
      location: Work

Maybe I’d rather use a person entity, but you stated device tracker. a person entity should work too.

3 Likes

Do you specifically want a Calendar or are you just interested in tracking hours?

Hi Edwin,
thanks for the sample.
i want to see in the calendar, on what time leaving home and at what time i’am back.
that only for monday untill friday.
that by using a device tracker.

The monday till friday bit can be put in the conditions. But as this registers when you arrive at work and when you leave, I do not see why it is needed. If you do not go to work it will not do anything (and if you do, wouldn’t you want to know?

The leaving home bit is much harder. You never know in Home Assistant why you leave home. And what if you are at a friends place, and are then called to go to work from there? Or need to go to the dentist during working hours? Or if you go to a shop to buy some food straight from work?

You could save the time you enter and leave home, and when you are at work. If the time at work is between leaving and returning, then create the event. The principles are the same as in the example. But I think you will have many cases where it won’t work as expected. So what I gave you is just work, but truthful in all these cases.

1 Like

Clear answer.
The problem is that i do not have a fixed work zone.
Because of the fact that i drive around the country.

Then you asked the wrong question. Because then you want to know all the times you were not home on weekdays. This will inevitably lead to problems when you leave home on weekdays and return in the weekend, or vice versa.

Create a binary sensor to see of you are home or not. (this is needed to filter out other zones you might visit while you are gone.) Then replace the devicetracker instance in my example with the binary sensor. And add a condition for the weekdays.

thanks again.
first i will try to add the binary sensor but don’t know how to do that.
sorry…newbie

You can create a template (binary) sensor in the home assistant settings, under helpers.

Place this in the field for the state template, be sure to replace the device_tracker entity by the one you need.

This tells you if you are home:

{{ is_state('device_tracker.me','home') }}

Honestly, I can’t see something here, that would work without issues. If you use your home (arriving/leaving) that would not work on holidays for example. If you’d pin it to your work, that can’t work if you don’t have an office (in the conventional sense :laughing:). If you pin it to the car, you can’t drive to the supermarket without being “at work”.

In this case, I’d advise to look for something, that you really only use, if you’re working. Eg. a work phone, a login to a company computer or network, something like that. It would make much more sense, to work with something like that.

And if you don’t have anything like that, I’d rather think about a BT beacon, that you hang on your working tools. A BT beacon connected to your saw…anything would be better, than relying on “home” or “not_home”. :slight_smile:

But it’s your question, let us know more about how you work and what you do, so we can think about better solutions. :slight_smile:

This will help me out to test some several things.
Thanks again

Some explanation from my side.
i’am working as a service engineer with customers around the country, in this case The Netherlands.
an as you suggested, i am using a “Work Phone” only at the time whem i am working.
so maybe i can figure out something with this phone.

If there isn’t a definitive source of truth, your next best bet is probably to use the Bayesian binary sensor to combine values from multiple sensors to determine when you are most probably “at work”.

OK, that sounds good, as you can use that as relatively unique possibilty to get your work status.

When exactly do you use that work phone? Is it on before you “go to work” or do you turn it on when you get to the car? Depending on how you exactly use it, it should be possible to be relatively exact in determing the times you work.

If so, you could go with something like phone == on while not in “home_zone”.

Please describe exactly, how you use that phone (regarding the work time :laughing: ), maybe some other ideas come up. Or just fiddle around and let us know what you came up with! :slight_smile:

OK.
Created an Work calender.
Create an binary_sensor
The phone is always ON and in the WIFI network.
When leaving home the phone is not in the WIFI network anymore.
So, when leaving home the time needs to start in the (work) calender.
Then, when arriving home and the phone is in the WIFI network again it should shown that in the calender. That means that there should be an time leaving untill arriving displayed in the calender.
i 'll tryed this (see below)

alias: werk
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.home_or_not_home
    from: Work
condition: []
action:
  - service: calendar.create_event
    target:
      entity_id: calendar.work
    data:
      summary: At work
      description: At work
      start_date_time: "{{ trigger.from_state.last_changed }}"
      end_date_time: "{{ trigger.to_state.last_changed }}"
      location: Work

but when testing this automation i get an error

Executed: June 10, 2024 at 7:27:55 PM

Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘from_state’

i thought it would be a bit simpeler but…?
Any tips are very welcome

EDIT.
Also tryed this one…

alias: Start working time
description: ""
trigger:
  - platform: zone
    entity_id: device_tracker.os_hev_19_21_location
    zone: zone.home
    event: leave
condition: []
action:
  - service: calendar.create_event
    metadata: {}
    data:
      summary: start
      description: Start working time
      start_date_time: "2024-06-10 00:00:00"
      end_date_time: "2024-06-11 00:00:00"
      start_date: "2024-06-10"
      end_date: "2024-06-11"
    target:
      entity_id: calendar.work
mode: single

Based on the car i'am driving
But then the error is something like this....

**Executed: June 10, 2024 at 7:57:21 PM**
**Error: must contain at most one of start_date, start_date_time, in.**

You cannot test automations that rely on the trigger variable by using the “Run” button in the Automation editor.

However, more importantly, the trigger in the first automation will never fire. Binary sensor only have “on”/“off”/“unavailable” as valid states… the “from” state will never be “Work”.

The error in the second automation is due to the fact that you have provided too many configuration variables. The variables start_date and end_date are only for all-day events and cannot be used at the same time as start_date_time and end_date_time.

In addition to the answer above, the trigger should fire when you come home, because that is when the end of the away period is known.

The reason for the binary sensor is that there can be many states the device tracker has when you are not home (if you define zones). The binary sensor was suggested to make sure that does not affect the automation.