Our home automation project is going along very nicely. We have a few TP-Link smart plugs installed around the house, and have been using their Kasa app for a while. One thing we really like is being able to set automation to turn light on and off. You can trigger that by specific time or sunset and sunrise event with the app, but I run into a limitation.
For example, we usually turn a few lights on around the house when it is getting dark around sunset time, so let’s automate it.
I looked at the Kasa app and was excited to find that you can automated based on sunset, but it only has option to turn lights on at sunset, but does not support any offset time. This is not good enough for us as by the actual sunset time, it tends to be too dark inside the house. So what I’m looking for is to be able to turn lights on few minutes before sunset. I figured that 20 minutes before sunset are usually good in general.
As I have already consolidated most of our home automation using Hass.io running on a Raspberry Pi, I decided to give that a try instead. It turns out to be pretty simple.
TP-Link is a self discovered component in Hass.io, so all you need to do is to add it to the configuration.yaml file, it will discover all your devices automatically.
Once your Hass.io server is restarted, you will see your smart plugs configured as switches, and they will appear in the overview dashboard automatically. Here is an example of 2 smart plugs configured as “My Desk Lamp” and “Traffic Light” in my home:
You can see that the status show one is on and the other is off in this example and you can slide the control to turn the light on and off manually.
Now let’s move on to the automation, turn lights on 20 minutes before sunset each day.
Automation has 3 parts: trigger, condition, and action:
- Trigger is what will trigger this automation
- Condition has to be met
- Action is what you want to happen next
As there are 2 lights that I would like to turn on, it is handy to create a group for that. Here is an example to create a group called “evening_lights”, add the following to the groups.yaml file:
evening_lights: name: Evening Lights entities: - switch.my_desk_lamp - switch.traffic_light view: true all: true
Add the following to the automations.yaml file. This are the instructions to tell Hass.io to trigger 20 minutes before sunset, and perform 2 actions:
- Turn all the smart plugs on in the evening lights group
- send a Pushbullet notification to your phone so that you know lights are turn on.
- id: 'turn lights on before sunset' alias: Turn lights on before sunset trigger: - event: sunset offset: -00:20:00 platform: sun condition:  action: - service: switch.turn_on data: entity_id: - group.evening_lights - service: notify.mypushbullet data: message: Turn lights on before sunset
Please feel free to adjust the automation based on your specific need, click here is you would like to get more examples regarding automation in Hass.io. Another suggestion is to also set an automation to turn the evening lights off at a particular time, like midnight, it is pretty simple.
That’s it! Once I have all these configured, restarted the Hass.io server, and evening lights will be on automatically each day before sunset. Pretty cool!
For more details on Hass.io TP-Link integration, including a list of supported devices, please click here for more details.