In the world of smart home automation, convenience is the name of the game. One way to achieve this is by creating a dedicated toggle switch for various devices and actions in your home. In this article, we’ll guide you through the process of setting up a toggle switch using Home Assistant. We’ll use the example of creating a toggle switch for controlling the “Bedroom Fan” using two scripts: one to turn it on and another to turn it off. You can adapt this tutorial to control any device or action in your smart home. We’ll also provide placeholder images to illustrate the steps. Let’s get started!
Prerequisites:
Before we dive into the steps, make sure you have the following in place:
- A working Home Assistant setup.
- The “Bedroom Fan” integrated into Home Assistant using a compatible remote control or device.
- Basic knowledge of YAML configuration.
Step 1: Create On and Off Scripts
The first step in creating your toggle switch for the “Bedroom Fan” is to create two scripts – one to turn the fan on and another to turn it off. These scripts send the appropriate commands to your fan using a remote control or device. Here are the example scripts:
bedroom_fan_on: alias: "Turn On Bedroom Fan" icon: hass:fan mode: single sequence: - data: command: b64:sgYyAAoYFgwWDAoYChgWDAoYFgwWCxYMChgKGBYMFgwKGBYMChgWDAoYChgWDBYMChcWDArcAAAAAAAA service: remote.send_command entity_id: remote.broadlink_pro_remote
bedroom_fan_off: alias: "Turn Off Bedroom Fan" icon: hass:fan-off mode: single sequence: - data: command: b64:sgYyAAoXFgsWCwoXChcKFxYLChgXCxYMChcKFxYLChcWCwoYChcWCwoXChcWCwoXChcKFwrcAAAAAAAA service: remote.send_command entity_id: remote.broadlink_pro_remote
The provided scripts use the remote.send_command
service to control the “Bedroom Fan.” Ensure that you replace remote.broadlink_pro_remote
with the actual entity ID of your fan’s remote control.
Step 2: Create a Toggle Switch Helper
Next, we’ll create a toggle switch helper that will represent the state of the “Bedroom Fan.” This switch will be used to control the fan’s on/off state.
You can do it through the UI by going to Settings -> Devices & services -> Helpers -> New helper -> Toggle switch:
Or add the following to your configuration.yaml
:
input_boolean:
bedroom_fan_switch:
name: Bedroom Fan
initial: off
Step 3: Create an Automation
Now that we have our scripts and toggle switch helper in place, it’s time to create an automation that will trigger based on the state change of the toggle switch.
This automation will then call the appropriate script to turn the fan on or off. Here’s the automation for controlling the “Bedroom Fan”:
automation:
- alias: "Bedroom Fan Control"
trigger:
- platform: state
entity_id: input_boolean.bedroom_fan_switch
action:
- choose:
- conditions:
- condition: state
entity_id: input_boolean.bedroom_fan_switch
state: 'on'
sequence:
- service: script.bedroom_fan_on
data: {}
- conditions:
- condition: state
entity_id: input_boolean.bedroom_fan_switch
state: 'off'
sequence:
- service: script.bedroom_fan_off
data: {}
Here’s a breakdown of each part:
alias: "Bedroom Fan Control"
: This line gives the automation a user-friendly name, which is “Bedroom Fan Control.” It’s used for reference and identification.trigger
: This section defines the event that triggers the automation. In this case, the trigger is based on the state change of theinput_boolean.bedroom_fan_switch
entity. When this toggle switch changes state (either from “on” to “off” or vice versa), it triggers the automation.action
: This section specifies what should happen when the automation is triggered. It uses thechoose
action, which allows you to define conditions and sequences of actions based on those conditions.- Inside the
choose
action, there are two conditions:- The first condition checks if the state of
input_boolean.bedroom_fan_switch
is “on.” If it is, it proceeds to the first sequence. - The second condition checks if the state of
input_boolean.bedroom_fan_switch
is “off.” If it is, it proceeds to the second sequence.
- The first condition checks if the state of
- In the first sequence, when the
input_boolean.bedroom_fan_switch
is “on,” the following action is taken:service: script.bedroom_fan_on
: This line calls thebedroom_fan_on
script you defined earlier. It sends the “Turn On” command to your bedroom fan using theremote.send_command
service.
- In the second sequence, when the
input_boolean.bedroom_fan_switch
is “off,” the following action is taken:service: script.bedroom_fan_off
: This line calls thebedroom_fan_off
script you defined earlier. It sends the “Turn Off” command to your bedroom fan using theremote.send_command
service.
In summary, this automation listens for changes in the state of the input_boolean.bedroom_fan_switch
entity. When the switch is turned on, it triggers the “Turn On” script, and when the switch is turned off, it triggers the “Turn Off” script, effectively controlling your bedroom fan’s state based on the state of the switch.
By following these steps, you’ve successfully created a toggle switch for controlling your “Bedroom Fan” using Home Assistant. Now, you can easily control your fan with just a tap on the switch in your Home Assistant dashboard or through voice commands if you’ve integrated voice assistants like Amazon Alexa or Google Assistant. Enjoy the convenience of a smart home that adapts to your needs!
Do you want to control your bedroom fan, but don’t have any device that can do it for you smartly?
You can use Broadlink and control almost any RF/IR devices! And yes, it works with Home Assistant!