Lights - motion + evening mode + manual

Hi,

I have a set of lights out the front, and recently had an idea to have them turn on in dim mode at sunset and turn off around 11pm.
Then on top of that I have a motion sensor that will cause the lights to turn to full brightness for 5 minutes between sunrise and sunset.
Then thirdly, I want to be able to manually switch the lights on (which will will override the motion off, or evening mode)

Looking through this, was going to be quite complex to do with automations, probably alot required because of the different state changes. What would be the best way to approach this?

Mostly worked this out, the trickiest part is the light switch, its coming through an MQTT gateway, and I can’t tell if its been manually switched, or its been switched on by automation (via my sunset rules).

Hello @winter! How are you?

I’ve just installed a few motion sensors (Xiaomi Aqara) and implemented more or less like your idea!

I’ve created an automation for every room that the motion sensor is installed, like this:

- alias: Motion - bedroom light
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000120af26
    to: 'on'
  action:
    - condition: state
      entity_id: light.suite_tv
      state: 'off'
    - service: light.turn_on
      entity_id: light.suite_tv
    - wait_template: "{{is_state('binary_sensor.motion_sensor_158d000120af26', 'off')}}"
    - delay: 00:00:30
    - wait_template: "{{is_state('binary_sensor.motion_sensor_158d000120af26', 'off')}}"
    - condition: state
      entity_id: light.suite_tv
      state: 'on'
    - service: light.turn_off
      entity_id: light.suite_tv

The turn on and off automation has a line to check if the light is on already so the automation doesn’t fire up if the light is on manually, thus creating an override for the script, as you suggested, right? Also there is another line that checks again if the light had been switched of manually in between the automation run just before the final switch off command.

After that, I’ve created a couple of automatons for sunset and sunrise, so I can disable the automation itself if there is light outside. Working with on/off automation makes it simple:

- alias: Routine - Sunset
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation | int }}'
    below: 5
  action:
    - service: automation.turn_on
      entity_id:
        - automation.luzes_da_casa__banheiro_social
        - automation.luzes_da_casa__banheiro_suite
        - automation.luzes_da_casa__cozinha
        - automation.luzes_da_casa__quarto
        - automation.luzes_da_casa__suite
- alias: Routine - Sunsrise
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation | int }}'
    above: 5
  action:
    - service: automation.turn_off
      entity_id:
        - automation.luzes_da_casa__banheiro_social
        - automation.luzes_da_casa__banheiro_suite
        - automation.luzes_da_casa__cozinha
        - automation.luzes_da_casa__quarto
        - automation.luzes_da_casa__suite
        - automation.luzes_da_casa__sala

I think that working with the numeric state of the sun elevation woks best.

Maybe this idea can help you by creating your dimmed lights actions.

Hope it helps!

Cheers mate!

1 Like

i know this is a old post, but I hope somebody could point me in the right direction.

Im trying to use the above code for 2 motion sensors i have in my living room and dining room.
I dont want the dim screen to come on if the light.flower_lamp is on and after testing that part is working good.

but if the dim script is activated my lights wont turn off again, what am I missing?

- alias: Motion - Livingroom
  trigger:
    platform: state
    entity_id:
      - binary_sensor.motion_sensor_158d00023e32ee
      - binary_sensor.motion_sensor_158d000237550f
    to: 'on'
  action:
    - condition: state
      entity_id: switch.flower_lamp
      state: 'off'
    - service: script.turn_on
      entity_id: script.livingroom_motion_dim
    - wait_template: "{{is_state('binary_sensor.motion_sensor_158d00023e32ee', 'off')}}"
    - wait_template: "{{is_state('binary_sensor.motion_sensor_158d000237550f', 'off')}}"
    - delay: 00:00:30
    - wait_template: "{{is_state('binary_sensor.motion_sensor_158d00023e32ee', 'off')}}"
    - wait_template: "{{is_state('binary_sensor.motion_sensor_158d000237550f', 'off')}}"
    - condition: state
      entity_id: switch.flower_lamp
      state: 'on'
    - service: script.turn_on
      entity_id: script.livingroom_motion_dim_off

Thanks!

Hello @Corey_Maxim! How are you today?

That is an old post indeed :slight_smile:

I’ve had many upgrades on this automation and now I use like this for months now:

  - alias: MuvRH 303 - Luz do banheiro
    trigger:
      platform: state
      entity_id: binary_sensor.motion_sensor_158d0001ad6c20
      to: 'on'
    condition:
      condition: state
      entity_id: light.muv303banheiro
      state: 'off'
    action:
      - service: light.turn_on
        entity_id: light.muv303banheiro
      - delay: 00:00:03
      - wait_template: "{{ ((is_state('light.muv303banheiro' , 'off')) or ((((as_timestamp(strptime(states.sensor.date__time.state, '%Y-%m-%d, %H:%M')) - as_timestamp(states.binary_sensor.motion_sensor_158d0001ad6c20.last_updated) ) / 60) | int) >= states.input_number.timmer_muvrh_303_banheiro.state | int -1 and is_state('binary_sensor.motion_sensor_158d0001ad6c20' , 'off'))) }}"
      - condition: state
        entity_id: light.muv303banheiro
        state: 'on'
      - service: light.turn_off
        entity_id: light.muv303banheiro

This wait template took me hours of testing a lot of help from the amazing people here in the community.

You need the time and date component to use this template.

Cheers!