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: