Logic help with a timer

Hello - Need some help with a timer automation. Basically, when motion is detected between a specific timeframe (condition), start the timer and turn on some lights… Once it finishes, turn the lights off. I think my automation is stuck in the conditions section as when timer.finished triggers, it won’t fall through the conditions to my Choose action. Can someone help get me over this logic hump?

alias: Stairs-1stFloor - Morning Lights NEW
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.hallway_early_morning_timer
    id: Hallway-Timer-FINISHED
  - platform: state
    entity_id:
      - binary_sensor.1st_floor_hallway_air_sensor_01_motion_sensor
    from: "off"
    to: "on"
    id: Hallway-Motion-DETECTED
condition:
  - condition: time
    after: "01:00:00"
  - condition: and
    conditions:
      - condition: sun
        before: sunrise
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Hallway-Motion-DETECTED
        sequence:
          - service: timer.start
            data:
              duration: "00:45:00"
            target:
              entity_id: timer.hallway_early_morning_timer
          - service: switch.turn_on
            data: {}
            target:
              entity_id:
                - switch.kasa_1_family_room
                - switch.kasa_3_living_room
      - conditions:
          - condition: trigger
            id:
              - Hallway-Timer-FINISHED
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id:
                - switch.kasa_1_family_room
                - switch.kasa_3_living_room
mode: single
condition:
  - condition: time
    after: "01:00:00"
  - condition: and
    conditions:
      - condition: sun
        before: sunrise

First of all your AND statement is after the first condition. So its never going do an AND compare between the 2.
Conditions are already AND so listing multiple will act like that. Conditions - Home Assistant
2nd: For some reason just putting before sunrise will cause the condition to fail, you can test this in the automation by using the Test option with it. Someone can prob explain why this is, but it works like this :face_with_raised_eyebrow:

condition: 
  - condition: time
    after: "01:00:00"
  - condition: sun
    after: sunset
    before: sunrise

image

Instead of turning off the switches 45 minutes after motion is detected, why not simply turn them off after no motion is detected for 2 minutes? You can use a State Trigger to do that with its for option set to minutes: 2 and don’t need a timer entity.

Or is there a requirement to have the lights on for a minimum of 45 minutes?

2 Minutes isn’t long enough in my situation… and if HA is restarted/updated during the “for” period, time is lost and the lights would never turn off… This is exactly the reason the timer helpers were created.

I have similar automation. I have currently motion sensor and I want to turn off light when there is no motion for 2 minutes. But if in 2 minutes motions is detected, again, then reset timer and start count down again.
This is what I have and it’s working perfectly fine:

alias: Kitchen light
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_motion_sensor_occupancy
    from: "off"
    to: "on"
    id: Motion detected
  - platform: state
    entity_id:
      - binary_sensor.kitchen_motion_sensor_occupancy
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: Motion is off
    from: "on"
  - platform: state
    entity_id:
      - timer.kitchen_light_helper
    from: active
    to: idle
    id: Helper
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Motion detected
          - type: is_illuminance
            condition: device
            device_id: 4ffbaa70f931ad2540d3055c4212e7dc
            entity_id: sensor.kitchen_motion_sensor_illuminance_lux
            domain: sensor
            below: 50
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.kitchen_light_helper
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_light
      - conditions:
          - condition: trigger
            id: Motion is off
        sequence:
          - service: timer.start
            data: {}
            target:
              entity_id: timer.kitchen_light_helper
      - conditions:
          - condition: trigger
            id: Helper
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_light
mode: single
max_exceeded: silent

It was merely a suggestion; the time period can be whatever you want. If there’s no morion detected for 5 minutes then it implies no one is present (unless they’re sitting on the toilet) and it seems prudent to turn off the lights and not wait another 40+ minutes. However, it’s your choice how you want it to work.

That’s true but if the for period is short then the chances of being interrupted by a restart are minimized (in my case, the host machine is protected by a UPS so I’m not concerned by unscheduled restarts). There are other ways to mitigate this situation on startup but it sounds like you prefer to use a timer so I won’t dwell on alternatives.

BTW, I recall a bug report claiming a timer entity’s ability to start its countdown again, after being interrupted by Home Assistant restarting, wasn’t working. Perhaps it has been fixed but have you recently tested an interrupted timer’s ability to pick up from where it left off?

That’s the usual way of implementing a motion-controlles light (and what I had suggested to Brewder). However, as pointed out by Brewder, the for’s countdown doesn’t survive a restart so the preference is to employ a timer.

OK I cleaned up my previous conditions block but I’m still struggling. The lights never go OFF. I suspect that’s because once the timer finishes, my conditions block never allows it to fall through to the Choose actions where I trigger the lights OFF.

I think I need to add an OR statement to account for situations when the time was activated between 1am and Sunrise but expires AFTER that time period.

Example: Motion is detected at 06:15am and before Sunrise at 06:30. 45 minute timer starts. Timer expires at 06:45… The timer.finished trigger activates but won’t PASS the conditions block as it’s no longer between 1am and Sunrise… How do I get it to “fall through” to my choose actions?

Any more thoughts?

alias: Stairs-1stFloor - Morning Lights NEW
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.hallway_early_morning_timer
    id: Hallway-Timer-FINISHED
  - platform: state
    entity_id:
      - binary_sensor.1st_floor_hallway_air_sensor_01_motion_sensor
    from: "off"
    to: "on"
    id: Hallway-Motion-DETECTED
condition:
  - condition: and
    conditions:
      - condition: time
        after: "01:00:00"
      - condition: sun
        before: sunrise
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Hallway-Motion-DETECTED
        sequence:
          - service: timer.start
            data:
              duration: "00:45:00"
            target:
              entity_id: timer.hallway_early_morning_timer
          - service: switch.turn_on
            data: {}
            target:
              entity_id:
                - switch.kasa_1_family_room
                - switch.kasa_3_living_room
      - conditions:
          - condition: trigger
            id:
              - Hallway-Timer-FINISHED
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id:
                - switch.kasa_1_family_room
                - switch.kasa_3_living_room
mode: single

Are you sure this is correct event ?

trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.hallway_early_morning_timer
    id: Hallway-Timer-FINISHED

Why don’t you use state for a trigger like I do? Probably your event doesnt exist or doesnt exist in this form so your automation never triggers to turn off the lights.

That’s a great question. I followed another tutorial that showed the coding for “timer.finished” as the event type and I set it up my home office lighting automaton. It seems to work great there so I was duplicating the setup.

If I converted to use state, I guess the state goes from running to idle is how I would code that?

The same way I did in my automation. Just check your timer statuses. It should show active when it is running and idle when it stop.

I’m pretty confident the event check for timer.finished works fine… I just re-checked my home Office automation and the timer documentation here: Timer - Home Assistant (home-assistant.io)

So I’ll just need to keep testing my hallway automation or simply add a failsafe automation that ensures the lights are off at a specific time… it’s not ideal but will work.

You can check event in developer tools → events. As I know your trigger event got this info from there.

The trigger activates… so I know that’s working… but it never gets through to my Choose actions…

Just move the time check condition to the choose condition, that way the timer finishing can always turn of the lights

Funny thing i tested this and it just works (changed around times for testing)
Light goes on for 10 seconds and then goes off

alias: Test Timer
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: a26f03d706ce19099bdcbffc3f5c1116
    type: action
    subtype: up-press
    discovery_id: 0x001788010965da9e action_up-press
    id: "on"
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.test_timer
    id: timer
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
          - condition: sun
            before: sunset
            after: sunrise
          - condition: time
            after: "01:00:00"
        sequence:
          - service: timer.start
            data:
              duration: "00:00:10"
            target:
              entity_id: timer.test_timer
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.rgbw_esther
      - conditions:
          - condition: trigger
            id:
              - timer
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.rgbw_esther
mode: single

Well now I feel dumb! Now it seems so obvious! thank you.