how to toggle the state of a light or switch for Alexa in Home Assistant

The touch light switch on the wall lies in wait for someone to press it. When it’s on a press turns it off and when it’s off it turns on. That’s toggling a switch. Nothing clever there but if I am a push button on a remote control, what what message do I send to the light?

Often you’ll find that a remote control button press can only treated as an ‘on’ or an ‘off’. Amazon’s Alexa allows you to make routines that will turn lights on or off. What it can’t do (as of 2020) is to tell the light to do the opposite of what it was last doing. This is a ridiculous weakness – the way to work round this in Home Assistant is to create a script that toggles the light. You then ‘expose’ that switch to Alexa. In other words, you make the script public so that Alexa can make it run and toggle the light. It’s totally mad, I know.

See Automated Home Party – toggle lights and switches

Use the emulated Hue component and create a light that toggles

Go to the file editor in the Home Assistant frontend. You start by ‘exposing’ your scripts to Alexa.

  • In File editor open your configuration.yaml and add this code
  • Save the edits and completely shut down and restart the Raspberry Pi
  host_ip: 192.168.1.X 
# this is the IP of your own Home Assistant 
  listen_port: 80 
  advertise_ip: 192.168.1.X
    - script 
  expose_by_default: false 
# If expose by default is true you risk adding an unmanageable number of entities to 
# ALEXA. Read the Emulated hue doc if you want to expose just the entities you need. 
#   - light 
    - switch 
    - script 

Create a script to act as a light toggle

  alias: toggle cupboard lights 
    service: switch.toggle 
      - switch.cupboard_lights

Create a routine in Amazon Alexa

  • Outline:
    • Go to the Alexa app and find ‘Routines’. Create a new routine
    • Decide on the trigger event
    • Under add action > Smart Home > Control device. Find your script called ‘toggle cupboard lights’
  • For the detail on using Alexa see Automated Home Party – toggle lights and switches.

Leave a Reply

Your email address will not be published. Required fields are marked *