Light Up Your Home with Artificial Intelligence: Find Out How!
Articolo

Light Up Your Home with Artificial Intelligence: Find Out How!

Find out how intelligent automation can transform your home into a smart environment. Turn the lights on and off based on your movement.

Intelligent Automation

In the age of digitalization, “intelligent automation” is becoming increasingly central to our daily lives. From simple household tasks to more complex tasks, automation is transforming the way we interact with our environment. In this article, we will explore how we can use intelligent automation to improve energy efficiency and convenience in our homes. Imagine walking into a room and the light turns on automatically, detecting your presence. And when you leave, the light turns off by itself, saving energy. This is no longer a dream, but a reality that we can realize thanks to intelligent automation. Using Home Assistant, an open-source platform for home automation, and a bit of YAML code, we can create a system that turns lights on when it detects motion and turns them off when the environment is clear. And to further save energy, we can program the system to only work from dusk to dawn, when light is really needed. Read on to find out how we can achieve this intelligent automation and make our homes smarter and more energy efficient.

We turn on the light automatically from dusk to dawn

 
Let's create an automation that turns on the light when the motion sensor detects movement,
ensuring that this only happens in the period of time from sunset to sunrise.
 
automation: - alias: Turn on the light when motion is detected   trigger:     platform: state     entity_id: binary_sensor.your_motion_sensor     from: 'off'     to: 'on'   condition:     condition: or     conditions:       - condition: sun         after: sunset       - condition: sun         before: sunrise   action:     service: light.turn_on     target:      entity_id: light.your_light   - alias: Turn off the light when no motion is detected   trigger:     platform: state     entity_id: binary_sensor.your_motion_sensor     from: 'on'     to: 'off'     for:       minutes: 5   condition:     condition: or     conditions:       - condition: sun         after: sunset       - condition: sun        before: sunrise   action:     service: light.turn_off     target:       entity_id: light.your_light
 
In this example, the automation only activates if the motion sensor detects movement and if it is after sunset or before sunrise.
When the motion sensor no longer detects movement for 5 minutes, the automation turns off the light, but only if it is after sunset or before sunrise.
Remember to replace binary_sensor.your_motion_sensor and light.your_light with your real entity IDs.
If you want a different delay before the light turns off, you can change the value in for: minutes: 5 to the desired number of minutes.
Be sure to check your YAML syntax and test your automation to make sure it works as expected.
 
However, there is a small problem with this automation and that is if the light turns on just before dawn, the automation to turn off the light may not activate if it is already dawn.
 

How to overcome the shutdown problem if dawn has already "broken"?

 
A possible solution could be to remove the light off condition. This way, the light will turn off regardless of whether it is day or night,
as long as there is no movement detected for a certain period of time. Here's what automation might look like:
 
automation: - alias: Turn on the light when motion is detected   trigger:     platform: state     entity_id: binary_sensor.your_motion_sensor     from: 'off'     to: 'on'   condition:     condition: or     conditions:       - condition: sun         after: sunset       - condition: sun         before: sunrise   action:     service: light.turn_on     target:      entity_id: light.your_light - alias: Turn off the light when no motion is detected   trigger:     platform: state     entity_id: binary_sensor.your_motion_sensor     from: 'on'     to: 'off'     for:       minutes: 5   action:     service: light.turn_off     target:       entity_id: light.your_light
 
 
In this example, the automation to turn on the light is activated only if the motion sensor detects movement and if it is after sunset or before sunrise.
However, the automation to turn off the light is activated when the motion sensor no longer detects movement for 5 minutes, regardless of whether it is day or night.
Remember to replace binary_sensor.your_motion_sensor and light.your_light with your real entity IDs.
If you want a different delay before the light turns off, you can change the value in for: minutes: 5 to the desired number of minutes.
Even in this case however there is a problem, if the light is turned on manually, the current automation would still turn it off if no movement is detected for a certain period of time
 

And if the light was turned on manually, how could we prevent it from being turned off by the automation?

 
To avoid this, you could consider using a boolean input or a status sensor to track whether the light was turned on by the automation or manually.
Then, we could add a condition to the shutdown automation to control this state.
So we should have something like this
 
input_boolean: light_automation: automation: - alias: Turn on light when motion is detected   trigger:     platform: state     entity_id: binary_sensor.your_motion_sensor     from: 'off'     to: 'on'   condition:     condition: or     conditions:       - condition: sun         after: sunset       - condition: sun         before: sunrise   action:    - service: light.turn_on       target:         entity_id: light.your_light     - service: input_boolean.turn_on       target:         entity_id: input_boolean.light_automation   - alias: Turn off the light when no motion is detected   trigger:     platform: state     entity_id: binary_sensor.your_motion_sensor     from: 'on'    to: 'off'     for:       minutes: 5   condition:     - condition: state       entity_id: input_boolean.light_automation       state: 'on'   action:     - service: light.turn_off       target:         entity_id: light.your_light     - service: input_boolean.turn_off       target:         entity_id: input_boolean.light_automation
 
In this example, when the automation turns on the light, it also sets input_boolean.light_automation to 'on'.
When intelligent automation turns off the light, it first checks whether input_boolean.light_automation is 'on'.
If it is, turn off the light and set input_boolean.light_automation to 'off'.
If input_boolean.light_automation is 'off', it means that the light was turned on manually, so the automation does not turn it off.
Again, remember to replace binary_sensor.your_motion_sensor and light.your_light with your actual entity IDs.
Be sure to check your YAML syntax and test your automation to make sure it works as expected.
 
We hope that what is described above can be useful to you in some way.
 
Until next time .. !