61

How to Integrate Amazon Smart Plug with Home Assistant

 3 years ago
source link: https://brandonwillmott.com/2020/09/01/how-to-integrate-amazon-smart-plug-with-home-assistant/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Integrate Amazon Smart Plug with Home Assistant

Amazon Smart Plugs are an excellent addition to automate your home with Alexa. However, if you’re an advanced home automation user using a more robust solution like Home Assistant, you will find their limited usage to the Alexa app very frustrating!

I discovered a solution to get Amazon Smart Plugs accessible in Home Assistant using input booleans and template binary sensors. By pairing a binary sensor from Home Assistant with an Amazon Smart Plug inside of an Alexa Routine, we’re able to control the smart plug from Home Assistant!

home-assistant-alexa-app-relationship-1.png?w=638

I have a lamp by my fireplace that’s plugged into an Amazon Smart Plug that I want to control from Home Assistant:

fireplace-lamp-in-alexa-app-before.jpeg?w=473

Create Input Boolean and Template Binary Sensor

Let’s begin by creating the input boolean and the binary sensor in Home Assistant. I’ve chosen to create the input boolean using the Home Assistant UI. Navigate to Configuration -> Helpers then add a new Helper that is a Toggle:

configuration-helpers-toggle-fireplace-lamp.png?w=416

When adding the Toggle, Home Assistant doesn’t give you the option to name the input boolean, so it will create a generic one based on the common name you enter. For my fireplace lamp, it created input_boolean.fireplace_lamp. I prefer it to be a little more descriptive so I modified it after creation to prepend alexa_ha_:

lovelace-ui-fireplace-lamp-input-boolean.png?w=396

Alternatively, you can add the following code for the input boolean to configuration.yaml:

input_boolean: 
  alexa_ha_fireplace_lamp: 
    name: "Fireplace Lamp"
    icon: "mdi:lamp"

Binary sensors aren’t available in the Home Assistant UI so it must be added to configuration.yaml:

binary_sensor:
  - platform: template
    sensors:
      alexa_ha_fireplace_lamp:
          friendly_name: "Alexa HA - Fireplace Lamp"
          device_class: garage_door
          value_template: "{{ is_state('input_boolean.alexa_ha_fireplace_lamp', 'on') }}"

If you have existing binary sensors, just include everything from the device’s name down.

After adding this, save the confguration.yaml file, run Check Configuration, then if the configuration passed, restart Home Assistant.

What’s Going on Here?

If you’re not familiar with input booleans and binary sensors: an input boolean is a virtual device that has an “on” or “off” value. A binary sensor stores the state of a device and has a device class that represents that state. I’m using the binary sensor as a virtual device to track the state of the Amazon Smart Plug as it changes across Home Assistant and the Alexa App. Together it allows me to track the state of the entity and control it from the UI.

Although binary sensors can have a device class of type plug which makes more sense, I’m using garage_door instead. When I’ve tried using type plug, the binary sensor doesn’t transfer to Alexa properly and is renamed _AccessorySwitch. 🤷🏼‍♂️

Once Home Assistant restarts, the Alexa App sees the input boolean and binary sensor as devices and sends me a push notification that new devices are available. If you don’t see them after restarting Home Assistant, manually sync the entities from Configuration -> Home Assistant Cloud menu.

Sync Entities If Necessary

ha-configuration-alexa-sync-entities.png?w=628

Rename Amazon Smart Plug in Alexa App

Since I’ve named the input boolean “Fireplace Lamp” (which is the current name of the plug), we’ll need to rename the plug to avoid any confusion for Alexa. I prepend “HA -” to the name to differentiate it enough to not trigger when saying “fireplace lamp” and to remind me of the connection to Home Assistant:

fireplace-lamp-in-alexa-app-after-name-change.jpeg?w=494

Create Routines in Alexa App

Now we need to create two routines in the Alexa App that include both the Amazon Smart Plug and the binary sensor:

Routine for Turning On The Fireplace Lamp

My completed routine is shown below. Note that the when trigger is the binary sensor opening and the action is to turn on the Amazon Smart Plug and the input boolean. This will keep our device and the Home Assistant UI in sync.

alexa-app-turn-on-fireplace-lamp-routine.jpeg?w=473

Routine for Turning Off The Fireplace Lamp

My completed routine is shown below. Now the opposite as the example above: the when trigger is the binary sensor closing and the action is to turn off the Amazon Smart Plug and the input boolean. Again, this will keep our device and the Home Assistant UI in sync.

alexa-ha-turn-off-fireplace-lamp-routine.jpeg?w=497

Add Input Boolean to Lovelace UI

Now we’re ready to control the Amazon Smart Plug from Home Assistant! Add the input boolean to Lovelace UI however you’d like. For illustration, I’m adding it to a new entity card named Living Room:

lovelace-ui-fireplace-lamp-entity.png?w=986

All Done!

I can now turn this device on and off from Home Assistant and by using voice with Alexa. Discovering and documenting this process has been helpful for me to better understand Home Assistant better and finally control all my Amazon Smart Plugs from it! Remember, we’re only going to toggle the input boolean in Home Assistant or in the Alexa App, never the binary sensor! The Alexa App can only control a binary sensor and it also stores the state of the plug.

Feel free to leave a comment below or message me on Twitter if this has been helpful or if you have questions!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK