Donnerstag, 9. April 2020

Home Assistant with ESPEasy Switch and REST

I flashed a cheap Sonoff S20 power plug switch with ESPEasy years ago (see this blog post) and wanted to see how easy I could add this switch to my new Home Assistant environment.

The common way to control this from a home automation environment seems to be MQTT -  including all kind of problems. Since ESPEasy provides a simple REST api there must be a direct way to integrate the switch into Home Assistant.


Turning the switch on is a simple http GET call with the last number beeing 0 for off and 1 for on:


Reading the state is also simple:

First try was using the RESTful Switch from HA, but it expects the switch command to be a http POST. Unfortunately ESPEasy expects a GET command.

Finally, I used the command_line switch from HA in combination with simple curl calls. This is the working code from configuration.yam:


switch:

  - platform: command_line
    scan_interval: 5
    switches:
      connection_octoprint:
        friendly_name: "ESPEasy Switch"
        command_on: "curl http://192.168.178.63/control?cmd=GPIO,12,1"
        command_off: "curl http://192.168.178.63/control?cmd=GPIO,12,0"
        command_state: "curl http://192.168.178.63/control?cmd=status,GPIO,12"
        value_template: "{{ '\"state\": 1' in value }}"


And do not forget to add the following rule to your ESPEasy device (using the ESPEasy web interface):

On Button#State=1 do
  if [Relay#State]=0
    gpio,12,1
  else
    gpio,12,0
  endif
endon

This way the switch works in both directions: pressing the button on the device or by switching from HA - and the state is correctly displayed in HA and on the device using the LED. Note the delay of 5 seconds for showing the switch state in HA after pressing the device button. This is due to the polling interval. 

Switch in Home Assistant

Configuration of ESPEasy

Keine Kommentare:

Kommentar veröffentlichen