Automate Xiaomi Aqara Door/Window sensor

Hello everyone,

I wonder if someone can help me, I’m setting up a home assistant automation to turn on a light when a door is opened. I’m using an Xiaomi aquara window/door sensor to trigger the automation. After spending ages before I found that I wanted “on” and “off” as values rather than “open” & “closed” I got the basics working OK. The trigger which does work is:

entity_id: binary_sensor.window_door_sensor
from: 'off'
platform: state
to: 'on'

However, I want to only trigger the event during darkness hours, rather than having the light turned on every time I open the door. This is where things go wrong, I can’t get the conditional element to work.
This (and variations thereof) is what I’ve tried as a condition:

after: sunset
before: sunrise
condition: sun

However, when I save this I get a notification about an invalid config in an automation. Needless to say, the automation is not running when I add that condition. I’ve also tried using time as a condition, but without success.

I’m running Home Assistant 0.114.0 from docker, which is running under debian.

Anyone have any great ideas?

Show your whole code and format it properly as explained here and then we’ll be able to help you.

Is this better?

The formatting yes, but we need the whole code at once not just parts of it, because indentation is important as well.

Apologies: let me try again:

mode: single
- id: '1596986361544'
  alias: Bedside Lamp on in the evening
  description: Turn on bedside lamp when bedroom door opens
  trigger:
  - entity_id: binary_sensor.window_door_sensor
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: sunset
    before: sunrise
    condition: sun
  action:
  - device_id: 450d079f6b45410191cba58b94f5865c
    domain: switch
    entity_id: switch.bedside_lamp
    type: turn_on

Your condition will never be true, because “after sunset” is from sunset until midnigt and “before sunrise” is from midnight until sunrise. It’s stated in the documentation see here.
You need to separate the conditions into two conditions with the OR operator. There’s an example in the docs:

condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise

It’s a good practice to read the documentation first, a lot of times reading them solves the “issue”. :slightly_smiling_face:

OK: let me have a go with that. Its almost dark here so I’ll test when it is and confirm, but from the sound of it you already know it will work!

Thanks

I’ve marked it as solution as indeed, it did all work very well.