Lights off when no movement

Hi everyone, can someone please help me with some automation?
https://pastebin.com/VXpn3Eny

This is for light in the bathroom, it is controlled by a motion sensor. I am using the motion sensor to turn on the light when walking into the room. But I did not find any good way to turn it off again when there is no movements. The motion sensor can’t see trough glass so it would switch off the lights while showering. I was then thinking it could start a timer for like 10 minutes or more when it does not detect any movementents.

It should start a timer when there is no movement, but I also want it to be cancelled and restart if there is movement within the set time. When the time is reached without any interruption it will then switch off the lights. How could I proceed with this? I was able to make a 10 minutes timer but missing the possibilites to restart the timer and so on. I would be very happy if someone could look into the code for me. Thanks :slight_smile:

Easy peasy. :slight_smile:

timer:
  bathroom_light:
    duration: '00:10:00'
automation:
  - alias: Bathroom light on, cancel timer
    trigger:
      platform: state
      entity_id: binary_sensor.MOTION
      to: 'on'
    action:
      - service: timer.cancel
        entity_id: timer.bathroom_light
      - service: light.turn_on
        entity_id: light.BATHROOM
  - alias: Bathroom light, start timer
    trigger:
      platform: state
      entity_id: binary_sensor.MOTION
      to: 'off'
    action:
      service: timer.start
      entity_id: timer.bathroom_light
  - alias: Bathroom light off
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.bathroom_light
    action:
      service: light.turn_off
      entity_id: light.BATHROOM

Whenever motion is sensed the timer will be started/restarted and the light will be turned on (which does nothing if it is already on.) Then when the timer finally finishes the light will be turned off.

Whenever motion is sensed the light will be turned on and the timer will be canceled (in case it was running.) When motion detection stops the timer will be started. When the timer expires the light will be turned off.

@pnbruckner Thanks.
So my code is correct then?

LOL! I never looked at it. I will…

Well, I don’t see where you’re turning the light on, but the automation that watches for the timer to finish does look right.

It does also make me think that in my solution I should have had to: 'off' for the trigger in the first automation. (I.e., start the timer after motion is no longer detected, not when motion is first detected.) I’ll edit it.

EDIT: Oh, wait. You probably want three automations altogether. One to turn the light on when motion is detected, another that starts the timer when motion detection stops, and the third to turn the light off when the timer expires. Sorry about that. Editing the original suggestion again…

I use this to turn on the lights:

  • alias: Play Sonos bad oppe
    trigger:
    • platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor_4
      from: ‘off’
      to: ‘on’
      condition:
    • condition: time
      after: 06:50:00
      before: ‘23:00:00’
      action:
    • service: media_player.turn_on
      entity_id: media_player.bad_oppe
    • service: light.turn_on
      entity_id: light.fibaro_system_bad_2_etasje
      id: 736d6888bf304e0fb19a315e8a767cf3

Ok. Based on my last update (see above), you’ll want to add canceling the timer to this automation’s actions.

So the code on the top is good to go now?
Thanks again for helping out with this :slight_smile:

I think so. (It was only harder than it should be because I started by saying, “Easy peasy!” :wink:) Just substitute the real entity_id’s. Glad to help. If you have any issues with it, don’t hesitate to say so.

@pnbruckner The code works like a sharm, thanks a lot.
Now I have another automation I want to add.

It should turn on two Philips Hue lights when\after sunset but only if living room lights is on.
Any suggestions about how to proceed with this?

I understand the condition sunset but not sure what I should use for the condtion “only if light is currently on”.

- alias: Turn on lights after sunset
  trigger:
    - platform: sun
      event: sunset
    - platform: homeassistant
      event: start
  condition:
  condition:
    - condition: or
      conditions:
        - condition: sun
          after: sunset
        - condition: sun
          before: sunrise
    - condition: state
      entity_id: light.LIVING_ROOM
      state: 'on'
  action:
    service: light.turn_on
    entity_id: light.HUE1, light.HUE2

EDIT: For those that might come across this later on without reading the rest of the topic, I’ve modified the automation above with a fix discussed below. (Basically I changed a condition for the state of sun.sun being below_horizon to one that uses sun conditions.)

Hmmm…
Not sure what I did wrong, but it did not fire at sunset today :frowning:

Have you already adjusted your time zone and Latitude and longitude in your configuration.yaml?
If you don’t, it is set for the UTC time zone by default.

Yes, I it has correct longitude and latitude there.
latitude: 59.7505520
longitude: 10.8340330

and

time_zone: Europe/Oslo

But I can see that the Sun component is using 12 hour instead of 24 hour, does that matter?
Maybe it is the code. I think it should be
- condition: sun
after: sunset
Instead of:
- platform: sun
event: sunset
Is that correct?

It should not, as you are calling sunset, and not a specific time.
It might be trying to meet too many requirements.
Try using the living room light as the trigger, and after sunset as the condition.
this way, it will check for the sun set every time you turn on the living room light, rather than checking the living room light only once at sunset.

The automation I wrote is triggered either at sunset or when HA starts. I then checks to make sure it is between sunset and sunrise (i.e., below_horizon.) That condition is for when the automation is triggered by HA starting. You originally had the condition for the living room light being on; that’s why that condition is there.

If it did not run at last sunset then that must be because the living room light was not on.

As far as 12hr vs 24hr, that’s only how it’s displayed in the frontend. Internally all times are in UTC.

Hm, that is strange.
The light was on but nothing happened.

The code looks like this now:

  • alias: Turn on lights after sunset if lights is already on in living room
    trigger:
    • platform: sun
      event: sunset
    • platform: homeassistant
      event: start
      condition:
    • condition: state
      entity_id: sun.sun
      state: below_horizon
    • condition: state
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_7
      state: ‘on’
      action:
      service: light.turn_on
      entity_id:
    • light.le_klint_stalampe
    • light.trapp
      data:
      brightness: 140

Please format it so we can read it. Thanks.

- alias: Turn on lights after sunset if lights is already on in living room
  trigger:
    - platform: sun
      event: sunset
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
    - condition: state
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_7
      state: 'on'
  action:
    service: light.turn_on
    entity_id: 
    - light.le_klint_stalampe
    - light.trapp
    data: 
      brightness: 140

I still think you will have best results if you just use the light as the trigger, and below horizon as the condition.
You can also use it as a wait template with a long timeout, waiting for sunset after the light is triggered