Control By Web WebRelay

I used modbus for my X-301, worked pretty much out of the box. You need to enable modbus over TCP in the device first. Here’s my config for the two output relays:

modbus:
   type: tcp
   host: 192.168.1.2
   port: 502

switch:
  platform: modbus
  slave: 1
  coils:
    - name: Relay1
      slave: 1
      coil: 1
    - name: Relay2
      slave: 1
      coil: 0

My Controlbyweb device is configured as follows in switches.yaml:

  - platform: command_line
    switches:
      warmwater:
        command_on: "curl -k http://192.168.1.2/state.xml?relay1State=1"
        command_off: "curl -k http://192.168.1.2/state.xml?relay1State=0"
        command_state: "curl -k http://192.168.1.2/state.xml"
        value_template: '{{value[67]=="1"}}'

Works like a charm. If the relay is switched by other means it is updated in HA within 30 seconds.

seems I experience a problem:

  - platform: command_line
    switches:
     1ste:
       friendly_name: "1ste"
       command_on: "curl -X GET http://10.20.0.240/state.xml?relay1State=2"
       command_off: "curl -X GET http://10.20.0.240/state.xml?relay1State=0"
       command_state: "curl -X GET http://10.20.0.240/state.xml"
       value_template: '{{ value[67] == "1" }}'
     KeukenLichten:
       friendly_name: "KeukenLichten"
       command_on: "curl -X GET http://10.20.0.240/state.xml?relay2State=2"
       command_off: "curl -X GET http://10.20.0.240/state.xml?relay2State=0"
       command_state: "curl -X GET http://10.20.0.240/state.xml"
       value_template: '{{ value[97] == "1" }}'
     3de:
       friendly_name: "3de"
       command_on: "curl -X GET http://10.20.0.240/state.xml?relay3State=2"
       command_off: "curl -X GET http://10.20.0.240/state.xml?relay3State=0"
       command_state: "curl -X GET http://10.20.0.240/state.xml"
       value_template: '{{ value[127] == "1" }}'
     BergingLichten:
       friendly_name: "BergingLichten"
       command_on: "curl -X GET http://10.20.0.240/state.xml?relay4State=2"
       command_off: "curl -X GET http://10.20.0.240/state.xml?relay4State=0"
       command_state: "curl -X GET http://10.20.0.240/state.xml"
       value_template: '{{ value[157] == "1" }}'

results in

Invalid config for [switch.command_line]: invalid slug KeukenLichten (try keukenlichten) for dictionary value @ data['switches']. Got OrderedDict([('1ste', OrderedDict([('friendly_name', '1ste'), ('command_on', 'curl -X GET http://10.20.0.240/state.xml?relay1State=2'), ('command_off', 'curl -X GET http://10.20.0.240/state.xml?relay1State=0'), ('command_state', 'curl -X GET http://10.20.0.240/state.xml'), ('value_template', '{{ value[67] == "1" }}')])), ('KeukenLichten', OrderedDict([('friendly_name', 'KeukenLichten'), ('command_on', 'curl -X GET http://10.20.0.240/state.xml?relay2State=2'), ('command_off', 'curl -X GET http.... (See ?, line ?). 

any idea ?

Several products from this company support SNMP (not the one mentioned in the original post, unfortunately…) But I just wanted to mention this option. I’ve used an X-300 for many years for humidity control and am very happy with it. If your device supports it, using SNMP might be easier.

Here’s an example definition of a sensor that I use to fetch the current humidity:

sensor:
  - platform: snmp
    name: 'Humidity'
    host: 192.168.1.220
    community: 'webrelay'
    baseoid: 1.3.6.1.4.1.30586.10.5
    unit_of_measurement: 'percent'
    value_template: '{{value | float}}'
    scan_interval: 60

Hope this might help somebody.

1 Like

I was warned this was an old topic before posting but this is the only one I could find on the subject. In hopes of adding to the knowledge base I thought I would post my working config. First, some background on the application: I want to control a light switch on some indoor tennis courts (simple enough). I tried Z-wave and after a couple weeks every sensor and switch had dropped off or been marked as “failed”, so I decided to use the wired controlbyweb POE relay model X-WR-1R12-1I-E. This is a single relay model, powered by 802.3af POE. More interesting this model has a 5V output and an optically isolated input. So, for me this presented the opportunity to have a standard switch on the courts (lights on/off) along with web control and automation. This is useful for the case when someone leaves the lights “on” after hours and I want HA to send “off” commands every 15 mins after closing to make the lights are off while we are closed overnight. So, I wired the 5 V output to a physical switch, which someone can turn on when they feel like its needed, and this then feeds the “input” to the web relay.

Based on info from this thread, the HA switch integration went fairly well, except for the “state”. As noted by others above I had some difficulty with reading the current state. For simple on/off commands (which the automation can use) the switch worked fine as follows :

switch:
  - platform: command_line
    scan_interval: 5
    switches:
      court_lights:
        friendly_name: "Court Lights"
        command_on: "curl -k --http0.9 http://192.168.20.218:8085/state.xml?relayState=1"
        command_off: "curl -k --http0.9 http://192.168.20.218:8085/state.xml?relayState=0"

After many attempts following the above posts and getting errors, the final “state” read command in my case is as follows:

        command_state: curl -k --http0.9 http://192.168.20.218:8085/state.xml | grep -E -m 1 -o "<relaystate>(.*)</relaystate>" | sed -e 's,.*<relaystate>\([^<]*\)</relaystate>.*,\1,g'
        value_template: '{{ value == "1" }}' 

Note these two lines are just after the above in the court_lights configuration.yaml definition. Oddly for me, this only worked by elimination of the opening and closing quotes, which seems a different syntax from the command_on and command_off yaml shown above.

The SED commands shown reduce the long XML response of the webrelay to a single value {1,0} which is then translated by the value template to {ON, OFF} for a switch. I could not get the character position value syntax of previous posts above to work myself, and an advantage of the above approach could be that the position of the desired string does not have to be known, since SED will search and extract the target value. Don’t pay attention to all the info you find on google about how its bad to parse XML with bash commands. That’s advice for programmers.

In my case, since the physical switch on court may be over-ridden by the HA automation or web control (e.g. someone left the light switch on and the automation turned them off), I thought it useful to have a “Switch Position” feedback in HA. This seemed to work best as a “binary sensor” command line entity. The YAML for this entity is as follows, noting again that the usual opening and closing quotes are not used, and in this case the “relaystate” is replaced with “inputstate”:

binary_sensor:
  - platform: command_line
    scan_interval: 10
    name: "court_switch_position"
    command: curl -k --http0.9 http://192.168.20.218:8085/state.xml | grep -E -m 1 -o "<inputstate>(.*)</inputstate>" | sed -e 's,.*<inputstate>\([^<]*\)</inputstate>.*,\1,g'
    payload_on: 1
    payload_off: 0

Ultimately, this is what my lovelace card looks like, using the “switch” entity for the first line and the “binary_sensor” entity for the second:

Court_Lights_Lovelace

Your indenting is off. “1ste” is only indented 1 space rather than 2 spaces.
That should correct the problem.
Check whether your relay#State=2 is really correct. From the documentation I have I would have expected “=1”.

I added the sensor to see if my count was wright. Use this online tool to count them, Word Character Count

I tried to look at both your example and the SNMP help page and came up with something like this:

switch:
   -platform: snmp
       name: "relay1"
       version: "2c"
       host: 192.168.12.12
       community: webrelay
       baseoid: 1.3.6.1.4.1.30586.46.0.5
       payload_on: 0
       payload_off: 1

It works in so far as I can turn off the switch but I cannot turn on that switch and I do not know the state of the switch. Is there a reason I cannot turn on the switch?

I also tried using xml… from a terminal window I can turn on and off the switch but when I try using the following, the switch slides on and then back to off. I also have tried using -X GET instead of just -k

platform: command_line
  switches:
    furnace_reset:
      friendly_name: "Furnace Reset"
      command_on: “curl -k http://192.168.12.12/state.xml?relay1State=1”
      command_off: “curl -k http://192.168.12.12/state.xml?relay1State=0”
      command_state: “curl -k http://192.168.12.12/state.xml”
      value_template: '{{value[66]== "1"}}'

I’m using SNMP only for fetching humidity readings (i.e. GETs). I’ve never tried using PUTs to control the output relay.

Thanks for your response!

There was another post of someone having a problem where their slide switch kept going back to off and I believe it was related to HA not knowing the state of the switch. I switched the values of payload_on and payload_off with each other and now payload_on turns the switch off. Is there a way to know the status of the switch. I see the sensor, but not the switch.