Automation of motion sensors

Hi;

I’m not sure if this is the right Areas for automation questions, but I didn’t find a better one :slight_smile:

I want to automate my corridor lighting with a motion sensor. Turnning light on and off after x minutes works fine as described here:

What I want to achieve is the following and I’m not sure how to do that. I want the motion sensor actions to depend on weather i turned the light on or off manually before.

E.g.:
- Light turned on manually (via button, app or other automation)

  • Motions is detected -> nothing should happen (as my motion detection automation sets the light to a specific brightness and i don’t want it to change when i turn on the light manually)

  • No more motion detected (short term) -> light should not be switched out

  • No more motion detected for >1 hour -> light should be switched off

  • After turning off the light manually the normal motion detection logic (on - off after 10min.) should be in place again.

Thanks for any hints on how to achieve this.
Dennis

without you posting the code you have working now I can just give you a generic answer but you can add a condition that requires the light to be off before the automation will trigger.

as for the longer than hour requirement you might have to create another automation to do that separately.

my current code is this:

- alias: Turn on corridor light when there is movement
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: binary_sensor.presence_8
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.flur_deckenlampe

- alias: Turn off corridor light x minutes after last movement
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: binary_sensor.presence_8
    to: 'off'
    for:
      seconds: 1
  action:
    service: light.turn_off
    entity_id: light.flur_deckenlampe

So for the first part (not doeing anything if light is on) i could I add the following?:

  conditions:
    - condition: state
      entity_id: light.flur_deckenlampe
      state: 'off'

The second part, (not) turning off (or after 1 hour) I have no idea yet…

Yes that will work for the first automation in the post above.

For the second one I think you will probably need to create an input_boolean to sense whether the light was switched on by the switch or the motion sensor and then use that as the condition for the second automation.

so something like -

input_boolean:
  some_name_here:

then in an automation -

- alias: turn_on_boolean
  trigger:
    platform: state
    entity_id: light.flur_deckenlampe
    to: 'on'
  condition:
    condition: state
    entity_id: binary_sensor.presence_8
    state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: input_boolean.some_name_here

Then use the state of the boolean being off in the the second automation. It then won’t turn off if you turn the light on manually while the motion sensor is off because that will turn on the boolean.

All of this assumes that the light has an instant state feedback to HA. IOW, if the light is an “always powered” smart bulb. If the switch kills power to the light and there is no way for HA to know if the light got turned on manually because you turned the power back on to the smart bulb then all bets are off.

In that case I don’t know how you could do what you want to accomplish.

I think I need to edit my post above to cover the situation when the light gets turned back off manually…

- alias: toggle_boolean
  trigger:
    platform: state
    entity_id: light.flur_deckenlampe
  condition:
    condition: state
    entity_id: binary_sensor.presence_8
    state: 'off'
  action:
    service_template: >
      {% if trigger.to_state.state == 'on' %}
        homeassistant.turn_on
      {% elif trigger.to_state.state == 'off' %}
        homeassistant.turn_off
      {% endif %}
    entity_id: input_boolean.some_name_here

Hi finity,

the first part is creating a variable, correct?

For the second part: But i often would use a Switch in the room to turn on the light manually, but it could be that the motion sensor is still detecting motion and would be “on” in this case, so the action would not be triggered, right?

It’s not really a “variable”. It is a virtual switch that only exists in the software. It can then be controlled by HA in automations or be turned on & off thru the front end manually.

yes, if that is true then what you want to do won’t work in that way.

I have a situation that is a little bit similar to your situation in that I have smart bulb that is controlled by a motion sensor. When it detects motion it turns the light on and when the motion is no longer detected it waits 3 minutes and then turns the light back off.

The problem is that if i enter the room the lights turns on automatically then I sit down in the chair it stops detecting motion and the light turns off even if I’m still in the room.

I solved it in a kind of hacky way by making an input_boolean that prevents the light from being turned off automatically. Then I have a 30 minute timer that turns off the light if no motion is seen in that amount of time with the input boolean on. If motion is seen in that time then it resets the 30 minute timer to start over.

It’s not ideal but it works OK for me.

that’s exactly the same problem I have. But with your logic, how do you activate the boolean. because you don’t want it to stay on for 30min every time, right? in most cases you just want the 3 minutes, but in some cases you sit down and want the 30min

I have to turn it on manually if I’m going to be in the room for a while. Most times I don’t use it tho since I’m just passing thru the room. In that case the 3 minute delay works fine.

It’s kind of the same situation. But it needs to be “Family friendly” here :wink: So using the Button to turn on the light / or just press it again while I’m in the room could also set the boolean to true (buttons work in my config with automations anyway). That leaves 2 problems:

  • what if i turn the light on via the web ui/app. That obviously is not done via an automation
  • how does your automation for the 30 min delay look like?

input_boolean.bool_1 is the boolean to disable the light automation. when that is on it turns on the light if it’s off and starts a 25 minute timer. it then flashes the light to give a 5 minute warning that it’s going to be turned off. If I want the light to stay on then I turn on input_boolean.bool_2 to restart the timer for 25 minutes.

automation:
  - alias: Turn on computer room lights when there is movement
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.comp_rm_motion_template
        to: 'on'
      - platform: state
        entity_id: input_boolean.bool_1
        to: 'on'
    action:
      - service: light.turn_on
        entity_id: light.comp_light_template
      
  - alias: Turn off computer room lights 3 minutes after last movement
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.comp_rm_motion_template
        to: 'off'
        for:
          minutes: 3
      - platform: state
        entity_id: input_boolean.bool_1
        to: 'off'
    condition:
      condition: state
      entity_id: input_boolean.bool_1
      state: 'off'
    action:
      - service: light.turn_off
        entity_id: light.comp_light_template
  
  - alias: Computer Room Light Control
    trigger:
      - platform: state
        entity_id: input_boolean.bool_1
        to: 'on'
      - platform: state
        entity_id: input_boolean.bool_2
        to: 'on'
    action:
      - service: script.turn_off
        entity_id: script.comp_light_control
      - delay: '00:00:02'
      - service: script.turn_on
        entity_id: script.comp_light_control

script:
  comp_light_control:
    alias: Computer Room Light Control Script
    sequence:
      - delay: '00:25:00'
      - service: light.turn_off
        data:
          entity_id: light.comp_light_template
      - delay: '00:00:01'
      - service: light.turn_on
        data:
          entity_id: light.comp_light_template
      - delay: '00:00:01'
      - service: light.turn_off
        data:
          entity_id: light.comp_light_template
      - delay: '00:00:01'
      - service: light.turn_on
        data:
          entity_id: light.comp_light_template
      - delay: '00:05:00'
      - service: homeassistant.turn_off
        data:
          entity_id: input_boolean.bool_1

Thanks.

It kind of works now. if i press an on button on a remote the Boolean is set to “on” and to “off” accordingly. The only thing that doesn’t work is turning lights on via webui/app as this doesn’t trigger the automation (as does the remote)

If you turn on/off input_boolean.bool_1 on the webUI then it will trigger the automation to turn on/off the light.

See the second trigger in the automations above.

If it doesn’t then you will need to post the code that you have now so we can troubleshoot why it isn’t working.

I have a similar issue. I am unsure as to why this stopped working but I am struggling to try and fix the problem. So, I have an automation that I want to turn on lights when there is motion detected within my kitchen space. A second one that turns them off if no motion is detected after 20 minutes. Now, I can manually trigger the automation through the UI and they will turn on and off but for some reason, they will not do so automatically. Can anyone assist me please?? The yaml for the two automations are below.

alias: Kitchen/Dining Room lights on
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion_sensor
    attribute: state
    from: 'off'
    to: 'on'
condition:
  - condition: sun
    after: sunset
action:
  - service: light.turn_on
    data:
      brightness_pct: 100
    entity_id: group.kitchen_lights
mode: single
alias: Kitchen/Dining Room lights off
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion_sensor
    attribute: state
    from: 'on'
    to: 'off'
    for: '00:20:00'
condition:
  - condition: time
    after: '19:00'
    before: '08:00'
action:
  - service: homeassistant.turn_off
    data: {}
    entity_id: group.kitchen_lights
mode: single

Any direction is greatly appreciated and thank you in advance!
Lyall

Could you show the details of the motion sensor entity from the developers tools->states page?

Does it actually have an attribute named “state” that is either ‘on’ or ‘off’?

if not then try this in the ‘on’ automation (and similar syntax for the ‘off’):

trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion_sensor
    to: 'on'

came across https://github.com/arthurdent75/SimpleScheduler , simple and elegant scheduling