Motion sensor automation with additional features

Hello Community!

I would need your help. Namely, I want to create a single (if that is even possible) automation that will allow me to do everything of the following list. I would like to make the automation as efficient as possible. From what I’ve read in the various URIs, this will also require helpers.

  1. when motion detected → light on
  2. if no motion detected for a short time, light off (use the Aqara Motion Sensors without hardware hack)
  3. light should be switched on only from a certain brightness (about 20 lx)
  4. light should have less brightness in the night than in the evening (evening → 70 %, night → 1 %)
  5. before the light goes out in the evening, dim it to 30 % (when motion is detected, dim it back to 70 %)

Now my question: Is this at all possible in an automation? Does anyone of you possibly already have one or the other automation that goes in this direction? Or would NodeRED be the better choice in this regard, since the Aqara sleep without hack for a minute.

Yes.

I have a single automation that performs all of the functions listed in this topic (the automation is also posted in that topic).

2 Likes

Hello! OMG I‘ll try to understand that and report back if I fail completely xD Thank you for your INCREDIBLE work :muscle:

1 Like

If I try to add it, i get the error: message malformed - expected dictionary. And I don´t have a switch, fan and occupancy sensor atm. Will it work if i change the occupancy sensor to the motion sensor but let the 2 minutes there?

is it possible, that you make a blueprint of your version?

The automation serves as an example for anyone to modify for their own use. Feel free to change it to meet your requirements. If you get errors, review your modifications.

Currently, no. I’ve been running two instances of the automation for over a year.

Can you help me to delete all the mentioned things I don‘t have in the code without destroying everything?

The automation references seven entities:

  1. Light
  2. Motion Sensor
  3. Occupancy Sensor
  4. Light Level Sensor
  5. Input Boolean (tracks auto-off function)
  6. Switch (ceiling fan)
  7. Sun (if below_horizon)

List the names of the entities you have.

Light, Motion Sensor, Light Level Sensor. Input boolean ans sun are default available, or am I wrong? i would use a wall switch from philips hue (dim switch). and with switch you mean socket or a switch like my dim switch?

So if I get it right (switch is a switch, no socket and input boolean & sun are default availabe) only the occupancy sensor isn‘t needed in the code. Thank you in advance!

I’ll repeat my question one more time: List the names of the entities you have.

  1. Light: light.badezimmer_deckenleuchte
  2. Motion sensor: binary_sensor.badezimmer_bewegungsmelder_neu
  3. not available
  4. Light level sensor: sensor.badezimmer_beleuchtungsstaerke
  5. input_boolean.badezimmer
  6. if its a switch and no socket: Switch: switch.badezimmer_schalter, if its a socket: switch.badezimmer_steckdose

I hope i got it right

I’ve replaced the example’s entities with the ones you supplied. The example’s occupancy sensor is simply replaced by your existing motion sensor.

What I have not done is adapt it to meet your schedule-based brightness requirements (it simply sets the light to about 50% between sunset and 23:30 and 10% between 23:30 and sunrise). You will need to implement all other modifications to meet your requirements.

- alias: 'Automatic Bathroom Light'
  id: automatic_bathroom_light
  mode: queued
  trigger:
  - id: 'motion'
    platform: state
    entity_id: binary_sensor.badezimmer_bewegungsmelder_neu
    from: 'off'
    to: 'on'

  - id: 'unoccupied'
    platform: state
    entity_id: binary_sensor.badezimmer_bewegungsmelder_neu
    to: 'off'
    for: '00:02:00'

  - id: 'brightness'
    platform: state
    entity_id: light.badezimmer_deckenleuchte
    attribute: brightness

  - id: 'timeout'
    platform: state
    entity_id: input_boolean.badezimmer
    to: 'off'
    for: '00:20:00'

  - id: 'light_off'
    platform: state
    entity_id: light.badezimmer_deckenleuchte
    from: 'on'
    to: 'off'

  - id: 'fan'
    platform: state
    entity_id: switch.badezimmer_schalter
    from:
    - 'on'
    - 'off'
    to:
    - 'on'
    - 'off'

  condition: "{{ is_state('sun.sun', 'below_horizon') }}"

  action:
  - variables:
      light: light.badezimmer_deckenleuchte
      fan: switch.badezimmer_schalter
      auto_off: input_boolean.badezimmer
      occupancy: binary_sensor.badezimmer_bewegungsmelder_neu
      luminance: sensor.badezimmer_beleuchtungsstaerke
      lo: 26
      mid: 125
      brightness: >
        {{ lo if now() > today_at('23:30') or
            now().time() < (state_attr('sun.sun', 'next_rising') | as_datetime | as_local).time() else mid }}
  - choose:
    - conditions:
      - "{{ trigger.id == 'motion' }}"
      - "{{ is_state(light, 'off') }}"
      - "{{ states(luminance) | int(0) < 15 or is_state(fan, 'on') }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: '{{ light }}'
        data:
          brightness: '{{ brightness }}'

    - conditions:
      - "{{ trigger.id == 'unoccupied' }}"
      - "{{ is_state(light, 'on') }}"
      - "{{ is_state(auto_off, 'on') }}"
      sequence:
      - service: light.turn_off
        target:
          entity_id: '{{ light }}'
        data:
          transition: 5

    - conditions:
      - "{{ trigger.id == 'brightness' }}"
      - "{{ is_state(auto_off, 'on') }}"
      - "{{ trigger.from_state.state == trigger.to_state.state }}"
      - "{{ trigger.to_state.attributes.brightness > brightness + 2 or trigger.to_state.attributes.brightness < brightness - 2 }}"
      sequence:
      - service: input_boolean.turn_off
        target:
          entity_id: '{{ auto_off }}'

    - conditions:
      - "{{ trigger.id == 'timeout' or (trigger.id == 'light_off' and is_state(auto_off, 'off')) }}"
      sequence:
      - service: input_boolean.turn_on
        target:
          entity_id: '{{ auto_off }}'
      - condition: "{{ is_state(occupancy, 'off') and is_state(light, 'on') }}"
      - service: light.turn_off
        target:
          entity_id: '{{ light }}'

    - conditions:
      - "{{ trigger.id == 'light_off' }}"
      sequence: []

    - conditions:
      - "{{ trigger.id == 'fan' }}"
      sequence:
      - service: "input_boolean.turn_{{ 'off' if trigger.to_state.state == 'on' else 'on' }}"
        target:
          entity_id: '{{ auto_off }}'

    default: []

Cool automation, thank you very much. I have a question to the part with the variables:

  - variables:
      light: light.badezimmer_deckenleuchte
      fan: switch.badezimmer_schalter
      auto_off: input_boolean.badezimmer
      occupancy: binary_sensor.badezimmer_bewegungsmelder_neu
      luminance: sensor.badezimmer_beleuchtungsstaerke
      lo: 26
      mid: 125
      brightness: >
        {{ lo if now() > today_at('23:30') or
            now().time() < (state_attr('sun.sun', 'next_rising') | as_datetime | as_local).time() else mid }}

Is it possible to use an elseif or elif in the template of brightness? I need a third value (lo for early morning, med for later morning and high for the noon for example). It’s more a general question, but i’m struggelin every time i try to adapt your/such a template with a second condition…

The example uses an inline if but you can change it to use whatever style of if you want.

Could you please give me an example how it should look like, templating is not one of my strength :roll_eyes:

Refer to the first example in the Templating documentation. It contains an if-else-endif.

Sorry to bother you, just a last question. With this shorthanded notation (i think it is called {{value if condition else another value}} ) if - elif - else is not possible, right?

It’s possible:

{{ 'cat' if x == 'fur' else 'bird' if x == 'feathers' else 'fish' if x == 'scales' else 'unknown' }}

Thank you so much, you gave me the missing piece of the puzzle, i now understand the syntax of the shorthanded template (with more than one if in it).
Your answer is really appreciated.

Thanks Mike

Hi! Big thank you :slight_smile:

A last question:

If I want to use the automation without a light switch:

What do I have to delete without destroying everything?