Turn a light off after x mins if it was turn on via rule

thats a sticky situation. You could change your automation to just look at the to_state instead of from:off to:on

1 Like

Create a binary sensor around the motion sensor with “delay_off” set to 10 seconds. Will delay the reported “off” by 10 seconds. If the state of the motion goes back to true during that 10 seconds it will not turn off.

Then you automation doesn’t change except for the binary_sensor that is the trigger.

Related example; but with delay_on but delay_off works the same way:

garage_door_stuck:
      friendly_name: Garage Door Stuck
      device_class: garage_door
      value_template: "{{ is_state('binary_sensor.garage_door_detected', 'on') }}"
      delay_on: 
        minutes: 10
3 Likes

You don’t have a timer extend function? In openHAB we know the timer exists and is it null, and in subsequent actions can say reschedule the timer for another 5 mins.

But that’s an edge case somewhat.

I did once try this

  - wait_template: "{{as_timestamp(now()) - as_timestamp(states.binary_sensor.centralite_3326l_0e33c46e_1_1280.last_changed) > 10 }}"  

But I could not get that to work, lights came on, but of never turned off.

So, based on your feedback, This look good?

binary_sensors.yaml (called from my configuration.yaml)
 - platform: template
   sensors:
     motion_delay_kitchen:
       friendly_name: Kitchen Lighting motion delay
       value_template: "{{ is_state('binary_sensor.centralite_3326l_0e33c46e_1_1280', 'on') }}"
       delay_off: 
         seconds: 15

Then automation.yaml

- alias: Motion Lighting in the Kitchen
  trigger:
  - platform: state
    entity_id: binary_sensor.centralite_3326l_0e33c46e_1_1280
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: group.kitchen_zwave_lights
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - service: homeassistant.turn_on
    entity_id: group.kitchen_zwave_lights
  - wait_template: "{{ is_state('binary_sensor.motion_delay_kitchen', 'off') }}"
  - condition: state
    entity_id: group.kitchen_zwave_lights
    state: 'on'
  - service: homeassistant.turn_off
    entity_id: group.kitchen_zwave_lights

Nice trick, thanks everyone for the ideas.

Edit: I did go back and make one change. I’m going to keep the trigger as the motion sensor, but the wait to the new binary sensor. I figure that will be slgihtly faster triggering the lights to on. One less thing to monitor

@petro I have an other need :stuck_out_tongue:

When I open a door I’d like to turn on a light.
When I close the door… Turn the light off.

I’m sorry to disturb you again for simple stuff but with my actual knowledge I need to create 2 " - alias : " automation :frowning:

Creating 2 de-activable items in front panel…

Thanks again

Finaly I got it :wink:

- alias: 'Grenier'
  trigger:
    - platform: state
      entity_id: binary_sensor.nano_grenier_4_3
      to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.nano_grenier_4_2
    - wait_template: "{{ is_state('binary_sensor.nano_grenier_4_3', 'off') }}"
    - service: switch.turn_off
      entity_id: switch.nano_grenier_4_2
1 Like

Looks good. I think it’s a matter of style and opinion on keeping the trigger and wait as you have them.

In my opinion I’d base them both off motion_delay_kitchen as that way if something got buggered in a future release you would be more likely to notice the entire automation not working; versus partially. As you have it you could end up with a split-brain situation.

But I also respect your “need for speed”

1 Like

Yep, you got it. That’s the purpose of wait templates. Good use of it!

Thanks again for the help. The binary sensor template idea was great. Worked perfect last night. Tonight, I’ll swap it to use the binary sensor as the trigger too, and see if I can tell any difference in speed. :slight_smile:

1 Like

Even on a rPI1 you will not notice :wink:

would there be any difference in placing the delay? in your configuration you place it at the binary_sensor , others have put it at the wait_template calling the binary_sensor.

secondly, since I cant find any reference in this thread, what s the difference with the for: statement?

example:

  - id: Switch Bedside table off when room left
    alias: "Switch Bedside table off when room left"
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.master_bedroom_motion_sensor
      to: 'off'
      for:
        minutes: 10
    condition:
      condition: state
      entity_id: binary_sensor.activity_opstaan
      state: 'on'
    action:
      - service: light.turn_off
        data:
          entity_id: light.bedside_table
      - condition: state
        entity_id: input_boolean.notify_notify
        state: 'on'
      - service: notify.notify
        data_template:
         message: '{{as_timestamp(now()) | timestamp_custom("%X") }}: Bedside table is turned down.'

havent tested it yet since it hasn’t happened , but i was hoping this would take care of motion occurring during the time the triggering sensor is off, turning it on again?..
not yet a formal binary_sensor, but a sensor with only states On and Off…

Is this a good and sure way (even after restart HA) to turn on and off an entity after X hours?

## Turn on Hall Light when leaving for work
- alias: Turn On Bathroom Heater
  trigger:
    platform: time
    at: '05:45:00'
  condition:
   - condition: time
     weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
    - service: switch.turn_on
      entity_id: switch.sonoffs20_1    

- alias: 'Turn Off Bathroom Heater'
  trigger:
    platform: state
    entity_id: switch.sonoffs20_1
    to: "on"
    for: '02:00:00'
  action:
  - service: switch.turn_off
    entity_id: switch.sonoffs20_1