Light on with switch or motion detector and switch off automatically if there is no movement

Hello, I have been looking for a long time to switch on the light via a switch or a motion detector and switch off the light again when no movement is detected

for this I created a timer in the configuration.yaml

timer

then I created 2 automations to start the timer 1 time

and the 2 automation to switch off the light after the timer has expired

timer2

Maybe someone could use it …

2 Likes

Would be way more useful if you provide code instead of pictures of code…

Please read the community guidelines, especially point 11 on formatting code.

3 Likes

Thanks for sharing.

Unfortunately because you have pasted images of text rather than correctly formatted text it is difficult for others to use your code. Text can be copied and pasted. Images can’t.

Also your conditions can be considerably simplified. I’m not going to type all that out to show you how. If it was text I could.

In fact the conditions are not doing anything as your triggers are already OR logic. Your conditions, A or (B (or C)) is equivalent to A or B or C. Which is what your triggers are already doing.

Also, also, this can be done a lot simpler without a timer and only one automation. See the example in the v0.113 release notes.

EDIT: spent too long looking for that example.

1 Like

ok here is code…

configuration.yaml

Timer erstellen

timer:
lichtautomatischaus:
duration: ‘00:05:05’

lichtautomatischausflurunten:
duration: ‘00:05:05’

Automation 1

alias: 1/2 Bad-Licht-Auto-Aus
description: ‘’
trigger:

  • platform: state
    entity_id: switch.meq0454469_1
    from: ‘off’
    to: ‘on’
  • platform: state
    entity_id: switch.meq0454469_2
    from: ‘off’
    to: ‘on’
  • platform: state
    entity_id: binary_sensor.presence_2
    from: ‘off’
    to: ‘on’
    condition:
  • condition: or
    conditions:
    • condition: state
      entity_id: switch.meq0454469_1
      state: ‘on’
    • condition: or
      conditions:
      • condition: state
        entity_id: switch.meq0454469_2
        state: ‘on’
      • condition: or
        conditions:
        • condition: state
          entity_id: binary_sensor.presence_2
          state: ‘on’
          action:
  • service: timer.start
    data: {}
    entity_id: timer.lichtautomatischaus
    mode: single

Automation 2 …

alias: 2/2 Bad-Licht-Auto-Aus
description: ‘’
trigger:

  • platform: event
    event_type: timer.finished
    event_data:
    entity_id: timer.lichtautomatischaus
    condition: []
    action:
  • service: switch.turn_off
    data: {}
    entity_id: switch.meq0454469_2
  • service: switch.turn_off
    data: {}
    entity_id: switch.meq0454469_1
    mode: single

Please have a read of point 11 in the link BurningStone posted above, then edit your post to format the pasted code correctly. Indentation is very important in YAML.

nevertheless thank you! i will need a code like this!

See this then: https://www.home-assistant.io/blog/2020/07/22/release-113/#automations--scripts-bug-fix

1 Like

how can i post the code

is it so right

configuration.yaml

# Timer erstellen
timer:
  lichtautomatischaus:
    duration: '00:05:05'

Automation 1

  alias: 1/2 Bad-Licht-Auto-Aus
  description: ''
  trigger:
  - platform: state
    entity_id: switch.meq0454469_1
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: switch.meq0454469_2
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.presence_2
    from: 'off'
    to: 'on'
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: switch.meq0454469_1
      state: 'on'
    - condition: or
      conditions:
      - condition: state
        entity_id: switch.meq0454469_2
        state: 'on'
      - condition: or
        conditions:
        - condition: state
          entity_id: binary_sensor.presence_2
          state: 'on'
  action:
  - service: timer.start
    data: {}
    entity_id: timer.lichtautomatischaus
  mode: single

Automation 2

  alias: 2/2 Bad-Licht-Auto-Aus
  description: ''
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.lichtautomatischaus
  condition: []
  action:
  - service: switch.turn_off
    data: {}
    entity_id: switch.meq0454469_2
  - service: switch.turn_off
    data: {}
    entity_id: switch.meq0454469_1
  mode: single
2 Likes

Sure, it can be easier if someone likes to write an idea, but I haven’t found anyone else for me …
That works first … And I wanted to post that because maybe someone is facing the same problem …

Great! You got it.

Now try this and see if it does the same thing:

  - alias: 1/2 Bad-Licht-Auto-Aus
    trigger:
      - platform: state
        entity_id:
          - switch.meq0454469_1
          - switch.meq0454469_2
          - binary_sensor.presence_2
        from: 'off'
        to: 'on'
        for: '00:05:05'
    action:
      - service: switch.turn_off
        entity_id: 
          - switch.meq0454469_2
          - switch.meq0454469_1
    mode: single

Though I’m a little worried abut this:

  - platform: state
    entity_id: binary_sensor.presence_2
    from: 'off'
    to: 'on'

Do you really want to turn the light off if presence has been detected?

If not:

  - alias: 1/2 Bad-Licht-Auto-Aus
    trigger:
      - platform: state
        entity_id:
          - switch.meq0454469_1
          - switch.meq0454469_2
          - binary_sensor.presence_2
        from: 'off'
        to: 'on'
        for: '00:05:05'
      - platform: state
        entity_id: binary_sensor.presence_2
        from: 'on'
        to: 'off'
        for: '00:05:05'
    action:
      - service: switch.turn_off
        entity_id: 
          - switch.meq0454469_2
          - switch.meq0454469_1
    mode: single
2 Likes

hey tom thanks for your help i just tried that …

alias: test
description: ''
mode: single
trigger:
  - platform: state
    entity_id: switch.meq0454469_2
  - platform: state
    entity_id: switch.meq0454469_1
  - platform: state
    entity_id: binary_sensor.presence_2
    from: 'off'
    to: 'on'
    for: '00:02:00'
  - platform: state
    entity_id: binary_sensor.presence_2
    from: 'off'
    to: 'on'
    for: '00:02:00'
action:
  - service: switch.turn_off
    data: {}
    entity_id: switch.meq0454469_2
  - service: switch.turn_off
    data: {}
    entity_id: switch.meq0454469_1

but when I turn on the light it goes off again …

1 Like

It’s probably your presence detection as I said above it should probably be:

    trigger:
      - platform: state
        entity_id:
          - switch.meq0454469_1
          - switch.meq0454469_2
          - binary_sensor.presence_2
        from: 'off'
        to: 'on'
        for: '00:05:05'
      - platform: state
        entity_id: binary_sensor.presence_2
        from: 'on'
        to: 'off'
        for: '00:05:05'

Hey Tom, thank you very much, it works wonderfully … Thank you very much … Even if I don’t know why yet … I have problems with many examples here in the forum … Simply copying and adapting unfortunately does not work … That is certainly due to the Formatting…

I use in Home Assistant … Visual Studio code …

Simply copy and paste doesn’t work … of course you have to adapt the entity etc. … but the formatting doesn’t work … you might have a tip

I think you should go with Tom’s second automation
Don’t feel too bad about your efforts but this is a subject we see a LOT of.
If you had searched the forum you’d have come up with many examples and we could probably do 10 or 15 variations on each one.
You may even place all the logic in a binary sensor and then just flip-flop the lights from that - which gives you a VERY short automation.
You joined Dec 29 2020 so I’m impressed with what you have accomplished in one month.
Keep trying and if there’s anything you don’t understand about Tom’s automation come back and we’ll explain

1 Like

I have the same problem, VSC tries to guess your indentation, where most code snippets are already correctly indented.
After pasting hit ctrl-z and it undoes the guess - this works about 95% of the time

You may use template sensor with delay_off option on top of your actual motion sensor. This is how i made it, simple automation turns off the light due this sensor’s off event, while it turns off only after 5 mins of motion sensor silence:

- platform: template
  sensors:
     home_livingroom_move:
       device_class: motion
       value_template: "{{ is_state('binary_sensor.home_livingroom_move_sensor', 'on') }}"
       delay_off:
         minutes: 5