Automation: Turn on Light From Motion just Once

I have a nightstand light that I am trying to automate by a detection from the motion sensor when I go up stairs (which triggers the motion sensor) and the time is 10 PM to 11:59 PM.

I have this working, but I’m looking to make sure it only triggers once. If I lay down and turn the light off and the cats come up the stairs within the time window, they trigger the motion sensor which turns the nightstand back on. Is there a way to trigger an automation just once a day? IE After I get in bed and turn it off manually I dont want the motion sensor to turn it on again until the next night.

That’s a common request (limit an automation to execute just once a day) and the simplest way to do it is with a Template Condition that checks the automation’s last_triggered attribute.

  • If the value is before the start of the day that means it has not executed today (and is allowed to execute its actions).
  • If the value is anytime today, that means it has already executed once (and is not allowed to execute again today).
condition:
  - condition: time
    after: '22:00:00'
    before: '23:59:00'
  - condition: template 
    value_template: >
      {{ this.attributes.last_triggered | default(as_datetime(0), true) < today_at() }}

You can add a template condition to check that the automation hasn’t triggered today:

condition:
  - condition: template
    value_template: "{{ this.attributes.last_triggered < today_at('00:00:00') }}"

Edit: I’m too late. See 123’s answer

For future reference, these two are equivalent:

today_at()
today_at('00:00:00')

For a new automation that has never been triggered, the value of its last_triggered attribute is null. Therefore the following template will attempt to compare null to a datetime object and result in an error.

{{ this.attributes.last_triggered < today_at() }}

The example I posted above guards against that situation by using the default filter to provide a datetime object whose value is older than today.

So does this look correct?

alias: Night Stand Motion after 10
description: ""
trigger:
  - type: motion
    platform: device
    device_id: d5e15ddcaf656aca06dbebab7f218b46
    entity_id: 0ed91c57150958b585f8588a7bfd449b
    domain: binary_sensor
condition:
  - condition: time
    after: "22:00:00"
    before: "23:59:00"
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
  - condition: template
    value_template: >-
      {{ this.attributes.last_triggered | default(as_datetime(0), true) <
      today_at() }}
action:
  - type: turn_on
    device_id: dc14f02b0016f3930127a4976cf09ac8
    entity_id: c7139d4316b4db9761d8bebc5b72fe18
    domain: switch
mode: single

Yes. However, do not try to test this automation by manually executing it or by testing the condition in the Automation Editor.

Any automation that references the trigger or this object in its conditions or actions can only be tested by actually triggering the automation.

Understood as it would update the last run time. Thank you for the help!

No, it would fail for the reason I explained above.