Christmas Light Switch Automation

Setup Information:

  • Raspberry Pi 3
  • Home Assistant All In One Installer
  • Aeotec Z-Stick Gen5
  • Aeotec by Aeon Labs ZW096 Smart Switch, Small, White

Hi All,

I want to create a simple automation where an Aeon Labs ZW096 Smart Switch is turned on when the sunsets and turned off 5 hours later.

How I accomplish this is still a bit fuzzy in my mind despite the examples that have been provided here.

What I have come up with is:

automation:
  alias: "Christmas lights turn on at sunset and turn off five hours later."
  trigger:
    platform: sun
    event: sunset
  action:
    service:
    entity_id:

I am not clear on the following:

  1. How do I determine the service to use?
  2. How do I determine the entity_id to use? There is an old entity_id and new entity_id when I look at the switch in the HA frontend.
  3. How do I add the command to turn off the light switch five hours after it came on?

This is my first attempt at writing an automation. I appreciate any suggestions to help me get started in this area!

  1. The service is related to the device you are turning on. For a switch it’s switch.turn_on for a light it is light.turn_on. homeassistant.turn_on works for a lot of devices also.

  2. I would use the new entity id

  3. Two options. Add a delay after the first action and the turn the light off. Second option is just a separate automation to turn off the switch at a set time.

I’m pretty sure with the delay option if you restart HA or there was a power outage the switch would not turn off after 5 hours as all the automations get reset. Just as a heads up.

See below for the delay syntax. About half way down the page.

  1. Use a service for the domain the entity is in. In this case, switch, so you’ll want switch.turn_on. To see all of the services for a domain, in the lower left corner on the front end under developer tools select services. On that page, you’ll be able to select a domain and see all of its services (e.g. turn_on, turn_on, toggle). The documentation pages are in general very helpful, but the page for switch is a bit different. For some good examples take a look at the page for light, which is a bit more thorough. Switch behaves similar to light, but just with less options. https://home-assistant.io/components/light/

  2. Almost certainly use the new one. The new and old is a hangover from an old breaking change. This depends on what version you’re running. If it’s anything remotely recent it uses the new one.

  3. There’s a million ways to trigger an automation, but I would trigger it on sunset with a five hour offset. https://home-assistant.io/docs/automation/trigger/#sun-trigger

Thanks to @Dolores and @silvrr for the help.

I don’t think I wrote the YAML correctly since the YAML Lint is highlighting “action” as it is used twice. Below is what I came up with based on your feedback:

automation:
  alias: "Christmas lights turn on at sunset and turn off five hours later."
  trigger:
    platform: sun
    event: sunset
  action:
    service: switch.turn_on
    entity_id: sensor.aeotec_zw096_smart_switch_6_power
  delay: 05:00:00
  action:
    service: switch.turn_off
    entity_id: sensor.aeotec_zw096_smart_switch_6_power  

What did I do wrong? :slight_smile:

Thanks again!

shouldn’t that be:

switch..aeotec_zw096_smart_switch_6_switch

Edit: Actually, I think there’s more wrong than just that. I don’t have the device so I can’t confirm, but I’d wager this might work. Note that I changed the entity id (the name, not just the “switch.” part).

action:
  - service: switch.turn_on
    entity_id: switch.aeotec_zw096_smart_switch_6_switch
  - delay: '05:00:00'
  - service: switch.turn_off
    entity_id: switch.aeotec_zw096_smart_switch_6_switch

If you’re going to have multiple actions in an automation, you need to use the list format with “-” as shown in the automation 2 example on this page.

But, I recommend making this two separate automatons, with the second one triggering on sunset but with a five hour offset.

@Zorks,
That did it! I didn’t have the correct entity id (didn’t look far enough down the different States listed.

Thanks again to you and everybody who helped contribute ideas.

1 Like