Turn Lights On 20 Minutes Before Sunset!

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 some lights on around the house when it is getting dark before sunset time, so it will be great to automate it.

I looked at the Kasa app and was excited to find that you can automate triggered with sunset time each day, 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’ve 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 to configure and a powerful feature.

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 like below, and it will discover all your devices automatically.

tplink:

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.

BTW, I also really like the feature that you can customize each light with your own image, which will make it quite easy to locate the specific light fixture 😃.

You can see that the current status show one light is on and the other is off in this example. You can slide the control to turn the light on and off in the control panel.

Now let’s move on to the automation, the goal is:

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 before action is taking
  • 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”, just 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

Next 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:

  1. Turn all the smart plugs on in the evening lights group
  2. Send a notification to your phone so that you know lights are turned 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

Right now, I don’t have any condition set, but you can easily add condition such as if someone is home or if it is cloudy.

Another good idea is to create an automation to turn the evening lights off at a particular time, like midnight, it is pretty simple and below is the sample code for your automations.yaml file:

- id: 'lights out at midnight'
  alias: Lights Out at midnight
  trigger:
  - at: 00:00:00
    platform: time
  condition: []
  action:
  - service: switch.turn_off
    data:
      entity_id:
      - group.evening_lights
  - service: notify.mypushbullet
    data:
      message: Lights out, goodnight.

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. For more details on Hass.io TP-Link integration, including a list of supported devices, please click here for more details.

That’s it!  Once I have all these configured, restarted the Hass.io server, and evening lights will be on automatically each day 20 minutes before sunset and off at midnight.  Pretty cool!

1,073 total views, 6 views today

No Comments

Post a Comment