Integrate a self written "Switch" into Home Asssistant
In this post you learn how to integrate a self written relay switch (for example with an Arduino UNO/MEGA or ESP82666 or ESP32) into Home Assistant. Lot of tutorials are using MQTT or ESP Home. On this page I describe how to control switches with plain HTTP requests.
Pre requisits
Regarding Home Assistant (HA) you will need:
- A running instance of Home Assistant. I'm using a Raspberry Pi5.
- In HA you have to install file File Editor (Install via Settings / Add ons / ADD-ON STORE called "File editor")
- In HA you need to know how to add new entities to your dashboard
Obvious you will need a device
- A microcontroller with an output like an relay or a LED which can be controlled via HTTP
- A running server on that microcontroller
- This example will be based on my Generic ESP Webserver but any other webserver should be usable
HA: Two Options
There are two options how to integrate a HTTP Switch:
- Settings / Devices & Servies / ADD INTEGRATION / Command Line
- Settings / Devices & Servies / ADD INTEGRATION / RESTful Command
Here I will describe the command line integration.
The configuration.yaml
You could add your command line integration directly in your configuration.yaml. I propose to put all command line integrations in a separate file and just include that file in configuration.yaml. In my example it will be named mycommand.yaml.
# Loads default set of integrations. Do not remove. default_config: # Load frontend themes from the themes folder frontend: themes: !include_dir_merge_named themes automation: !include automations.yaml script: !include scripts.yaml scene: !include scenes.yaml # My entries command_line: !include mycommand_line.yaml
HA: mycommand_line.yaml
Now create a new file calles "mycommand_line.yaml". In mycommand_line.yaml you define the commands to switch your relay. Additionally you can define a command to get the current state.
#command_line: - switch: name: 69 Sonoff S20 unique_id: 69sonoff command_on: /usr/bin/curl "http://172.18.67.69/cmd?c=on1" command_off: /usr/bin/curl "http://172.18.67.69/cmd?c=off1" command_state: /usr/bin/curl "http://172.18.67.69/state1" value_template: '{{ value == "0" }}' icon: > {% if value_json.rel1 == 1 %} mdi:toggle-switch {% else %} mdi:toggle-switch-off {% endif %}
- #command_line
- "command_line" commented with a # as that keyword is already in the configuration.yaml before the include command.
- - switch:
- Now start a new section switch
- name
- you can give your switch a name
- unique_id
- a unique identifer for your switch
- scan_interval
- is in seconds and defines how often the command command_state is called.
- command_on
- is the command to switch the output on. In the Example "http://172.18.67.69/cmd?c=on1" is the command to switch on pin 1 on the ESP8266
- command_off
- similar to previous entry, this command will switch off the output
- command_state
- a resource on the switch which responds the current state of the switch. In the example I request the state data with from a JSON. This line is optional but necessary to let the switch change in the home assistant dashboard
- value_template
- is an expression. If the expression validates to true, the switch is marked as ON
- icon
- optional icon logic to switch on and off switch. The example shows a multi line entry, hence the >
Restart HA Configuration
When you have modified (added) a plattform in the configuration.yaml you must RESTART your configuration. Usually this is only needed once. Use
Developer tools / Check and Restart / Restart
Later, when you just do adoptions in the mycommand_line.yaml it is sufficent if you just do a YAML configuration reloading.
Check Configuration
After you have changed files you can do a quick check of your files in Developer Tools / YAML / CHECK CONFIGURATION.
Reload YAML configuration
You will find it in the Developer tools also:
Developer tools / YAML configuration reloading / All YAML configuration
HA: Add Switch to Dashboard
Now you can add a new card by entity. Chose your entity (in this example "69 Sonoff S20") and press CONTINUE
Home Assistant will notify "We created a suggestion for your" and leave the card with ADD TO DASHBOARD
In your dashboard you can use your Arduino / ESP based switch:
Summary
You see, you can very easily integrate your own Arduino/ESP based relays and LEDs into Home Assistant. Just inform HA about the needed commands and let it collect the actual state of your switch.