Google calendar : create event and using same variable for start and end date

Trying to do this but it seems a bit wasteful to have to declare and set the local variable again, but it seems i’m not allowed to use ‘a’ again.

service: google.create_event
data:
  start_date_time: "{% set a=now()|string() %} {{ a[0:19] }}"
  end_date_time: "{% set a=now()|string() %} {{ a[0:16] }}{{a[16:17]}}{{a[17:19]|int + 1}}"
  summary: Succesful ParkingEye Exemption using Life360
target:
  entity_id: calendar.home_assistant

You need to create the variable at the correct scope level for your purposes.

Variable Scope Documentation

However, in this case you’d probably be better off foregoing the variable and just using datetime methods instead of string splicing:

service: google.create_event
data:
  start_date_time: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
  end_date_time: "{{ (now()+timedelta(seconds=1)).strftime('%Y-%m-%d %H:%M:%S') }}"
  summary: Succesful ParkingEye Exemption using Life360
target:
  entity_id: calendar.home_assistant

Cool, that certainly works. I’m guessing this would be better because it’s more optimised?

Ideally i wanted to use the same value rather then using now() again as the time will differ ever so slightly (but probably not worth bothering about).

I tried to find the last_updated state of the automation but i couldn’t find out how to get that. I was thinking trigger.automation.to_state.last_updated but any permutation i put in developer tools doesn’t work for me.

Anyway thanks!

Then you need to set the variable at a wider scope.

...
action:
  - variables:
      current_time: "{{ now() }}"
  - service: google.create_event
    data:
      start_date_time: "{{ current_time.strftime('%Y-%m-%d %H:%M:%S') }}"
      end_date_time: "{{ (current_time + timedelta(seconds=1)).strftime('%Y-%m-%d %H:%M:%S') }}"
      summary: Succesful ParkingEye Exemption using Life360
    target:
      entity_id: calendar.home_assistant
....

While automations have access to the self-referencing variable this, it won’t give you what you want either. You would have to use the full

state_attr('automation.REAL_ID_HERE','last_triggered')

in the Action block as above for current_time. Defining it as a trigger variable or script variable will not work because last_triggered is not updated until the conditions are passed.

The trigger variable is undefined outside a running automation.