In Home Assistant, an automation is a action that runs one time when a condition is reached. The condition itself is called a trigger.
There are many types of trigger available such as:
- The current time.
- A time pattern, such as every 5 minutes or every hours.
- The state of an entity in the system, such a a button press or a value reached.
Here I’m going to tell you about my automations that are related to the sun, more precisely if the sun rises or set. If you want to react to the current light level outside, it’s the best option: if you look only at the time, you’ll have to keep adjusting your automations as the seasons changes.
All the automations in my system are in YAML code: I have other automations that are not supported by the UI, so it’s more convenient to edit everything from the configuration file. Both examples are automations that are in the chicken coop.
Closing a switch (smart plug) according to the sun
My first automation is a smart plug that is powered off when the sun sets, which closes a door when the chickens are safely inside. They won’t stay outside in the dark, but the sunset condition is reached a bit too early for that and they would get trapped outside. The 1:00:00 offset makes it so the automation starts 1 hour after the sun sets, and not immediately.
So, the type of trigger (the platform) is sun, and is reached when a sunset event happens (+ 1 hour according to the offset). When that trigger is reached, the action is executed: in the example below, the switch plug_4 in my system is turned off.
- alias: "Close the door after sunset" mode: single trigger: - platform: sun event: sunset offset: 01:00:00 action: - service: switch.turn_off entity_id: switch.plug_4
This would work with any “switch” device, but for this automation I’m using a Sinopé Zigbee smart plug:
Dimming a light according to the sun
My second automation dims the light in the coop over time as the sun goes down. The chickens need to sleep in the dark so they have good egg production, but they would get caught on the floor if the light would turn off all at once. With this automation, as the light dims, the chicken have time to settle down on their perch so they are ready when the light closes totally.
A simpler automation not shown here with the event sunrise turns the light on at 100% brightness in the morning.
Since there are many dimming steps, this is in fact 4 separate automations with different offset times. Otherwise, the trigger is very similar to the previous example.
On the other hand, the action is a bit different since it’s a dimmer and not a switch: I also need to send the brightness level to set. Also, not all dimmers use the same data, you may have to experiment a bit to find the right format to use for your hardware (all possible values are in the Home Assistant documentation).
- alias: "Close light 1" mode: single trigger: - platform: sun event: sunset offset: 01:00:00 action: - service: light.turn_on entity_id: light.coop_light data: brightness_pct: 50 - alias: "Close light 2" mode: single trigger: - platform: sun event: sunset offset: 1:15:00 action: - service: light.turn_on entity_id: light.coop_light data: brightness_pct: 30 - alias: "Close light 3" mode: single trigger: - platform: sun event: sunset offset: 01:30:00 action: - service: light.turn_on entity_id: light.coop_light data: brightness_pct: 15 - alias: "Close light 4 (done)" mode: single trigger: - platform: sun event: sunset offset: 01:45:00 action: - service: light.turn_on entity_id: light.coop_light data: brightness_pct: 0
The hardware I used for this a a dimmable light, in my case a Sinopé Zigbee smart dimmer: