How to check when script was last triggered

How to check when script was last triggered? I’m trying to use with motion sensor to check when last called. The motion sensor is connected to the alexa app and it is calling the script.

That information can be obtained using a template: {{state_attr('script.XXXX','last_triggered')}}

If you need help incorporating templating into your automation you will need to post the automation.

Would it be possible to have a condition checking how long it had been?

motion_bedroom:
  alias: Motion bedroom
  sequence:
  - choose:
    - conditions:
      - condition: time
        after: '17:00:00'
        before: '23:00:00'
        weekday:
        - mon
        - wed
        - tue
        - thu
        - fri
        - sat
        - sun
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.bedroom_on
        metadata: {}
    default: []
  mode: single
  icon: mdi:motion-sensor

Yes, you can use templates to compare the current time to the last_triggered time. For example, if you wanted to allow a script to be executed only if it hadn’t been triggered for at least 5 minutes you would use:

condition: template
value_template: >
  {{now() >= state_attr('script.XXX','last_triggered')|as_local + 
  timedelta(minutes=5) }}
1 Like

I tried it but it didn’t work. Am I doing something wrong?

alias: Motion bedroom
sequence:
  - choose:
      - conditions:
          - condition: time
            after: '18:00:00'
            before: '23:00:00'
            weekday:
              - mon
              - wed
              - tue
              - thu
              - fri
              - sat
              - sun
          - condition: template
            value_template: >
              {{now() >=
              state_attr('script.motion_bedroom','last_triggered')|as_local + 
              timedelta(minutes=1) }}
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.bedroom_on
            metadata: {}
    default: []
mode: single
icon: mdi:motion-sensor

We’re going to need a little more information…

What do you mean when you say you tried it?
What happened?
Is it between 17:00 and 23:00 where you are in the world?
What does the script trace/debug say?

I ran the script with and without the template. Without it worked. When I added the template it broke. Yes it is between the times however it is still not working. This is my debug:

Executed: 5 April 2022, 21:08:05
Result:
result: false
conditions/0
Executed: 5 April 2022, 21:08:05
Result:
after:
  __type: <class 'datetime.time'>
  isoformat: '18:00:00'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '21:08:05.016353'
before:
  __type: <class 'datetime.time'>
  isoformat: '23:00:00'
weekday:
  - mon
  - wed
  - tue
  - thu
  - fri
  - sat
  - sun
now_weekday: tue
result: true
conditions/1
Executed: 5 April 2022, 21:08:05
Result:
result: false
entities:
  - script.motion_bedroom

This is never going to do anything. last_triggered is set to the current date and time when the script starts running. Your script basically says this:

IF time is between 18:00:00 and 23:00:00 AND it's been 1 minute since this started
  Turn on a scene

It’s not possible for that to do anything unless it somehow took a full minute to check the time.

Do you want the script to wait 1 minute before turning on the scene after the motion sensor detects motion? If so what you’re looking for is a delay action like this:

alias: Motion bedroom
sequence:
  - choose:
      - conditions:
          - condition: time
            after: '18:00:00'
            before: '23:00:00'
        sequence:
          - delay: '00:01'
          - service: scene.turn_on
            target:
              entity_id: scene.bedroom_on
            metadata: {}
    default: []
mode: single
icon: mdi:motion-sensor

Note: I dropped the weekday bit because every day is the default, you only need to list if if you want it to pass on a subset of the days of the week

If this isn’t what you want then perhaps you can tell us what you’re trying to do? Would be easier to help

1 Like

Is it possible to do if the script hasn’t been run for at least 1 minute then run script?

Never mind I figured it out. I created a script which turns on a scene (scenes don’t have the last triggered attribute) I then made another script with the timings and then used this.

condition: template
value_template: >
  {{now() >= state_attr('script.XXX','last_triggered')|as_local + 
  timedelta(minutes=4) }}

This turns on the light on script if it hasn’t been 4 minutes.

Thanks @Didgeridrew @CentralCommand