Motion and Lights

Hi, I have the following dilemma:

Generally, I have a lot of automations that combine motion sensors and lights. But, there are moments that I want to have lights on without them being turned off by the automation if the motion sensor does not see me.

Whats the trick to “turn off” such automations for a short while(for instance, by pressing the light switch or other method), apart from manually turning the automation off. Thanks

I am a fan of the timer helper, because it allows you to manipulate it and check for it in other automations. So if you set the off action on a timer, you can use whatever you want (press of a switch or whatever, to abort the timer.

I only let the motion detection turn off a light if the motion detection turned on the light.

I use a convention of a double tap on the switch to create a permanent occupancy until I turn the light off.

Here’s an example using zwave as the trigger. I just add this in as one of the occupancy sensors for the light control.

  - trigger:
      - platform: homeassistant
        event: start
        id: "start"
      - platform: event
        event_type: zwave_js_value_notification
        id: keypressed2x
          node_id: 43
          command_class_name: Central Scene
          property_key: "001"
          value: KeyPressed2x
      - platform: state
        entity_id: switch.attic_light
        to: "off"
        from: "on"
        id: switch_off
      - name: "lt_hold_attic"
        state: >-
          {% if == "keypressed2x" %} 1 {% else %} 0 {% endif %}
      - binary_sensor.lt_hold_attic
1 Like

Not sure in your exact use case how it would best be handled, but I do know the below is VERY useful to have in your configuration.yaml and this will be of help to you for this problem or future issues:

Here is how to record if the light was turned on or off manually or by the dashboard UI, by an automation or manually - saved into a run-time state variable, so you can use that helper in your logic later. In my example below, I only needed to know how the light was last turned off, but you can update your logic to use as needed:

So if the light is on, “Bathroom Lights Off Context” = “n/a”.
If the light is off, “Bathroom Lights Off Context” will be “physical”, “dashboard_ui”, or “automation”.

# Bathroom Lights (for accessing these and with the last_changed value):
# {{ states('sensor.bathroom_lights_off_context') }}
# {{ states.sensor['bathroom_lights_off_context'].last_changed }}
# 1. Track the 'off' context changing (has to be triggered even if turning on,
# so any change at all will cause logic to be triggered if subsequent objects
# already set as "physical" will still be updated)
  - trigger:
      - platform: state
        entity_id: light.bathroom_shelly_1_relay
      - name: "Bathroom Lights Off Context"
        state: >
          {% set c_id = %}
          {% set c_parent = trigger.to_state.context.parent_id %}
          {% set c_user = trigger.to_state.context.user_id %}
          {% if states('light.bathroom_shelly_1_relay') == 'on' %}
          {% elif c_id != none and c_parent == none and c_user == none %}
          {% elif c_id != none and c_parent == none and c_user != none %}
          {% elif c_id != none and c_parent != none and c_user == none %}
          {% else %}
          {% endif %}
        unique_id: bathroom_lights_off_context

1 Like

Can you elaborate a bit more how do you exactly use the timer helper in this specific situation with lights and motion? Thanks

It depends a lot on what means have to cancel motion behavior. I’m also not at a pc, so writing actual automations for you is a bit hard now.

The basics are:

  • When motion is detected, check if the light is already on. If so, you should do nothing and the automation to turn lights of when motion ends should not be active. Only if lights are off, activate the “lights off by motion” automation and turn the light on. Cancel any light off timer (see below when it is activated).
  • The “light off by motion automation” should do this: When motion is stopped (and motion activated the light), start a light off timer (to give you time to start moving again).
  • When the light off timer ends, turn of the light. disable the “lights off by motion” automation.
  • when some one presses the lights on button or the lights off button (or any other action that you want to cancel the “motion turns lights off” behavior: cancel the lights off timer, “disable the lights off by motion” automation.

Ps. It is a bit complicated because you should not want to start the lights off timer when motion starts. If you do that, and motion persists, the lights will go off while you are still in motion. If you do take that approach, then when the timer runs out you should check for motion then the timer and restart the timer if there is motion. But then you should also restart the timer when motion ends. Otherwise the timer could run out right after motion stopped. The upside of that appoach is that the timer is the only thing you need to look at to see if motion started it. Then you do not need to disable automations.

1 Like