Family room lights staying on all night

Below is my insane attempt at creating a fun scene for the family room. Part of the time it is joined with the kitchen and entry hall in a group of motion directors. The rest of the time, like overnight, it is on its own motion detector. For whatever reason, the lights are staying on all night and not being turned off via no motion. I’m at a loss and sure I’m screwing up royally somewhere. Any help appreciated.

- id: '1899958999017'
  alias: FAMILY room lights on with some cloud cover
  trigger:
    platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.openweathermap_cloud_coverage
      above: 60
      below: 70
    - condition: sun
      before: sunset
      before_offset: -02:00:00
    - condition: time
#      before: 18:00
      after: 09:00
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 30

- id: '1899959999017'
  alias: FAMILY room lights on with some cloud cover set brightness if lights already
    on
  trigger:
    platform: time
    at: 09:00:01
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.family_room_main_lights
      state: 'on'
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 30
- id: '1899958999016'
  alias: FAMILY room lights off
  trigger:
    platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'off'
    for:
      minutes: 2
  condition:
  - condition: time
    after: "21:30:00"
    before: "16:30:00"
  action:
  - service: light.turn_off
    entity_id: light.family_room_main_lights
- id: '1899958999018'
  alias: FAMILY room lights off based on evening
  trigger:
    platform: state
    entity_id:
    - group.family
    to: 'off'
    for:
      minutes: 2
  condition:
  - condition: time
    before: "21:30:00"
    after: "16:30:00"
  action:
  - service: light.turn_off
    entity_id: light.family_room_main_lights


- id: '1899958999011'
  alias: FAMILY room lights lots of cloud cover
  trigger:
    platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.openweathermap_cloud_coverage
      above: 70
    - condition: sun
      before: sunset
      before_offset: -02:00:00
    - condition: time
      after: "09:00"
      before: "20:00"
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 30
- id: '1899958999012'
  alias: FAMILY room lights with barely cloud cover
  trigger:
    platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.openweathermap_cloud_coverage
      above: 30
      below: 50
    - condition: sun
      before: sunset
      before_offset: -02:00:00
    - condition: time
      after: "09:00"
      before: "20:00"
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 0

- id: '1899958999013'
  alias: FAMILY room lights evening time
  trigger:
    platform: state
    entity_id:
    - group.family
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: sun
      after: sunset
      after_offset: -02:00:00
    - condition: time
      before: "20:30"
      after: "15:20"
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 60
- id: '1899959998017'
  alias: FAMILY room lights evening time set brightness if lights already on
  trigger:
    platform: sun
    event: sunset
    offset: -01:59:00
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.family_room_main_lights
      state: 'on'
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 60

- id: '1899958999014'
  alias: FAMILY room lights late night
  trigger:
    platform: state
    entity_id:
    - group.family
    to: 'on'
  condition:
  - condition: time
    before: '06:00'
    after: '20:30'
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 25


- id: '1899959999014'
  alias: FAMILY room late night set brightness if lights already on
  trigger:
    platform: time
    at: '20:30:01'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.family_room_main_lights
      state: 'on'
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 25

- id: '1899958999015'
  alias: FAMILY room lights morning
  trigger:
    platform: state
    entity_id:
    - group.family
    to: 'on'
  condition:
  - condition: time
    after: 06:00
    before: 09:00
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 40
- id: '1899959999015'
  alias: FAMILY room morning set brightness if lights already on
  trigger:
    platform: time
    at: 06:00:01
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.family_room_main_lights
      state: 'on'
  action:
  - service: light.turn_on
    target:
      entity_id: light.family_room_main_lights
    data:
      brightness_pct: 40

that’s a lot of automations…! :laughing:

but it sounds like only one of them isn’t working? the one between 21:30 & 16:30?

Have you looked at the automation trace to see why it’s failing?

Sure, sorry for not sharing originally:

Triggered by the state of binary_sensor.family_room_occupancy at January 31, 2022, 9:28:47 PM

Test time condition

Stopped because a condition failed at January 31, 2022, 9:28:47 PM (runtime: 0.00 seconds)

Executed: January 31, 2022, 9:28:47 PM
Result:
after:
__type: <class ‘datetime.time’>
isoformat: ‘21:30:00’
now_time:
__type: <class ‘datetime.time’>
isoformat: ‘21:28:47.741863’
before:
__type: <class ‘datetime.time’>
isoformat: ‘16:30:00’
result: false

That looks like the trigger happened at ~2128.

Which is before 2130. So the condition is not true. so the actions don’t run and the lights stay on.

Conditions don’t wait to turn true after the trigger occurs. as soon as the trigger is true it checks the conditions immediately. If true the actions run if not then they don’t.

Then the automation waits for the trigger to go back to false then to true before it checks the conditions again.

Ugh, okay so I’m betting that the lights were on due to activity on the group sensor (not family room) and then it switched over to the other family room sensor which had no activity at all so it wasn’t in a triggered state. I’m not sure how to work around this one. Any recommendations?

I include time triggers.

Eg. If my daytime ambient lighting is cyan, and my nighttime is red; then I include a time trigger at the changeover time.

So if you want lights to be off at a certain time. Then include a time trigger and check the motion sensors at that time in order to act accordingly. No motion, off they go :slight_smile:

So, add another automation like this?

- id: '70000958346016'
  alias: FAMILY room lights off again
  trigger:
    platform: time
    at: '20:30:01'
  condition:
  - condition:
  - condition: state
    entity_id: binary_sensor.family_room_occupancy
    state: 'off'
  action:
  - service: light.turn_off
    entity_id: light.family_room_main_lights

That was my initial thought when I first started with HA. And then learned you can add multiple triggers to automations so you don’t need a separate one. Just add a time trigger to the existing automation that turns off the lights.

But wouldn’t that just turn the lights off at the triggered time vs. detecting if the sensor is triggered or not?

- id: '1899958999016'
  alias: FAMILY room lights off
  trigger:
    platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'off'
    for:
      minutes: 2
    platform: time
    at: '20:30:01'
  condition:
  - condition: time
    after: "21:30:00"
    before: "16:30:00"
  action:
  - service: light.turn_off
    entity_id: light.family_room_main_lights

I’m not conceptualizing how it would function sorry if this is foolish.

Possible. If you have a condition to ensure that the motion sensors are off then the lights won’t turn off.

[edit] - looks at yaml… …more like this

  trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_led_should_be_off
    state: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 45
  - platform: time
    at:
    - 07:00:01
    - '23:00:01'
  - platform: homeassistant
    event: start
  condition:
  - condition: state
    entity_id: binary_sensor.bathroom_led_should_be_off
    state: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 45

As you can see, my primary “motion has ended” trigger is the same as my condition. That is because my action has many choose forks that depend on time etc…

Gotcha, okay so I’ll give this a try tonight:

- id: '1899958999016'
  alias: FAMILY room lights off
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'off'
    for:
      minutes: 2
  - platform: time
    at: '20:30:01'
  condition:
  - condition: time
    after: "21:30:00"
    before: "16:30:00"
  - condition: state
    entity_id: binary_sensor.family_room_occupancy
    state: 'off'
    for:
      hours: 0
      minutes: 5
      seconds: 45
  action:
  - service: light.turn_off
    entity_id: light.family_room_main_lights

edited to fix “- platform”

Sadly, the lights didn’t turn off and the traceback shows it failed at the state sensor. Using the above code. The sensor was off at the time though. Confused.

Triggered by the state of binary_sensor.family_room_occupancy at February 1, 2022, 9:39:55 PM
Test time condition
Test state condition
Stopped because a condition failed at February 1, 2022, 9:39:55 PM (runtime: 0.00 seconds)

The state of the binary sensor was clear/off from 9:37:55 PM to 10:11:29PM and did not turn off the lights.

I still think you are misunderstanding triggers and conditions.

for example, you have the following:

trigger:
  - platform: state
    entity_id:
    - binary_sensor.family_room_occupancy
    to: 'off'
    for:
      minutes: 2
  - platform: time
    at: '20:30:01'
  condition:
  - condition: time
    after: "21:30:00"
    before: "16:30:00"
  - condition: state
    entity_id: binary_sensor.family_room_occupancy
    state: 'off'
    for:
      hours: 0
      minutes: 5
      seconds: 45

your two triggers are excluded by your two conditions.

Let’s say the trigger for the binary sensor goes to “true”. the automation will then look at your conditions to decide if the actions will run.

the first condition is the time that the automation is triggered by the binary sensor is between 2130 and 1630 (could be “true” or “false” → if false then no actions taken).

AND (both of the conditions need to be “true”)

the second condition is that the binary sensor needs to be “off” for 5:45.

But the trigger only needed it to be on for 2 minutes for the automation to trigger.

2 minutes is less than 5 minutes and 45 seconds.

So that condition is not “true” → both conditions are not “true” → the automation actions won’t run.

In the second scenario where the automation is triggered by time at 20:30:01 we can again look at your conditions:

the second condition requires that binary sensor be “off” for 5:45 (could be “true” or “false” → if false then no actions taken)

AND (both of the conditions need to be “true”)

the first condition is that it has to be between 21:30 and 16:30.

the 20:30:01 trigger is outside of that time slot of the condition → the condition is “false” → no actions are taken.

The automation will not wait for the conditions to be satisfied after the trigger is set.

The conditions are evaluated at the instant the trigger goes to “true” and if the conditions are met AT THAT INSTANT IN TIME then actions are executed, otherwise they aren’t.

And the automation will not trigger again until at least one of the triggers goes from “false” to “true” again.