Automated night light based on movement

Hello,

I’m trying to get two automations to work in latest version of HA.

I want to turn a scene on if motion is detected after 23:00:00 and then turn it off after 5 minutes.
Somehow i’m getting configuration errors in HA when saving this, help is appreciated!

Log error:

2017-08-20 14:04:14 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->trigger->1->for. (See /home/homeassistant/.homeassistant/configuration.yaml, line 154). Please check the docs at https://home-assistant.io/components/automation/

Here’s my automations that should control it:

#lights on
  - id: motion_light_natt
    alias: Nattlampa_Övervåning
    trigger:
      - platform: state
        entity_id: sensor.sn3_pir
        to: 'motion detected'
      - platform: time
        at: '23:00:00'
    action:
      service: scene.turn_on
      entity_id: scene.Nattlampa_On

#lights off
  - id: motion_light_natt_off
    alias: Nattlampa_off
    trigger:
      - platform: state
        entity_id: sensor.sn3_pir
        to: 'standby'
      - platform: time
        at: '23:00:00'
        for:
          minutes: 5
    action:
      service: scene.turn_on
      entity_id: scene.Nattlampa_Off

Here are the scene config chapter:

  - name: Nattlampa_On
    entities:
        switch.fonster_overvaning_dator:
            state: on
        switch.overvaning_balkong_fonster:
            state: on
        light.a_skrivbordslampa:
            state: on

  - name: Nattlampa_Off
    entities:
        switch.fonster_overvaning_dator:
            state: off
        switch.overvaning_balkong_fonster:
            state: off
        light.a_skrivbordslampa:
            state: off

I do something similar, this might help. Use conditions to check whether the trigger should do anything

Automation

  - alias: Lights on when motion on between midnight and sunrise
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000113d11c
        to: 'on'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: state
        entity_id: group.lamp
        state: 'off'
      - condition: state
        entity_id: media_player.samsung_tv_remote
        state: 'off'
      - condition: time
        after: '00:00:00'
      - condition: sun
        before: sunrise
    action:
      - service: homeassistant.turn_on
        entity_id: scene.nightlight
  - alias: Nightlight off after 15 min when motion off and past 12 midnight
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000113d11c
        to: 'off'
        for:
          minutes: 15
          seconds: 00
    condition:
      condition: and
      conditions:
        - condition: time
          after: '00:00:00'
        - condition: sun
          before: sunrise
          before_offset: '00:30:00'
        - condition: state
          entity_id: media_player.samsung_tv_remote
          state: 'off'
        - condition: state
          entity_id: group.lamp
          state: 'on'
    action:
      - service: homeassistant.turn_off
        entity_id: group.lamp
      - service: homeassistant.turn_off
        entity_id: light.kitchen_main

Scene

  - name: Nightlight
    entities:
        group.lamp:
          state: on
          transition: 2
          rgb_color: [143,143,143]
          brightness: 15
        light.kitchen_main:
          state: on
          transition: 2
          rgb_color: [143,143,143]
          brightness: 15

I understand sometimes Ive used conditions that might duplicate each other but this gives you an example.

1 Like

It can’t be 23:00 for 5 minutes.
Use this instead:

    trigger:
      - platform: state
        entity_id: sensor.sn3_pir
        to: 'standby'
        for:
          minutes: 5

Hello,

Thanks for your reply.

I used your automation and made this:

#lights on, motion detected
  - id: motion_nattlampa_pa
    alias: Nattlampa_on
    trigger:
      - platform: state
        entity_id: sensor.sn3_pir
        to: 'motion detected'
    condition:
      condition: and
      conditions:
      -  condition: state
         entity_id: sun.sun
         state: 'below horizon'
      -  condition: time
         after: '23:00:00'
      -  condition: sun
         before: sunrise
    action:
      - service: homeassistant.turn_on
        entity_id: scene.Nattlampa_On

#lights off, no motion detected
  - id: motion_nattlampa_av
    alias: Nattlampa_off
    trigger:
      - platform: state
        entity_id: sensor.sn3_pir
        to: 'standby'
        for:
          minutes: 5
          seconds: 00
    condition:
      condition: and
      conditions:
        - condition: time
          after: '23:00:00'
        - condition: sun
          before: sunrise
    action:
      - service: homeassistant.turn_on
        entity_id: scene.Nattlampa_Off
1 Like

Thank you all. Case closed!

Hey, may I ask your support?

This is my automation config:

- alias: Accendi Luce Ingresso se rilevato movimento
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00011c7d45
      to: 'on'
      for:
        minutes: 5
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: state
      entity_id: light.gateway_light_286c0788ab7e
      state: 'off'
    - condition: sun
      before: sunrise
  action:
    - service: homeassistant.turn_on
      entity_id: light.gateway_light_286c0788ab7e

If I trigger the light goes on but it doesn’t work on night.

Any hint?

Thank you in advance!

The motion sensor has to be “on” for 5 minutes, is that what you want?

Also, with before: sunrise, it’ll only operate between midnight and sunrise. You can just drop the sunrise piece since you’re already checking if it’s below the horizon.

No the light should be on for 5 minutes :sweat_smile:

How can I set it?

Hey!

I fixed! :slight_smile:

  • alias: Accendi Luce Ingresso se rilevato movimento
    trigger:

    • platform: state
      entity_id: binary_sensor.motion_sensor_158d00011c7d45
      to: ‘on’
      condition:
      condition: and
      conditions:
    • condition: state
      entity_id: sun.sun
      state: ‘below_horizon’
    • condition: state
      entity_id: light.gateway_light_286c0788ab7e
      state: ‘off’
      action:
    • service: homeassistant.turn_on
      entity_id: light.gateway_light_286c0788ab7e
  • alias: Spegni Luce Ingresso se rilevato movimento
    trigger:

    • platform: state
      entity_id: binary_sensor.motion_sensor_158d00011c7d45
      to: ‘off’
      for:
      minutes: 1
      action:
    • service: homeassistant.turn_off
      entity_id: light.gateway_light_286c0788ab7e