Log events on calendar via automations

Hello, I am trying to create an automation that tracks my time at the gym on a Google Calendar. I have the calendar integration set up and have been able to create events where the start time and end time are more static but this automation is not working for me.

The idea is that it starts when I arrive at the gym, waits till I leave, ensures that it was more than 5 mins (the gym is at a shopping center I drive by there often and that unfortunately doesn’t count as a workout) and then is supposed to create a calendar event with the start time being when I arrived and the end time being when I left. For some reason it’s not working. Here’s the YAML:

alias: Gym Tracker
description: >-
Track time spent at the gym and create a calendar event if more than 5
minutes.
trigger:

  • platform: zone
    entity_id: person.john
    zone: zone.gym
    event: enter
    action:
  • wait_for_trigger:
    • platform: zone
      entity_id: person.john
      zone: zone.gym
      event: leave
      timeout:
      hours: 2
      minutes: 0
      seconds: 0
      milliseconds: 0
      continue_on_timeout: false
  • variables:
    start_time: “{{ trigger.event.time }}”
    end_time: “{{ as_timestamp(now()) }}”
  • condition: template
    value_template: “{{ (end_time - as_timestamp(start_time)) >= 300 }}”
  • service: calendar.create_event
    data:
    start_date_time: “{{ start_time }}”
    end_date_time: “{{ end_time }}”
    summary: Gym Session
    description: Tracked gym session.
    target:
    entity_id: calendar.personal
    mode: single

And here’s the latest trace:

I also want one that tracks my sleep. I use a CPAP and it’s plugged into a smart plug with power monitoring. I’m assuming I can use the same logic for this automation, but I wanted to ask here if there is a better approach since I’m not really sure if it’s a bad thing to have that automation technically “running” the entire time I’m asleep, waiting for the power demand to drop to log my wake up.

Thanks!

1 Like

Check the automation’s trace and report the value produced by the following template. I don’t believe it produces a time value.

start_time: “{{ trigger.event.time }}”

Here’s what is returned by a Zone Trigger.

Yeah, here’s the trace log. It’s not returning a start time. Any idea how to get the start time?


      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2024-02-23T12:07:32.972240+00:00",
          "changed_variables": {
            "start_time": "",
            "end_time": 1708690052.973299
          }
        }
      ],
      "action/2": [
        {
          "path": "action/2",
          "timestamp": "2024-02-23T12:07:32.973629+00:00",
          "result": {
            "result": false
          }
        }
      ]
    },

Not exactly sure what starting time you’re after but my guess is it may be in trigger.from_state. Post the entire trace and help me understand what you mean by start time.

As shown in the documentation that Taras linked, trigger.event.time is not a thing. If you want to know when the trigger fired:

Define your own variable and attach it to the trigger.
trigger:
  - platform: zone
    entity_id: person.john
    zone: zone.gym
    event: enter
    variables:
      start_time:  "{{ now() }}"
condition: []
action:
  - wait_for_trigger:
      - platform: zone
        entity_id: person.john
        zone: zone.gym
        event: leave
    timeout: "02:00:00"
    continue_on_timeout: false
  - variables:
      end_time: "{{ now() }}"
  - condition: template
    value_template: "{{ end_time - start_time >= timedelta(minutes=5) }}"
  - service: calendar.create_event
    data:
      start_date_time: "{{ start_time }}"
      end_date_time: "{{ end_time }}"
      summary: Gym Session
      description: Tracked gym session.
    target:  
      entity_id: calendar.personal
mode: single

But I think you should be able to accomplish this without a multi-hour wait…

trigger:
  - platform: zone
    entity_id: person.john
    zone: zone.gym
    event: leave
    variables:
      start_time:  "{{ trigger.from_state.last_changed }}"
      end_time:  "{{ trigger.to_state.last_changed }}"
condition:
  - condition: template
    value_template: "{{ end_time - start_time >= timedelta(minutes=5) }}"
action:
  - service: calendar.create_event
    data:
      start_date_time: "{{ start_time }}"
      end_date_time: "{{ end_time }}"
      summary: Gym Session
      description: Tracked gym session.
    target:  
      entity_id: calendar.personal
mode: single