Help with automation using Xiaomi Door Sensor

I recently picked up a couple of Xiaomi door sensors which I absolutely love, I set up some simple automation’s to turn on lights when they are triggered. One of the sensors is on a coat closet that triggers a light on the opposite wall when the door is opened. This light is also part of another automation which is triggered by sunset so I was wondering how I do the following:

During the day the light is off so the closet door is opened and the light turns on at 100% and then turns off 10 minutes (I actually have 2 automation to do this, one “on” & one “off”) that was the easy part. I am not sure if it is possible to do this in one single automation though.

After sunset the light comes on at 25%. how do I create an automation that checks the state of the light, turns it too 100% when the closet door is opened then 10 minutes after the door is closed returns the light too its existing state?

I have tinkered with node red but really do not have a great deal of experience with this yet. I should also mention that I just used the automation component of HA so far.
Thanks in advance for any help

You should have one additional automation I think, besides the on/off ones you already have. The additional automation should store the brightness percentage based on the sun. So during the day, this value is set to 0 and during the evening it’s set to 25. Then adjust your off automation to turn the light on (this is not a typo, I really mean on) with the stored brightness. If I read the documentation correctly, turning a light on with 0 brightness will actually turn it off.

Assuming the light is just controlled by these automations (i.e., it’s not manually turned on/off which the automations need to take into account), I think you could do it this way:

automation:
  - trigger:
      platform: sun
      event: sunset
    action:
      service: light.turn_on
      entity_id: light.closet
      data:
        brightness_pct: 25
  - trigger:
      platform: state
      entity_id: binary_sensor.door
      to: 'on'
    action:
      - service: script.turn_off
        entity_id: script.closet_light
      - service: script.closet_light
script:
  closet_light:
    sequence:
      - service: light.turn_on
        entity_id: light.closet
        data:
          brightness_pct: 100
      - delay:
          minutes: 10
      - service: light.turn_on
        entity_id: light.closet
        data_template:
          brightness_pct: >
            {% if state_attr('sun.sun', 'next_setting').split('T')[0]
                  == now().date()|string %}
              0
            {% else %}
              25
            {% endif %}

The first automation turns the light on at 25% at sunset.

The second automation, rather than directly manipulating the light, starts a script instead. It first cancels the script in case it was still running from the last time the door was opened.

The script turns the light on at 100%, waits 10 minutes, and then (using @TheNr1Guest’s suggestion) turns the light on at either 25% (if it’s after sunset), or 0% (to turn it off) if it’s before sunset.

The test in the template checks the sun.sun entity’s next_rising attribute (which, BTW, is a string.) If the date is today’s date then the sun has not set yet. If it’s not (i.e., it’s tomorrow’s date), then the sun has already set today.

1 Like

Thanks for the replies,

Am I right in the assumption that the automation.yaml file will need to be edited for this instead of using the gui ? Also is the script also added to the automation.yaml?

I have something similar for a motion sensor and light between 9:30pm and sunrise, which turns the brightness up when the sensor goes off, and then down after 10mins. Take a look at the below as an example you could tweak. This goes in automations.yaml.

- alias: Front Porch Off Sensor
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000215891c
    to: 'off'
    for:
      minutes: 10
  condition:
    condition: or
    conditions:
    - condition: time
      after: '21:30:00'
    - condition: sun
      before: sunrise
  action:
  - service: light.turn_on
    data:
     entity_id: light.driveway
     brightness: 150

Sorry, when sensor goes ‘on’ I mean

First, when replying, you should use the Reply button under the post to which you’re replying instead of the Reply button at the end of the topic, otherwise the person you’re responding to will not be notified.

Regarding editing YAML files directly vs using the GUI, I don’t use the GUI, so I can’t really answer.

If you want to know how to edit YAML files directly, and how splitting your configuration among multiple files works, check out:

Duly noted regarding replying,

I already have my config files split so do I add the script info to the scripts.yaml ?

I can’t answer because I don’t know the details of how you have your configuration split up. There are many ways to do it. If you read that doc page, and your configuration.yaml file, then you should be able to answer your own question. If there’s something you don’t understand, then please ask a specific question, and/or provide more details, and I’d be happy to help.

Thank you I will give the above a try when I have some time.

I am pretty sure I have my configs split the way the document suggests

image

If that’s the case, then you’d put everything (for the script) but the script: keyword into scripts.yaml, since script: is already in configuration.yaml.

Copy that,

Again thank you for your help. I am still learning HA but have come far in the last 2 months. It is a fantastic platform supported by an even better community

1 Like