Tag: switch

How to turn on a switch at nighttime via Home Assistant automations

My wife recently asked me if it would be possible to have her bedroom lamp turn on after the sun goes down to “make the house more cozy”. The answer, of course, is “for you, it is but a trifle”.

Home Assistant makes it easy to create “automations” or programs that run to control your house. Having a button on your phone or computer to turn lights on and off is fun, but having the lights automatically come on when the sun sets; that’s the power of home automation.

As you add more and more smart devices to your home, you can “chain” them together to create more useful automations.

There are graphical tools like NodeRED which can make it easier to build these automations graphically, but I have had success just updating the YAML files manually for now.

In this example, I’m going to show how to automate my downstairs lamp switches (really outlets) to automatically turn on when the sun sets (only when we are home).

Here are the major steps

  • Install smart switches and/or outlets in your home and link them to Home Assistant
  • Create “trackers” to determine when you are home or not
  • Add a “sun” component to Home Assistant to know when the sun rises and sets in your part of the world
  • Create a “lamps” group to enable you to control a group of lights that should be turned on at the same time
  • Create an “automation” to toggle the switches to “on” when the sun sets

Install smart switches and/or outlets in your home and link them to Home Assistant

Obviously, you must have some smart switches or outlets already installed in your home and configured in Home Assistant as the first step. See the below blog posts to learn how to do this.

Create “trackers” to determine when you are home or not

You need to set up a “tracker” to determine whether or not you are home. This is so that you can use this information later in the automation. The way this works is that your router knows when your phone is connected to the network. Therefore, Home Assistant can “query” your router to see if your phone (technically, the MAC address) is connected.

Your mileage may vary here. Home Assistant supports many routers, but not all. You should create a special account just for Home Assistant to use to login to your router, but the normal admin login will do in a pinch.

  • Open the device_tracker.yaml file and add the following.
  • platform: the type of router you have, refer to the Home Assistant list to see what to put here
  • host: the IP address of your router (may be 192.168.1.1 for simple set-ups, login to your router to be sure)
  • username: the username to login to the router with
  • password: the password to use to login to the router
  • verify_ssl: if your router uses a SSL certificate to encrypt the admin page/data when logging in to the admin console, set this to true, false otherwise
- platform: !secret device_tracker_platform
  host: !secret device_tracker_host
  username: !secret device_tracker_username
  password: !secret device_tracker_password
  verify_ssl: !secret device_tracker_verify_ssl
  • Open the secrets.yaml file and add the following (note the names match what you defined before in the device_tracker.yaml file)
device_tracker_platform: unifi
device_tracker_host: 192.168.1.1
device_tracker_username: dwight
device_tracker_password: dwightIsAwesome!
device_tracker_verify_ssl: false

Now you need to add some “known devices” to Home Assistant so it knows what to look for when determining whether or not you are home (based upon whether or not your phone is connected).

  • Open (or create) the known_devices.yaml file and add the following.
  • hide_if_away: hide this device if not at home (set to false, kind of defeats the purpose here)
  • mac: the MAC address (this is the unique network identifier for your device, look this up for your specific phone)
  • name: the friendly name you want for this device
  • picture: if you have a picture for this device or person, add the path here
  • track: set to true so we know where the device is
jordan_iphone:
  hide_if_away: false
  mac: !secret known_devices_jordan_iphone_mac
  name: jordan iphone
  picture:
  track: true
kristal_iphone:
  hide_if_away: false
  mac: !secret known_devices_kristal_iphone_mac
  name: kristal iphone
  picture:
  track: true
  • Add the needed keys to your secrets.yaml file (obviously, not my real MAC addresses)
known_devices_jordan_iphone_mac: 00:AA:11:BB:22:CC
known_devices_kristal_iphone_mac: 00:ZZ:11:YY:22:XX

Add a “sun” component to Home Assistant to know when the sun rises and sets in your part of the world

Home Assistant has a built-in “sun” component that can determine when the sun rises & sets based upon your location and this data can be used in automations.

The way this works is that you need to tell Home Assistant your latitude and longitude so it can send this data to the service that determines when the sun rises and sets.

  • Add the following code to your Home Assistant configuration.yaml file
homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: !secret homeassistant_latitude
  longitude: !secret homeassistant_longitude
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: !secret homeassistant_elevation
# Track the sun
sun:

You will notice that I am “hiding” my latitude and longitude in a secrets.yaml file. Home Assistant uses this special file for things that should not be open (like passwords, my actual lat/long, MAC addresses, etc.) Naturally, this file should never be public (never uploaded to GitHub, for instance).

  • Open your secrets.yaml file (or create it if it doesn’t exist in the same directory as the configuration.yaml file)
  • Add the following items (note the names are the same as you put in the configuration.yaml file). This is obviously not my house 🙂
  • Save and close the file
homeassistant_latitude: 34.211001
homeassistant_longitude: -118.436418
homeassistant_elevation: 0

If you don’t know your lat/long and elevation, you can get this data from Google Maps. Just type in your address and it will show you the lat/long if you right-click on your home and select “What’s here“.

Create a “lamps” group to enable you to control a group of lights that should be turned on at the same time

  • Open the groups.yaml file and add the following.
  • Two groups are needed in my example because I want the lamps to show up in other nested groups in my GUI. You don’t necessarily have to have 2 groups.
lamps_hidden:
  name: Lamps
  entities:
    - switch.downstairs_office_lamp
    - switch.front_door_lamp
    - switch.living_room_lamp
    - switch.master_bedroom_kristal_lamp
  control: hidden
lamps:
  name: Lamps
  entities:
    - group.lamps_hidden
  view: yes

Create an “automation” to toggle the switches to “on” when the sun sets

  • Open the configuration.yaml file and add a line for automations and link it to a new automations.yaml file.
automation: !include automations.yaml
  • Open the automations.yaml file and add the following.
  • id: a unique identifier for this automation. I have several automations above this one, so mine is a7, configure as needed
  • alias: the name of the automation in the GUI
  • initial_state: whether or not the automation should be enabled on reboot of the Home Assistant
  • hide_entity: whether or not to hide this automation from the GUI
  • trigger: what causes this automation to kick off. In this case, I want it to fire 1 hour before sunset (so the house isn’t too dark at dusk)
  • condition: this is needed to ensure someone is home before the automation is enabled. I don’t want this automation to run if either me or my wife is not home (I could set up a separate “burglar deterrent” automation for running the lights when no one is home)
  • action: what the automation should do. In this case, I want Home Assistant to send the “turn_on” signal to the “lamps” group. This should toggle all of the lamps to on.
- id: a7
  alias: Turn on the lamps when the sun sets if someone is home
  initial_state: True
  hide_entity: False
  trigger:
    platform: sun
    event: sunset
    offset: "-1:00:00"
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: 'device_tracker.jordan_iphone'
        state: 'home'
      - condition: state
        entity_id: 'device_tracker.kristal_iphone'
        state: 'home'
  action:
    entity_id: group.lamps
    service: homeassistant.turn_on

Now we can wait for sunset and see if it turns on. After it comes on, we can see the results in the Logbook.

  • Go to the Home Assistant homescreen and click on Logbook

As you can see, at 6:44 PM (1 hour before sunset when I am writing this), the automation “Turn on the lamps when the sun sets if someone is home” has been triggered. You then see all of the lamps & lights I have associated with this automation come on.

That’s all there is to it. Now the lamps will turn on in my house 1 hour before sunset.

How to add a GE Z-Wave Smart Switch (1 gang) to your Home Assistant system

In this post, I will show you how to install a single GE Z-Wave Smart Switch (1 gang) to your Home Assistant system.

I am not a licensed electrician, so take my advice only as an amateur DIYer. If you are uncomfortable re-wiring a switch in your home, call an electrician to install the switch for you!

There are 2 ways to automate & control the lights in your house. You can either control the lights via the switches or via the bulbs themselves. Controlling the bulbs means buying very expensive replacements like the Phillips Hue. These give you the ability to have hundreds or more colors and lots of different options. However, you must leave the switches for these bulbs on all the time.

However, I prefer to just replace the switches in my house and keep my existing dumb CFL or LED light bulbs. For me, it is very important that lights keep working as normal, even if Home Assistant (or some other automation system) is unavailable. If either Home Assistant or some cloud service is down, I still want my switches to work as they always did. If I have someone over, I don’t want them to have to figure out my smart home system; they should just be able to turn the lights on and off as they always have. In addition, I haven’t seen much use in having the ability to change the colors of my bulbs. This may change in the future, but for now, that’s what I have decided to do.

Here are the tools and devices you will need to do this installation (affiliate links below).

There are 2 primary steps in this installation.

  • Replace the old electric switch with the new smart switch
  • Register your new smart switch with Home Assistant

Replace the old electric switch with the new smart switch

I am not a licensed electrician, so take my advice only as an amateur DIYer. If you are uncomfortable re-wiring a switch in your home, call an electrician to install the switch for you!

I cannot stress this enough. You can absolutely electrocute yourself or start a fire if you don’t do the electrical job correctly.

For simple jobs, I feel confident in my electrical ability to replace outlets, switches, etc. I have absolutely called in a professional electrician for complex jobs (like when I had a short in my bathroom GFCI outlets and they kept tripping). There is no shame in admitting when you need a professional to do a job!

If you are willing to keep going, then you need to turn the power off to the circuit that powers the switch you want to replace. Obviously, you could turn the power off to the whole house, but that would be silly! 🙂

This usually involves turning on the switch you want to replace (so the light is on), then start toggling the breakers one by one until the light turns off. To make this easier, I have found a very useful tool to determine which breaker controls which circuit in my house. I recommend you purchase a Klein Tools Digital Circuit Breaker Finder. You plug this device into an outlet that is on the circuit you want to find, then you use the included tester on each breaker until you find the one that matches. How it works is that it sends out an electrical signal over your internal power wires and it is received by the tester in your breaker box.

This is not a foolproof way of testing electric switches because it is possible your outlets in a given room are not on the same circuit as the lights, but it is a good possibility. As always, your mileage may vary and you should still test the light switch yourself to ensure it does not work anymore after you disable the breaker.

Now that you have disabled the circuit that powers the switch you want to replace, you can remove the old switch. Remove the 2 screws that hold the decorative faceplate on and then the 2 screws that attach the switch to the electrical box. Pull the electric switch out and look at the wires.

At a minimum, you will see 2 wires (usually black, but this is not guaranteed). These are known as the “line” and “load”. The “line” is the wire from your electrical breaker box and supplies the power. The “load” is to the device you want to power (lights, fans, etc). This is important because the smart switches must be installed with the correct wire inserted into the correct receptacle on the smart switch or it will not work.

This next step is where your mileage will vary considerably depending on the age of your home and the quality of the wiring job the contractor did when they built and wired your home. To install the GE Z-Wave Smart Switch (and most other smart switches), you must have 2 other wires. You must have a “neutral” (usually white, but not guaranteed) and a “ground” (usually copper and uninsulated, no plastic insulation around the wire). The colors of the wires are absolutely not guaranteed. Color coding has not always been the standard and there is no guarantee the contractor used the correct colors even if it was code (you must test the wire to be sure which is which). The typical skinny toggle switches many homes have don’t usually require a ground or a neutral to function, so many homes won’t have it. However, in order for the Z-Wave antenna to function, it must be powered at all times. This is the function of the “neutral” wire. It completes the circuit from the “line” so that the Z-Wave antenna is always broadcasting and receiving.

If your electrical box doesn’t contain a neutral wire, you cannot install this smart switch. There are certain kinds of switches that don’t require a neutral, but I have never tested them, so I can’t make recommendations at this time (however, my attic switch doesn’t have a neutral, even though the rest of my house’s switches do, so I will eventually find out).

Take a picture with your phone right now so you know how it was originally installed (both to make it easier to identify the various wires and so that you can restore it the original condition if you need to give up for whatever reason).

Remove each wire from the old light switch and separate them carefully so none of them are touching each other or anything else.

You need to identify which wire is the “line” and the “load”. This can be done using a Klein Tools Electric Voltage Tester. This will let you know which of the wires is “hot”. The “hot” wire is the “line” because it comes directly from the breaker box and always has electricity flowing through it.

Go back to the breaker box and turn the power back on. Now go back to the exposed wires and begin “testing” each one with the leads from the voltage tester. Put one of the leads on the “ground” (exposed copper) wire and begin testing each of the other wires (probably black) to determine which one is “hot”. The tool will flash 120 and buzz when it detects electricity in the line. This will be the “line”.

Turn the breaker for this circuit back off so you don’t electrocute yourself before continuing!

Take the “line” wire (probably black) and plug it into the “line” receptacle on the smart switch (probably in the bottom left-hand side when looking at the back of the switch). Tighten the screw on the side tightly to ensure the wire can’t fall out (tug on it lightly to ensure it is firmly held in place).

Take the other wire that was originally plugged into the switch, the “load” (probably black), and plug it into the “load” receptacle on the smart switch (probably in the upper left-hand side when looking at the back of the switch). Tighten the screw.

Now insert the “neutral” wire (probably white) into the “neutral” receptacle and tighten the wire. In my case, there was a pigtail with the neutral wires, so I removed it and plugged both of the neutral wires into the switch to complete the circuit.

Now you need to plug the “ground” into the switch. The ground wire protects you from electrical shorts by providing a path of least resistance to the “ground”. In my case, my ground wire was twisted together, so I needed to add a “jumper” wire (14 gauge) to make it easier to ground the switch (otherwise, the wire would have been very short and it would be too easy to pull out).

Now you can test the switch to see if you did the wiring correctly. Go turn the breaker back on. Begin toggling the switch. If you have wired everything correctly, toggling the switch up will turn on your lights. Toggling the switch down will turn the lights off. You will also see a small blue LED light when the switch is off. If you don’t see the blue light and the switch doesn’t turn on the lights, go back and carefully check which wires are plugged into which ports on the switch. If you hear crackling or popping, turn the breaker off and tighten all of your wire connections.

Now carefully push the switch back into the electrical box and replace the screws that hold it to the box. Then, put the new decorative faceplate on and tighten down the screws.

Congratulations, you have now installed the switch and can use it as a normal light switch.

Add the Z-Wave switch to Home Assistant

Link to Home Assistant documentation.

  • Navigate to your Home Assistant GUI in the browser
  • Click on Configuration
  • Click on Z-Wave
  • Click on Add node
  • On the Z-Wave smart switch, press up or down to put it in “pairing” mode
  • Click the Heal Network button. This will cause all of the Z-Wave switches to “report in” with the controller and it will optimize the routing of Z-Wave signals through your Z-Wave network.
  • Your Z-Wave switch is now added to Home Assistant

The next step is highly dependent on your individual configuration. If you are just starting out, you should see the switch added to the home page. If you have started enabling groups, you may not see new entities that have never been added before.

  • Click on the Unused entities button in the upper-right hand corner of the home screen (in the hamburger menu)
  • Toggle the switch to see if everything is set up correctly
  • Click on the name of the switch to rename it
  • Click on the gear
  • Click on the name override and give it a descriptive name
  • Click on the entity ID and give the name you will use to refer to it in the YAML files
  • Click Save, then click on the back arrow, then X

Now you need to add it to a group so it will show up in Home Assistant (this step may not be required for your setup)

  • Connect to your Raspberry Pi using Putty
  • Open the configuration.yaml file
  • Check to see if you have groups enabled

When you start using groups, you need to add every device to a group for it to show up on the home screen.

  • Open the groups.yaml file
  • Create a new group kitchen_hidden and list your outlet using the name you defined above. This is needed because the Home Assistant GUI will not show nested groups together in the same page if they are both shown. Note the control flag is set to hidden to hide the group from the GUI
  • Create a new group kitchen that you will use to create a new page in the Home Assistant GUI. Note the view flag is set to yes to show the group in the GUI
  • Optional, add your master_bedroom_hidden group to a “downstairs” group
    (mine will have more entries than yours, customize as needed)
downstairs:
  name: Downstairs
  entities:
    - group.lamps_hidden
    - group.kitchen_hidden
    - group.master_bedroom_hidden
    - switch.front_door_outdoor_lights
    - switch.front_door_entryway_lights
  view: yes
kitchen_hidden:
  name: Kitchen
  entities:
    - switch.kitchen_table_light
  control: hidden
kitchen:
  name: Kitchen
  entities:
    - group.kitchen_hidden
  view: yes
  • Save and close the file
  • Go to the Configurations tab in Home Assistant and click General
  • Click Check Config to ensure you didn’t make a mistake in the yaml files
  • Click Restart to update the running Home Assistant with your new groups
  • Refresh and go to the home screen
  • You will see your new tab at the top with the switch inside it
  • You will also see the new group added to the Downstairs tab as well.

That’s it! You have now successfully added a Z-Wave switch to Home Assistant.