Automation, action started within time condition, but is not stopped after the time period

I made a simple automation with trigger id’s to turn on a light when there is movement and turn it of after 5 minutes when there is no movement. This is done in 1 automation with a trigger id and if then.

I added a condition, to only let this work between sunset and 7:00am.

This is working great, but not when there is movement at 6:58. THe light won’t turn of after 5 minutes as the condition is after 7.00am. How can I fix this?

With this I have an other question. So with this automation you have to turn on thelight by hand during the day. I tried to add a trigger, which says that if light A is on for 5 minutes, it has to turn off. But that gives some strange behaviour. Any ideas how to solve this?

you need to post the automation(s) in yaml (no screensots) and ensuring that you format it correctly using ``` on the line before and after the code block.

Sorry, I thought it was a general question. Here’s the code:

alias: "Lights: Beweging voorkant huis"
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    id: motion-detected
  - type: not_occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition:
  - condition: time
    after: "00:30:00"
    before: "07:00:00"
    enabled: true
action:
  - if:
      - condition: trigger
        id:
          - motion-detected
    then:
      - type: turn_on
        device_id: cfd331b9bfa21d6b801b0bfd3ea59d82
        entity_id: a4d8cd24d518a3d37e1d8a82151733dc
        domain: switch
      - type: turn_on
        device_id: 4e9f1d5baed623fd73f1a744cb9d2282
        entity_id: a59157bd71711d8834e57d749d287eb7
        domain: switch
    else:
      - type: turn_off
        device_id: cfd331b9bfa21d6b801b0bfd3ea59d82
        entity_id: a4d8cd24d518a3d37e1d8a82151733dc
        domain: switch
      - type: turn_off
        device_id: 4e9f1d5baed623fd73f1a744cb9d2282
        entity_id: a59157bd71711d8834e57d749d287eb7
        domain: switch
mode: single

the only way you can do it since you are using two opposing triggers in a single turn on/turn off automation is to move the time condition out of the ‘condition:’ section and into the ‘action:’ section. then use a ‘choose:’ in the actions with the time as a condition for the turn on action and no time condition (or a later time condition ie a few minutes later than 7 to accommodate the delay) for the turn off action.

alias: "Lights: Beweging voorkant huis"
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    id: motion-detected
  - type: not_occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
action:
  - choose:
      - condition:
          - condition: time
            after: "00:30:00"
            before: "07:00:00"
            enabled: true
          - condition: trigger
            id:
              - motion-detected
        sequence:
          - type: turn_on
            device_id: cfd331b9bfa21d6b801b0bfd3ea59d82
            entity_id: a4d8cd24d518a3d37e1d8a82151733dc
            domain: switch
          - type: turn_on
            device_id: 4e9f1d5baed623fd73f1a744cb9d2282
            entity_id: a59157bd71711d8834e57d749d287eb7
            domain: switch
    default:
      - type: turn_off
        device_id: cfd331b9bfa21d6b801b0bfd3ea59d82
        entity_id: a4d8cd24d518a3d37e1d8a82151733dc
        domain: switch
      - type: turn_off
        device_id: 4e9f1d5baed623fd73f1a744cb9d2282
        entity_id: a59157bd71711d8834e57d749d287eb7
        domain: switch
mode: single

or if the light gets turned off when you don’t want it to outside of the condition hours:

alias: "Lights: Beweging voorkant huis"
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    id: motion-detected
  - type: not_occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: motion-cleared
action:
  - choose:
      - conditions:
          - condition: time
            after: "00:30:00"
            before: "07:00:00"
            enabled: true
          - condition: trigger
            id:
              - motion-detected
        sequence:
          - type: turn_on
            device_id: cfd331b9bfa21d6b801b0bfd3ea59d82
            entity_id: a4d8cd24d518a3d37e1d8a82151733dc
            domain: switch
          - type: turn_on
            device_id: 4e9f1d5baed623fd73f1a744cb9d2282
            entity_id: a59157bd71711d8834e57d749d287eb7
            domain: switch
      - conditions:
          - condition: time
            after: "00:30:00"
            before: "07:05:00"
            enabled: true
          - condition: trigger
            id:
              - motion-cleared
        sequence:
          - type: turn_off
            device_id: cfd331b9bfa21d6b801b0bfd3ea59d82
            entity_id: a4d8cd24d518a3d37e1d8a82151733dc
            domain: switch
          - type: turn_off
            device_id: 4e9f1d5baed623fd73f1a744cb9d2282
            entity_id: a59157bd71711d8834e57d749d287eb7
            domain: switch
mode: single

general questions are very rarely that. it’s hard to visualize from a description what you are trying to convey when reading the code provides a concrete example.

also it helps others to help you by providing the details needed to allow us to copy/edit/paste the solution.

as in the example above there’s no way I could (or would) type all of that out by hand but using your existing code as a starting point allows me to better help you.

that said none of what I wrote has been tested or even checked for proper syntax but hopefully it works for you.

1 Like

Thank you very much, going to teststraat this weekend.

just an FYI for the future…

Unless you tag me (by using @finity) or click the reply button directly below a post of mine I don’t get notified that you’ve replied to me. So I then have to notice that the thread is in my unread posts list.

Here’s a way to do it using logically ORed Trigger Conditions, where the second one is logically ANDed with a Time Condition, and a single service call.

alias: "Lights: Beweging voorkant huis"
description: ""
trigger:
  - id: 'on'
    type: occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
  - id: 'off'
    type: not_occupied
    platform: device
    device_id: 198487263e84d84d1563c68c75aa7c1c
    entity_id: c159177db18b0e52ce96cff56178b840
    domain: binary_sensor
    for:
      minutes: 3
condition:
  - or:
      - condition: trigger
        id: 'off'
      - and:
          - condition: trigger
            id: 'on'
          - condition: time
            after: "00:30:00"
            before: "07:00:00"
action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id:
        - a4d8cd24d518a3d37e1d8a82151733dc
        - a59157bd71711d8834e57d749d287eb7
mode: single

Here’s another way using State Triggers, logically ORed Template Conditions in shorthand notation where the second one is logically ANDed with a Time Condition, and no trigger id.

alias: "Lights: Beweging voorkant huis"
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.your_motion_detector
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.your_motion_detector
    from: 'on'
    to: 'off'
    for:
      minutes: 3
condition:
  - or:
    - "{{ trigger.to_state.state == 'off' }}"
    - and:
        - "{{ trigger.to_state.state == 'on' }}"
        - condition: time
          after: "00:30:00"
          before: "07:00:00"
action:
  - service: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id:
        - switch.your_first_switch
        - switch.your_second_switch
mode: single

An additional enhancement (to the conditions) would be to avoid turning the switches on (or off) if they are already on (or off).


EDIT

Correction. Changed value of second trigger id to off.

Thanks @finity and @123

You’re welcome!

If you chose to use finity’s suggestion, please consider marking his post with the Solution tag.

If you chose to use either of my two suggestions, please consider marking my post with the Solution tag.

By marking the post with the Solution tag, it will automatically place a checkmark next to the topic’s title. This signals to other users that this topic has been solved and helps them find answers to similar questions. It also places a link in the first post that leads to the Solution post.

For more information, refer to guideline 21 in the FAQ.

@123 In your code, I guess the second id: ‘on’ should that be off instead of on?

And thanks for the enhanchement tip, as I also have a PIR in my hallway. The lights turn on, but as we are moving there a lot for minutes, it means the automation is started many times, as the light was already on.

1 Like

Yes. Thank you for reporting the mistake. I have corrected the example.

For now I implemented the solution by @finity and see if it works like i would like it But I am going to try yours too @123, as it learned me to think in automations.

1 Like