2 Automation at same time

When automation turn on and turn off running at same time,
I dont wanna turn off the switch.

but the automation turn off result always be true
does anyone know how to fix it

      value_template: >-
        {% set last_triggered_on = state_attr('automation.turn_on_plug_when_event_starts', 'last_triggered') %}
        {% set last_triggered_off = state_attr('automation.turn_off_plug_when_the_event_ends_if_no_immediate_next_event', 'last_triggered') %}
        {% if last_triggered_on and last_triggered_off %}
          {% set last_triggered_on_ts = as_timestamp(last_triggered_on) %}
          {% set last_triggered_off_ts = as_timestamp(last_triggered_off) %}
          {{ (now() - last_triggered_on_ts).total_seconds() > 1 and (now() - last_triggered_off_ts).total_seconds() > 1 }}
        {% else %}
          true
        {% endif %}

The last_triggered shows when the action was last executed, not when the last trigger was activated. If your condition blocks the action, the last_triggered is not updated.

Perhaps you should check the last_changed state of the plug instead?

Show the whole automation YAML and we might be able to help better.

thanks for reply.

- id: turn_on_plug_when_event_starts
  alias: Turn on the plug when the event starts
  trigger:
    - platform: calendar
      event: start
      entity_id: calendar.practice944x_gmail_com
  condition:
    - condition: template
      value_template: "{{ 'ZZ' in trigger.calendar_event.summary }}"
  action:
    - service: switch.turn_on
      target:
        device_id: 3752fb00947ffb4dcefe4b8919663710
        entity_id: switch.f0436afedb6cadb1d8f991b75bff50b
  mode: single

- id: turn_off_plug_when_the_event_ends_if_no_immediate_next_event
  alias: Turn off the plug when the event ends if no immediate next event
  trigger:
    - platform: calendar
      entity_id: calendar.practice944x_gmail_com
      event: end
  condition:
    - condition: template
      value_template: "{{ 'ZZ' in trigger.calendar_event.summary }}"
    - condition: template
      value_template: >-
        {% set now_ts = as_timestamp(now()) %}
        {% set last_triggered_on = state_attr('automation.turn_on_plug_when_event_starts', 'last_triggered') %}
        {% set last_triggered_off = state_attr('automation.turn_off_plug_when_the_event_ends_if_no_immediate_next_event', 'last_triggered') %}
        {% set last_triggered_on_ts = 0 if last_triggered_on is none else as_timestamp(last_triggered_on) %}
        {% set last_triggered_off_ts = 0 if last_triggered_off is none else as_timestamp(last_triggered_off) %}
        {% if last_triggered_on_ts == 0 or last_triggered_off_ts == 0 %}
          false
        {% else %}
          {{ (now_ts - last_triggered_on_ts) > 1 and (now_ts - last_triggered_off_ts) > 1 }}
        {% endif %}
  action:
    - delay: "00:03:00"
    - service: switch.turn_off
      target:
        device_id: 3752fb00947ffb4dcefe4b8919663710
        entity_id: switch.f0436afedb6cadb1d8f991b75bff50b
  mode: single

how to use last_changed instead