logo elektroda
logo elektroda
X
logo elektroda
Dostępna jest polska wersja

Czy wolisz polską wersję strony elektroda?

Nie, dziękuję Przekieruj mnie tam

How do I automatically switch on the light in a room? Triggering a door sensor - Home Assistant

p.kaczmarek2 2202 9

TL;DR

  • A Home Assistant automation turns a light on when a Zigbee door sensor opens and off when it closes.
  • The setup uses Home Assistant's three-step automation flow: When, And if, and Then do, with the relay acting as the light switch.
  • The automation can be previewed in YAML, and it is created after pairing the door sensor and relay with Home Assistant.
  • The two-scenario test shows the light switching on as the door opens and switching off as it closes.
  • Rooms and bathrooms may need extra logic, such as a presence sensor or timer, because door-close shutdown alone can be wrong.
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
📢 Listen (AI):
  • Door sensor and Zigbee Smart Switch relay connected to a light bulb. .
    How to create a simple automation in Home Assistant ? How to trigger an event with a door sensor? How to automatically turn on a light? Here I will try to show you - step by step.

    The topic assumes that we already have a Home Assistant set up with a paired door sensor and relay. In this case, I have used Zigbee-based solutions, but from the HA's point of view, this is not relevant. Nevertheless, in the context of battery-powered devices such as temperature sensors, door openers, window sensors, etc, I strongly recommend Zigbee. It is simpler to use and the price is not much higher than for WiFi-based solutions. I value WiFi-based solutions for the fact that after changing the firmware , a lot can be realised on them and without a central server, but in terms of battery life, Zigbee always wins in my opinion.

    For the automation presented here, two devices will be used - a relay and a door-opening sensor, both controlled by Zigbee:
    Door sensor configuration page in Home Assistant. .
    Settings interface for Zigbee device - RelayBulb. .
    I assume we have already paired and connected the whole thing, and the HA can see both pieces of equipment. The relay will switch on the bulb.
    Zigbee Smart Switch connected to a light bulb and door sensor. .
    Now I will show how you can create an automation based on these.
    Open HA settings, go to Automations & Scenes:
    Screenshot of Home Assistant interface with automation creation option. .
    A new automation is created by the "Create automation" button:
    Create Automation button in Home Assistant interface. .
    There we have ready-made scenarios or the creation of an automation from 0:
    Screenshot of the Home Assistant interface showing the create new automation menu. .
    The automations consist of three sections:
    - When - the main event trigger, it initiates the whole action
    - And if - additional conditions that must be met for the automation to execute
    - Then do - a list of operations to be performed by the HA when the automation is triggered (e.g. turning on a light)
    Home Assistant automation interface with When, And if, Then do sections. .
    In this particular example, in the "When" section we first select the door sensor and then select its "Opened" event, separately we have the "Closed" option and a possible event time.
    In the "And if" section I didn't select anything, but here you could additionally use, for example, an occupancy sensor - if the door is closed and no one is in the room, turn off the light.
    It's worth remembering that you can preview our automation in YAML format in the side menu:
    Context menu in the Home Assistant interface with an option to edit in YAML. .
    Screenshot of automation in Home Assistant .
    It remains to fill in the action to be performed. There again, we first select the object and then the operation on it. For example, for turning off a light bulb:
    Home Assistant automation interface with settings for door sensor and relay. .
    Finally, we call our automation:
    Home Assistant automation creation screen with door sensor .
    All done, but it is still worth mentioning side possibilities. Once the automation has been created, it can of course be edited, but also duplicated (to create a variant), enabled, disabled, forced firing (convenient for testing) as well as checking the call history:
    Screenshot of automation management menu in Home Assistant with management options. .

    Left to test - below is a video of two automations - turning the light on when the door is open and off when it is closed.
    Door sensor and Zigbee relay used for automating light activation.
    This is how you can do simple automation in Home Assistant . The final version of the scenarios, of course, depends on what you want to achieve, because if, for example, you open the pantry door, it does indeed make sense to switch on the light as long as it is open, but in the case of, for example, a room or a bathroom, you may need to create a more complex logic - it does not make sense to switch off the light when someone is inside.... then it would be a good idea to use the "And if" condition and link the action also with the presence sensor, or you can simply assume that the light goes off by itself after a given time or you turn it off "classically", with a button.... perhaps I will try to discuss such a more advanced mechanism in the next topic, and in the meantime I will ask - have you tried to implement some simple automation in Home Assistant, and if so, on what principle of operation? Door sensor, motion sensor or maybe something else? .

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 14237 posts with rating 12141, helped 647 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21505532
    bratHanki
    Level 39  
    I'm not familiar with these modern 'conveniences' so I have an objection as the system turns on the bulb even though it's light out. Will there be an additional light sensor to block the system from operating during the day?
  • #3 21505539
    LA72
    Level 41  
    >>21505532 .
    A circuit with PIR and twilight sensor can be included.
  • ADVERTISEMENT
  • #4 21505555
    p.kaczmarek2
    Moderator Smart Home
    Good question. In this situation, add a condition to the "And if" shown in the automation creation screenshot:
    Dialog window for adding a condition in automation. .
    This additional condition can be either the state of another device (presence detection) or, for example, the current time of day or the position of the sun:
    Screenshot of automation settings for time and location. .
    If we have the light level in numeric format (rather than presence true/false) then numeric state can also be used:
    Configuration screen for the Numeric state condition in automation. .
    This can be useful if the room has a window, for example. On the other hand, in the case of the pantry, bathroom and toilet, we don't have a window, so we always turn the light on whether it's day or night. That's the layout of the flat.
    Helpful post? Buy me a coffee.
  • #5 21506093
    Damian_Max
    Level 21  
    And taking this reasoning further, let's assume this light is in front of the garage and the opening sensor is on the gate, is there / how would you propose to handle the additional lighting of this light on cloudy days?
    One solution is of course a light sensor mounted outside, and would it be possible to do this based on weather information?

    @p.kaczmarek2 I really enjoy your reviews! I have an additional question, you don't have to answer, but besides them, do you work full time? Because the amount of material you publish brings you to your knees xD
  • Helpful post
    #6 21506178
    kmarkot
    Level 30  
    In Home Assistant there is an entity Sun Sun in which you can add as a condition after sunset and before sunrise.
    I also use a photovoltaic system as a light sensor .
    I made an automation that closes the curtains in the TV room on the west side of the house.
    The trigger is an Android TV which when it changes state from off to on .
    Conditions
    When it is after 2 pm
    When the production from the PV is greater than 1000W
    Make
    Close the curtain in the TV room.

    Added after 9 [minutes]:

    And here's an example of another automation

    Screenshot of Home Assistant automation for turning on entryway light when the front door is opened.

    Added after 12 [minutes]:

    Yet another automation that sends a notification when a door is opened to an Android TV.

    Home Assistant automation interface for door opening notifications.
  • ADVERTISEMENT
  • #7 21506214
    bratHanki
    Level 39  
    To all these "miraculous" facilities I would also add a remote measurement of the owner's temperature because it can happen that everything works, the TV turns on, the curtains and lights work , in a word the house is alive, only the owner died a month ago ;-) .
  • ADVERTISEMENT
  • #8 21506217
    kmarkot
    Level 30  
    Unfortunately it doesn't quite work like that :) .
    Someone has to turn on the TV or open the door to make a certain response.
  • #9 21506222
    lexx_
    Level 12  
    From the "classic" automations, it is the control of lighting when movement is detected by a PIR or presence detector, with additional conditions in the form of light intensity or time of day, switching on lighting controlled by an astronomical clock, and, of more interesting ones, voice announcements about the end of washing (monitoring the power consumed by the washing machine), raising the roller blinds when the alarm rings on the phone, switching off the central heating furnace or closing the thermostatic head when the window is opened (and switching it on when the window is closed), switching the room temperature sensor controlling the central heating furnace to another zone at night, or automatic gate control (closing and opening) when a car leaves or enters a designated zone (about 50m from the house). All in all, automation in HA offers great possibilities, limited only by the imagination.
  • #10 21506250
    kmarkot
    Level 30  
    @bratHanki and seriously I wouldn't measure temperature but there are other options
    Mobile phone is logged into wifi no change in status e.g. light switches for a certain amount of time.
    Sends a notification to another phone.
    The problem could be the phone is discharged which would mean the householder is away.
    Another way may be to install an alarm system which sends a notification when it is not in standby and when there is no reaction from motion sensors.
    There are a lot of possibilities and we are only limited by our imagination.😀
📢 Listen (AI):

Topic summary

✨ The discussion focuses on creating a Home Assistant automation to automatically turn on a light when a door sensor is triggered. The setup assumes a paired door sensor and relay, preferably Zigbee-based for better battery life compared to WiFi devices. To avoid unnecessary lighting during daylight, it is recommended to add conditions such as light level sensors, presence detection, or time-based triggers (e.g., after sunset and before sunrise using the Sun entity in Home Assistant). Additional enhancements include integrating PIR motion sensors, twilight sensors, or external weather data to adjust lighting on cloudy days. Examples of automations include controlling curtains based on photovoltaic system output and sending notifications when doors open. The discussion also highlights broader automation possibilities like voice announcements, heating control linked to window sensors, and automatic gate operation. Various strategies for presence detection and safety notifications are suggested, emphasizing the flexibility and extensive capabilities of Home Assistant automations.
Generated by the language model.

FAQ

TL;DR: With 3 automation sections and 2 devices, “Zigbee always wins” for battery sensors. This FAQ helps Home Assistant users build a door-triggered light, add night-only conditions, preview YAML, and choose better logic for rooms where simple door-open = light-on rules fail. [#21505220]

Why it matters: A basic Home Assistant automation works fast for pantries and closets, but better conditions prevent pointless switching in bright rooms, bathrooms, and outdoor areas.

Option Battery-powered sensors Setup effort Flexibility noted in thread Best fit
Zigbee Preferred Lower Works well in HA; no special firmware step mentioned Door, window, temperature, and other battery devices
WiFi with custom firmware Weaker for battery life Higher Can do a lot after firmware change, even without a central server Mains-powered devices and custom standalone logic

Key insight: Use the door sensor as the trigger, then use And if to block bad activations. Time, sun position, presence, or numeric light level make the difference between a demo automation and a practical one.

Quick Facts

  • The example automation uses 2 Zigbee devices: one door-opening sensor and one relay that switches the bulb. [#21505220]
  • Home Assistant automation in the visual editor is split into 3 sections: When, And if, and Then do. [#21505220]
  • A practical curtain rule in the thread triggers after 2 pm and only when photovoltaic production is greater than 1000 W. [#21506178]
  • One geofencing-style gate idea acts when a car enters or leaves a zone about 50 m from the house. [#21506222]

How do I create a simple Home Assistant automation that turns a light on when a Zigbee door sensor reports the door as opened?

Create one automation with the door sensor as the trigger and the relay or bulb as the action. 1. Open Settings > Automations & Scenes. 2. Click Create automation and pick the door sensor in When, then choose Opened. 3. In Then do, select the relay or light entity and choose turn on. The thread example uses 2 devices: a Zigbee door sensor and a Zigbee relay. [#21505220]

What do the "When", "And if", and "Then do" sections mean in a Home Assistant automation?

They are the trigger, conditions, and actions of the automation. When starts the rule, such as a door changing to Opened. And if adds extra checks, such as presence, time of day, or light level. Then do runs one or more actions, such as turning a relay on or off. The thread explicitly describes these 3 sections as the core structure of a Home Assistant automation. [#21505220]

How can I make Home Assistant turn the light off when the door is closed, using a second automation or a matching trigger?

Create a second automation that mirrors the first one but uses the Closed event and a turn off action. The thread shows both states are available separately for the same door sensor: Opened and Closed. It also demonstrates two automations in testing: light on when the door opens and light off when it closes. That simple pair works well for a pantry or similar space. [#21505220]

Which is better for battery-powered door and window sensors in Home Assistant: Zigbee or WiFi with custom firmware?

Zigbee is the better fit for battery-powered door and window sensors in this thread. The thread author directly says, “Zigbee always wins” for battery life, while also noting that WiFi devices with changed firmware can do a lot and can work without a central server. Use WiFi custom firmware when flexibility matters more than battery endurance. Use Zigbee when the sensor runs on batteries and must stay simple. [#21505220]

How do I add a condition in Home Assistant so a door-triggered light only turns on at night and not during the day?

Add a condition in And if that checks time, sun position, or another sensor before the action runs. The thread shows this extra condition can use the current time of day, the position of the sun, or another entity state. That blocks daytime switching even when the door sensor still reports Opened. This is the clean fix when a user objects that the bulb would also turn on in daylight. [#21505555]

What is the Sun entity in Home Assistant, and how do I use it for conditions like after sunset and before sunrise?

Use the Sun entity as a condition that allows the automation only after sunset or before sunrise. "Sun entity" is a Home Assistant entity that tracks the sun’s state, exposing day-night status and solar position, so automations can check conditions like after sunset and before sunrise without a separate light sensor. In the thread, one example explicitly recommends Sun Sun for rules that should run only at night. [#21506178]

How does the Numeric State condition work in Home Assistant for automations based on light level or other measured values?

Numeric State runs the automation only when a measured value crosses a chosen threshold. "Numeric State" is a Home Assistant condition that evaluates a sensor as a number, not just true or false, so you can trigger or block actions using thresholds such as brightness, temperature, or power. The thread recommends it when light level is available in numeric form, especially in rooms with a window. [#21505555]

What is a PIR sensor, and how is it different from a presence detector in lighting automations?

A PIR sensor detects motion, while a presence detector is used for occupancy-style checks in automation logic. "PIR sensor" is a passive infrared motion sensor that reacts to movement through heat changes, while a presence detector aims to confirm that someone remains in the space, not only that they moved once. The thread mentions PIR in classic lighting control and separately suggests presence detection to avoid switching lights off on someone still inside. [#21505539]

How would you handle automatic lighting for a garage gate on cloudy days in Home Assistant, especially if you want brightness to depend on weather conditions?

Use an outdoor light sensor if you need brightness-based switching on cloudy days. The thread raises weather-based logic as a possibility, but the concrete methods given are stronger: use a real light sensor, the sun position, time of day, or numeric light level. For a garage gate, a door trigger plus a light threshold gives a clearer rule than open/close alone. That avoids turning lights on when daylight is still strong. [#21506093]

What are the best ways to use a light sensor, time of day, sun position, or weather data to block unnecessary light activation in Home Assistant?

Start with the simplest reliable condition and add complexity only if needed. Use sun position for a binary night rule, time of day for fixed schedules, and Numeric State when you have measured brightness. Use a dedicated light sensor when clouds matter. The thread also gives a practical example where some rooms, such as pantry, bathroom, and toilet, have no window, so the light may turn on regardless of day or night. [#21505555]

Where do I preview or edit the YAML for an automation created in the Home Assistant visual editor?

Open the automation and use the side menu to preview its YAML. The thread explicitly says you can view the automation in YAML format from the side menu after building it in the visual editor. That is useful when you want to confirm the trigger, condition, and action structure or duplicate the logic in a variant. The same screen also supports later editing, enabling, disabling, and test firing. [#21505220]

Why might a door-opened automation be a bad fit for a bathroom or room light, and what logic works better there?

It can fail because the light may switch off while someone is still inside. The thread says a pantry door is a good fit, but a room or bathroom often needs more logic. Better options are a presence condition in And if, a timeout that turns the light off after a set period, or manual switch-off with a button. This is the main edge case where simple door open = on, door closed = off logic breaks down. [#21505220]

What are some practical Home Assistant automation ideas beyond door sensors, such as washing machine notifications, curtain control, or heating shutdown when a window opens?

Useful ideas include curtain control, washer-finished announcements, and heating shutdown when a window opens. The thread lists voice announcements when washing ends by monitoring washer power, blinds closing when an Android TV changes from off to on, central-heating shutdown or thermostat closure on window open, nighttime zone switching for heating control, and automatic gate control near a 50 m home zone. These examples show HA goes far beyond simple lighting. [#21506222]

How can photovoltaic power production be used as an indirect light sensor in a Home Assistant automation?

Use PV generation as a brightness proxy and gate the automation with a power threshold. The thread gives a real example: close curtains only after 2 pm and when photovoltaic production is greater than 1000 W. That treats solar output as an indirect daylight sensor. It works best when the panels track available sunlight well enough for the room or façade you want to automate. [#21506178]

What troubleshooting steps should I follow if a Home Assistant door sensor automation works in tests but does not switch the relay or bulb reliably in real use?

Check the trigger event, the target entity, and the real-world conditions first. 1. Use the automation’s forced firing to verify the action path. 2. Check the call history to see whether the rule triggered on Opened or Closed. 3. Confirm the selected relay or light entity is the same one that actually controls the bulb. The thread also notes you can duplicate, enable, disable, and edit automations, which helps isolate whether the fault is the trigger, condition, or action. [#21505220]
Generated by the language model.
ADVERTISEMENT