logo elektroda
logo elektroda
X
logo elektroda

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

p.kaczmarek2 1377 9
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • 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 11944 posts with rating 9991, helped 572 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21505532
    bratHanki
    Level 38  
    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.
  • ADVERTISEMENT
  • #5 21506093
    Damian_Max
    Level 19  
    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
  • ADVERTISEMENT
  • Helpful post
    #6 21506178
    kmarkot
    Level 29  
    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.
  • #7 21506214
    bratHanki
    Level 38  
    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 ;-) .
  • #8 21506217
    kmarkot
    Level 29  
    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 29  
    @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.😀

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.
Summary generated by the language model.
ADVERTISEMENT