Light auto off

What I’m trying to achieve:
if the light is on, and no motion detected in the past xx seconds, turn off the light.
This is my “scene” in Fibaro HC2 which does exactly this:


If light == on for 90s
and
sensor != detected for 90s
then
turn off the light

Doesn’t matter if the light is turned on by another automation or manually, as long as there is no motion detected in the past 90 seconds, the light will turn off.
My config in HA:

- id: '1564747886278'
  alias: 洗衣房灯2关闭
  trigger:
  - entity_id: light.unknown_xi_yi_fang_deng_2_146
    for: 
        seconds: 90
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.0x00158d000319b07a_occupancy
    state: 'off'
  action:
  - data:
      entity_id: light.unknown_xi_yi_fang_deng_2_146
    service: light.turn_off

The motion sensor goes to “clear” after 60 seconds no motion detection. This automation works fine except if I turned the light on manually without triggering the sensor and trigger the sensor after 60 seconds, the light will simply stay on indefinitely.

Difference between Fibaro and HA automation:
It seems Fibaro automation is constantly running as long as the state of the light is on.
HA automation only runs once when the state of the light goes from off to on.

I’m sure with the flexibility of HA, this is achievable, any help or tips would be highly appreciated.

trigger: no motion for at least 90 seconds
condition: light is on
action: turn off the light

- id: '1564747886278'
  alias: 洗衣房灯2关闭
  trigger:
  - entity_id: binary_sensor.0x00158d000319b07a_occupancy
    for: 
        seconds: 90
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: light.unknown_xi_yi_fang_deng_2_146
    state: 'on'
  action:
  - data:
      entity_id: light.unknown_xi_yi_fang_deng_2_146
    service: light.turn_off
1 Like

This doesn’t seem to work if the light is turned on manually without triggering the sensor.

trigger:
  - entity_id: binary_sensor.0x00158d000319b07a_occupancy
    for: 
        seconds: 90
    platform: state
    to: 'off'

Does this mean the sensor has to go from “on” to “off”, then start the 90s countdown? Or HA is constantly counting?

Correct. The automation’s trigger detect state-changes. In this case, it detects when the sensor changes to off.

If you simply want the light to turn off after 90 seconds, even if the motion sensor is never triggered, then why bother monitoring the motion sensor’s state? Just create an automation to turn off the light after 90 seconds.

Problem is I don’t want it to turn off if motion is detected during the 90 seconds.
Is it possible to reset the 90s countdown whenever motion is detected?

That’s the way this works:

        seconds: 90
    platform: state
    to: 'off'

It is stating that the motion sensor must remain off for at least 90 seconds in order for the trigger to activate. Think of it as a timer that must count to 90 seconds. If the motion sensor’s state changes to on while the timer is counting, it causes the timer to restart counting from 0.

1 Like

That makes sense. I guess to achieve what I wanted, I’ll just add another automation on top of this one.
Thanks for you help!