Single run automation being retriggered

Hey there,

I have a fairly basic automation configured:

On the Rising Edge of movement detected entity it checks if the sun is down and if a light is off (to avoid triggering the automation if anyone had turned the light manually before). If so it turns on a light and waits for the Falling Edge of the same movement sector. Then it starts a 30s delay and finally turns off the light.

The automation is configured as “Single” meaning that it should not retrigger while it is running (up to the moment that the light gets turned off).

However, this seems to be happening.I can see on a trace that one execution started at 9:07:13 and ended at 9:07:52:

But then the next trace shows me it retriggered at 9:07:36, meaning that the previous instance was still running:

Even clearer here. One automation takes 40 sconds to run, finishing at 10:17:43 and the second trigger clearly overlaps it, starting at 10:17:22.


Any tips?

Thanks!

Please repost everything you have posted in yaml/json.

The only YAML is the automation, really…

alias: Lights - Deck Light On on Movement
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.garden_front
    from: "off"
    to: "on"
conditions:
  - condition: device
    type: is_off
    device_id: a3a72e7f3230c5578c08a9915a651e81
    entity_id: fc2b5aee1f9b5ad179e3fb2cc12bab88
    domain: light
  - condition: sun
    after: sunset
    after_offset: "01:00:00"
actions:
  - type: turn_on
    device_id: a3a72e7f3230c5578c08a9915a651e81
    entity_id: fc2b5aee1f9b5ad179e3fb2cc12bab88
    domain: light
  - wait_for_trigger:
      - trigger: state
        entity_id:
          - binary_sensor.garden_front
        from: "on"
        to: "off"
    continue_on_timeout: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - type: turn_off
    device_id: a3a72e7f3230c5578c08a9915a651e81
    entity_id: fc2b5aee1f9b5ad179e3fb2cc12bab88
    domain: light
mode: single

Ah, and here is the trace for one trigger, followed by the trigger which overlapped it.

First trigger

{
  "trace": {
    "last_step": "action/3",
    "run_id": "fc10df22e4235eb6120f0632d3c1597a",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2025-10-15T20:17:03.871828+00:00",
      "finish": "2025-10-15T20:17:43.887726+00:00"
    },
    "domain": "automation",
    "item_id": "1760551339871",
    "trigger": "state of binary_sensor.garden_front",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2025-10-15T20:17:03.871906+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.lights_deck_light_on_on_movement",
              "state": "on",
              "attributes": {
                "id": "1760551339871",
                "last_triggered": "2025-10-15T19:07:13.004303+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Lights - Deck Light On on Movement"
              },
              "last_changed": "2025-10-15T20:14:41.602080+00:00",
              "last_reported": "2025-10-15T20:14:41.602080+00:00",
              "last_updated": "2025-10-15T20:14:41.602080+00:00",
              "context": {
                "id": "01K7MR5DE2Y2E6QN3DSEY6VX0M",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "binary_sensor.garden_front",
              "from_state": {
                "entity_id": "binary_sensor.garden_front",
                "state": "off",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Garden Front"
                },
                "last_changed": "2025-10-15T19:07:52.975656+00:00",
                "last_reported": "2025-10-15T19:07:52.975656+00:00",
                "last_updated": "2025-10-15T19:07:52.975656+00:00",
                "context": {
                  "id": "01K7MMB2RF5RBWDEF9BN7NC907",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "binary_sensor.garden_front",
                "state": "on",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Garden Front"
                },
                "last_changed": "2025-10-15T20:17:03.870375+00:00",
                "last_reported": "2025-10-15T20:17:03.870375+00:00",
                "last_updated": "2025-10-15T20:17:03.870375+00:00",
                "context": {
                  "id": "01K7MR9RBY7P2CNMBE8XZVQH8Q",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of binary_sensor.garden_front"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2025-10-15T20:17:03.871937+00:00",
          "result": {
            "result": true
          }
        },
        {
          "path": "condition/0",
          "timestamp": "2025-10-15T20:17:03.871954+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "condition/0/entity_id/0": [
        {
          "path": "condition/0/entity_id/0",
          "timestamp": "2025-10-15T20:17:03.871976+00:00",
          "result": {
            "result": true,
            "state": "off",
            "wanted_state": "off"
          }
        }
      ],
      "condition/1": [
        {
          "path": "condition/1",
          "timestamp": "2025-10-15T20:17:03.872035+00:00",
          "result": {
            "wanted_time_after": "2025-10-15T17:51:51.269662+00:00",
            "result": true
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2025-10-15T20:17:03.872828+00:00",
          "changed_variables": {
            "context": {
              "id": "01K7MR9RBZMYZ99WZ05BZGSXV9",
              "parent_id": "01K7MR9RBY7P2CNMBE8XZVQH8Q",
              "user_id": null
            }
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2025-10-15T20:17:03.876620+00:00",
          "changed_variables": {
            "wait": {
              "remaining": null,
              "completed": true,
              "trigger": {
                "id": "0",
                "idx": "0",
                "alias": null,
                "platform": "state",
                "entity_id": "binary_sensor.garden_front",
                "from_state": {
                  "entity_id": "binary_sensor.garden_front",
                  "state": "on",
                  "attributes": {
                    "device_class": "motion",
                    "friendly_name": "Garden Front"
                  },
                  "last_changed": "2025-10-15T20:17:03.870375+00:00",
                  "last_reported": "2025-10-15T20:17:03.870375+00:00",
                  "last_updated": "2025-10-15T20:17:03.870375+00:00",
                  "context": {
                    "id": "01K7MR9RBY7P2CNMBE8XZVQH8Q",
                    "parent_id": null,
                    "user_id": null
                  }
                },
                "to_state": {
                  "entity_id": "binary_sensor.garden_front",
                  "state": "off",
                  "attributes": {
                    "device_class": "motion",
                    "friendly_name": "Garden Front"
                  },
                  "last_changed": "2025-10-15T20:17:13.878510+00:00",
                  "last_reported": "2025-10-15T20:17:13.878510+00:00",
                  "last_updated": "2025-10-15T20:17:13.878510+00:00",
                  "context": {
                    "id": "01K7MRA24PVM1BREEX98T084WR",
                    "parent_id": null,
                    "user_id": null
                  }
                },
                "for": null,
                "attribute": null,
                "description": "state of binary_sensor.garden_front"
              }
            }
          },
          "result": {
            "wait": {
              "remaining": null,
              "completed": true,
              "trigger": {
                "id": "0",
                "idx": "0",
                "alias": null,
                "platform": "state",
                "entity_id": "binary_sensor.garden_front",
                "from_state": {
                  "entity_id": "binary_sensor.garden_front",
                  "state": "on",
                  "attributes": {
                    "device_class": "motion",
                    "friendly_name": "Garden Front"
                  },
                  "last_changed": "2025-10-15T20:17:03.870375+00:00",
                  "last_reported": "2025-10-15T20:17:03.870375+00:00",
                  "last_updated": "2025-10-15T20:17:03.870375+00:00",
                  "context": {
                    "id": "01K7MR9RBY7P2CNMBE8XZVQH8Q",
                    "parent_id": null,
                    "user_id": null
                  }
                },
                "to_state": {
                  "entity_id": "binary_sensor.garden_front",
                  "state": "off",
                  "attributes": {
                    "device_class": "motion",
                    "friendly_name": "Garden Front"
                  },
                  "last_changed": "2025-10-15T20:17:13.878510+00:00",
                  "last_reported": "2025-10-15T20:17:13.878510+00:00",
                  "last_updated": "2025-10-15T20:17:13.878510+00:00",
                  "context": {
                    "id": "01K7MRA24PVM1BREEX98T084WR",
                    "parent_id": null,
                    "user_id": null
                  }
                },
                "for": null,
                "attribute": null,
                "description": "state of binary_sensor.garden_front"
              }
            }
          }
        }
      ],
      "action/2": [
        {
          "path": "action/2",
          "timestamp": "2025-10-15T20:17:13.880587+00:00",
          "result": {
            "delay": 30,
            "done": true
          }
        }
      ],
      "action/3": [
        {
          "path": "action/3",
          "timestamp": "2025-10-15T20:17:43.884081+00:00"
        }
      ]
    },
    "config": {
      "id": "1760551339871",
      "alias": "Lights - Deck Light On on Movement",
      "description": "",
      "triggers": [
        {
          "trigger": "state",
          "entity_id": [
            "binary_sensor.garden_front"
          ],
          "from": "off",
          "to": "on"
        }
      ],
      "conditions": [
        {
          "condition": "device",
          "type": "is_off",
          "device_id": "a3a72e7f3230c5578c08a9915a651e81",
          "entity_id": "fc2b5aee1f9b5ad179e3fb2cc12bab88",
          "domain": "light"
        },
        {
          "condition": "sun",
          "after": "sunset",
          "after_offset": "01:00:00"
        }
      ],
      "actions": [
        {
          "type": "turn_on",
          "device_id": "a3a72e7f3230c5578c08a9915a651e81",
          "entity_id": "fc2b5aee1f9b5ad179e3fb2cc12bab88",
          "domain": "light"
        },
        {
          "wait_for_trigger": [
            {
              "trigger": "state",
              "entity_id": [
                "binary_sensor.garden_front"
              ],
              "from": "on",
              "to": "off"
            }
          ],
          "continue_on_timeout": true
        },
        {
          "delay": {
            "hours": 0,
            "minutes": 0,
            "seconds": 30,
            "milliseconds": 0
          }
        },
        {
          "type": "turn_off",
          "device_id": "a3a72e7f3230c5578c08a9915a651e81",
          "entity_id": "fc2b5aee1f9b5ad179e3fb2cc12bab88",
          "domain": "light"
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01K7MR9RBZMYZ99WZ05BZGSXV9",
      "parent_id": "01K7MR9RBY7P2CNMBE8XZVQH8Q",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Lights - Deck Light On on Movement",
      "message": "triggered by state of binary_sensor.garden_front",
      "source": "state of binary_sensor.garden_front",
      "entity_id": "automation.lights_deck_light_on_on_movement",
      "context_id": "01K7MR9RBZMYZ99WZ05BZGSXV9",
      "domain": "automation",
      "when": 1760559423.8723478
    },
    {
      "state": "on",
      "entity_id": "light.tasmota_deck_light",
      "when": 1760559424.0515604,
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Lights - Deck Light On on Movement",
      "context_message": "triggered by state of binary_sensor.garden_front",
      "context_source": "state of binary_sensor.garden_front",
      "context_entity_id": "automation.lights_deck_light_on_on_movement"
    },
    {
      "state": "off",
      "entity_id": "light.tasmota_deck_light",
      "when": 1760559464.1121926,
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Lights - Deck Light On on Movement",
      "context_message": "triggered by state of binary_sensor.garden_front",
      "context_source": "state of binary_sensor.garden_front",
      "context_entity_id": "automation.lights_deck_light_on_on_movement"
    }
  ]
}

Overlapping trigger

{
  "trace": {
    "last_step": "condition/0/entity_id/0",
    "run_id": "ee8ee9e6b704d46cd0856996333b764d",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2025-10-15T20:17:22.741626+00:00",
      "finish": "2025-10-15T20:17:22.741819+00:00"
    },
    "domain": "automation",
    "item_id": "1760551339871",
    "trigger": "state of binary_sensor.garden_front",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2025-10-15T20:17:22.741696+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.lights_deck_light_on_on_movement",
              "state": "on",
              "attributes": {
                "id": "1760551339871",
                "last_triggered": "2025-10-15T20:17:03.872397+00:00",
                "mode": "single",
                "current": 1,
                "friendly_name": "Lights - Deck Light On on Movement"
              },
              "last_changed": "2025-10-15T20:14:41.602080+00:00",
              "last_reported": "2025-10-15T20:17:13.880873+00:00",
              "last_updated": "2025-10-15T20:17:03.872500+00:00",
              "context": {
                "id": "01K7MR9RBZMYZ99WZ05BZGSXV9",
                "parent_id": "01K7MR9RBY7P2CNMBE8XZVQH8Q",
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "binary_sensor.garden_front",
              "from_state": {
                "entity_id": "binary_sensor.garden_front",
                "state": "off",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Garden Front"
                },
                "last_changed": "2025-10-15T20:17:13.878510+00:00",
                "last_reported": "2025-10-15T20:17:13.878510+00:00",
                "last_updated": "2025-10-15T20:17:13.878510+00:00",
                "context": {
                  "id": "01K7MRA24PVM1BREEX98T084WR",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "binary_sensor.garden_front",
                "state": "on",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Garden Front"
                },
                "last_changed": "2025-10-15T20:17:22.740640+00:00",
                "last_reported": "2025-10-15T20:17:22.740640+00:00",
                "last_updated": "2025-10-15T20:17:22.740640+00:00",
                "context": {
                  "id": "01K7MRAASMD2H9YKR5HWA5NM79",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of binary_sensor.garden_front"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2025-10-15T20:17:22.741727+00:00",
          "result": {
            "result": false
          }
        },
        {
          "path": "condition/0",
          "timestamp": "2025-10-15T20:17:22.741745+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "condition/0/entity_id/0": [
        {
          "path": "condition/0/entity_id/0",
          "timestamp": "2025-10-15T20:17:22.741766+00:00",
          "result": {
            "result": false,
            "state": "on",
            "wanted_state": "off"
          }
        }
      ]
    },
    "config": {
      "id": "1760551339871",
      "alias": "Lights - Deck Light On on Movement",
      "description": "",
      "triggers": [
        {
          "trigger": "state",
          "entity_id": [
            "binary_sensor.garden_front"
          ],
          "from": "off",
          "to": "on"
        }
      ],
      "conditions": [
        {
          "condition": "device",
          "type": "is_off",
          "device_id": "a3a72e7f3230c5578c08a9915a651e81",
          "entity_id": "fc2b5aee1f9b5ad179e3fb2cc12bab88",
          "domain": "light"
        },
        {
          "condition": "sun",
          "after": "sunset",
          "after_offset": "01:00:00"
        }
      ],
      "actions": [
        {
          "type": "turn_on",
          "device_id": "a3a72e7f3230c5578c08a9915a651e81",
          "entity_id": "fc2b5aee1f9b5ad179e3fb2cc12bab88",
          "domain": "light"
        },
        {
          "wait_for_trigger": [
            {
              "trigger": "state",
              "entity_id": [
                "binary_sensor.garden_front"
              ],
              "from": "on",
              "to": "off"
            }
          ],
          "continue_on_timeout": true
        },
        {
          "delay": {
            "hours": 0,
            "minutes": 0,
            "seconds": 30,
            "milliseconds": 0
          }
        },
        {
          "type": "turn_off",
          "device_id": "a3a72e7f3230c5578c08a9915a651e81",
          "entity_id": "fc2b5aee1f9b5ad179e3fb2cc12bab88",
          "domain": "light"
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01K7MRAASNET2VMZS46XXVVF0M",
      "parent_id": "01K7MRAASMD2H9YKR5HWA5NM79",
      "user_id": null
    }
  },
  "logbookEntries": []
}
1 Like

This seems confusing at first blush, but I have a semi-educated guess about what’s happening here.

The mode is never even checked because the conditions don’t pass when the second trigger fires. It would be logical to expect that if the automation is configured as single that it would be somehow “suppressed” while it’s running, but that isn’t the only way to implement single.

Try temporarily turning off your conditions and see if you actually get an overlapping run. I bet you won’t.

2 Likes

By the way, entirely unrelated, but…

I suggest adding a timeout to your wait_for_trigger, and perhaps setting continue_on_timeout to false. Otherwise, you might encounter a situation every now and then where your automation gets stuck: for example, if the sensor does not directly transition from on to off but instead does so through an unknown or unavailable state.

I would probably actually suggest mode: restart for this approach. Consider what happens in the exact situation where you’re currently seeing the overlapping trigger: do you actually want to turn the lights off 30 seconds after the motion sensor has stopped detecting motion, if during that 30 seconds motion has been re-detected?

Indeed you are absolutely right!

Odd implementation, though. I would expect that on a Single automation the evaluation of the automation running state would be the first thing the system would do (i.e. that it wouldn’t even bother to evaluate the conditions).

Thanks, mystery cleared :slight_smile:

I suspect on the whole that conditions failing are a much more common reason for an automation not to be started than mode: single.

Also, there is technically a race condition. single means don’t run the automation if it’s already running. The automation might be at the tail end of its execution when the trigger comes in, but at least in theory have completed by the time all of the conditions are evaluated.

Good points, both of them :+1:

Timeout on the Wait for trigger might be a good idea, indeed. But is there any way to define the trigger condition as “Garden Back is NOT Detected”? This would include all those situations.

I don’t think the Restart mode would work well, at least without modifying the automation. If the automation restarted while in the 30 second delay it would always fail in the initial criteria (that the light was previously off) and never actually turn it off.

I could remove that condition and Restart would work nicely, but I have the “Light is Off” as criteria in order to avoid that the automation runs and ultimately turns off the light in case it has been manually turned On before. But I guess there might be a smarter way to do this :slight_smile:

Maybe remove “light is off as a condition” and encapsulate the turn_on call in an if statement that tests for the state of the light. Then restart will do what you need, I think: it will restart the wait_for_trigger and delay, but it won’t make the turn_on if the light is already on.

By the way, I don’t know your lights, but on many lights it’s harmless to send a turn_on when the light is already on, especially when (as here) you’re not specifying the brightness, color, or other parameters. But you know your lights best, so I assume you have a reason for wanting to avoid a turn_on if the light is already on.

You still want the timeout, though, I think. The question to ask yourself is whether you want the automation to continue or abort when it hits the timeout.

It’s not the turn on I want to avoid, it’s the turn off at the end of the automation :slight_smile:

Basically I want to avoid that the automation turns off the light if anyone has turned it on manually before, and the simplest way I could think of doing it is not run the automation at all if it was already on.

Ah. I think you’re going to need to recruit a helper like an input_boolean to keep track of whether the automation has turned on the light. Otherwise any route you take to this automation you’re going to run into annoying misbehaviors that undercut the utility of the automation. I subscribe to the notion that bad automation is worse than no automation.

And that does up the complexity quite a bit, because you will have to keep the input_boolean up to date, including when someone turns the light OFF manually.

Ideally I would prefer to have no time constraints on the Wait action, but stop the automation altogether, keeping the state of the light, in case the underlying sensor went offline.

Maybe adding a second automation that triggers on the “Unavailable” state of the sensor, and stops all automations that depend on it? It should prevent the automations from waiting forever if the sensor goes Detected → Unavailable → Clear.

But the simplest would be to wait for sensor “Not Detected”, instead of the “Detected → Clear” transition, which would turn off the light if either detection stopped or the sensor went offline, which is fine with me. Is this possible?

You can have multiple triggers in the wait_for_trigger section. Then once you get past that wait, you can test the state of the sensor to see if it’s indicating an actual state or has gone offline.

Still, I don’t think that solves the problem of what happens if the sensor is triggered off and then back on during the delay. Once you emerge from the delay, and you discover that the motion sensor is indicating motion, what do you do?

One suggestion, using an Input Boolean, and two automations (see note below about whether two automations are needed or whether you can roll it all into one):

  1. When the state of the light goes from on to off, set the Input Boolean to FALSE.
  2. [restart mode] When motion is detected, (a) if the light is off, set the Input Boolean to TRUE and turn on the light, (b) wait for the motion sensor to clear, (c) wait for your delay, and (d) if the Input Boolean is TRUE, turn off the light. (The final piece will trigger automation 1).

Now you’re not going to put the light into an automated-off-spiral if it was on before any motion was detected.

As for whether you need multiple automations, technically you don’t. You can assign Trigger IDs to the various triggers and then use an IF or a CHOOSE statement in the automation to figure out what to do. I think it’s cleaner to have individual automations, but that’s a philosophical question and not everyone agrees.

EDIT: Sorry, I revised automation (2) because what I originally proposed wasn’t going to work!

You can add your sun condition to this: Motion activated lights automation