I could use some help. I am trying to convert a rest api commands that works fine on the RPI command line into a RESTful Switch based on the information in the below.
Rest API command to turn ON the switch: curl -u myusername:mypwd -X PUT -H "X-CSRF: x" --data "value=true" --digest "http://192.168.1.15/restapi/relay/outlets/5/state/"
Rest api command to turn OFF the switch: curl -u myusername:mypwd -X PUT -H "X-CSRF: x" --data "value=false" --digest "http://192.168.1.15/restapi/relay/outlets/5/state/"
Here is the Restful Switch component that I have added to my configuration.yaml file:
However when I try to validate my config file I get the following error message: Invalid config for [switch.rest]: expected a dictionary for dictionary value @ data[âheadersâ]. Got ââX-CSRF: xâ --data âvalue=ââ. I know the issue is likely with how i converted the header value into the header and body on and body off statements, but I have tried multiple variations and I have not been able to get it to pass validation. I would really appreciate it if someone here who is familiar with the RESTful Switch could help me with my syntax.
I am running the latest 64-bit hass.io installation on a Raspberry Pi 3 B+
pnburkner: You are awesome sir. Got it to work. I and tried that earlier, but I think I had my quotation marks set incorrectly. Working now with the following switch command line.
The only problem I have now is that the switch does not show up as a switch, but two lightening bolts. I assume there is a way to change the switch type.
I believe thatâs because youâre not using the optional command_state (and possibly value_template), which is what the sensor will use to temporarily poll the status of the switch. Is there a command you can send to have the switch return its current state?
You need to add at least the command_state parameter to the configuration. Can you run that command (using curl) and tell me what the output is? Also, do echo $? afterwards so we can also see what the return status is from the curl command. That information will help determine whether or not you need to use value_template in addition to command_state.
I donât think you need to use curlâs -k option, since the other commands work without it. I do, however, suggest adding curlâs -s option (just put it before the -u option) to all the commands. Lastly, in the value_template, the |lower part may or may not be necessary. I think you can get away without it.
Let me now how this works.
EDIT:
First, I updated the config to get the indentation right. I must have screwed it up when I copied and pasted.
Next, I believe by default this will poll the switch (i.e., send command_state) every 30 seconds. You should see this in home-assistant.log (look for âRunning state commandâ.) If you want to change this interval, I think you can by adding scan_interval: xxx at the same indentation level as switches:. E.g.,:
The first code worked - partially. It would turn the switch on, but then in HA it would return to the off position even though the switch was actually on. I am trying your latest code now. The new hass.os takes forever to restart to it takes a while to testâŚ
Unfortunately, I still have the same issue. With the scan_interval added it will turn the switch on, but then in ha the switch returns to the off position and stays there. So turning on the switch work (yeah), but now I just need to get ha to see that it is on and keep the switch in the on position.
I think the problem is with the value_template. Unless that results in exactly âtrueâ (after first being changed to all lower case), then it will update the switchâs state as off.
So, can you run the curl command (curl -u usrname:pwd -H 'Accept:application/json' --digest 'https://192.168.1.15/restapi/relay/outlets/5/state/') again from the OS command line, and let me know the exact output?
EDIT: Actually, Iâm starting to think that command_state (at least) may need curlâs -s option.
curl failed to verify the legitimacy of the server and therefore could not establish a secure connection to it. To learn more about this situation and how to fix it, please visit the web page mentioned above.
Edit:
If it helps. Here is what I get when the switch is off.
SUCCESS!!! By adding the â-kâ back to the command_state line it works now. It stays in the on position when switched on. Now I can switch it on and off without any issues. This is my latest code:
Ok, thanks. Hmm, interesting about the -k switch. I would have thought your other commands (to turn the switch on and off) would have failed without -k. Oh well. So -k is necessary.
So I really think -s is also necessary. curl behaves differently when run at the command line and via a script or something. In the latter case it may be outputting a progress bar, which will mess up the output. So, definitely add -s.
Thank you sooooooooo much pn. I really appreciate the help. I have been banging my head against the wall for weeks trying to get this working. I originally tried using the DigitalLoggers component, but could never get it to work. It would just give a bunch of errors in the log each time I restarted. Using these Curl commands seemed like the next best option, but was struggling to get them to work as I have never had to use this component before. I really appreciate the help and guidance.
All the best.
Edit: I will try a few different variations like adding the -s and removing the â|lowerâ and seeing if it makes any difference.
The -s option makes curl âsilentâ, meaning only output data returned. If itâs working without -s, then adding it probably wonât do much. Removing |lower shouldnât make any noticeable difference, although it will make it slightly more efficient (no sense converting a string to lower case when it already is. )
Just a quick follow up and one more question if I may.
I added the -s to the command_state and removed the â|lowerâ from the value_template and it is working great. It does seem a bit less laggy now.
One last question. Is there anyway to move my username and password to my !secret file and include the reference in the command lines for on, off, and state? I tried using several string concatenation options like â+â and â&&â, but none would pass validations. I guess it might be possible to move the entire command lines to the secrets file, but as I have a number of these switches to setup that would quickly become a pain. Is there a straight forward way of substituting in the !secret passcode in the command line strings?
switch:
- platform: command_line
scan_interval:
seconds: 10
switches:
dli_power_outlet_6:
command_on: "curl -u username:pwd -X PUT -H 'X-CSRF: x' --data 'value=true' --digest 'http://192.168.1.15/restapi/relay/outlets/5/state/'"
command_off: "curl -u username:pwd -X PUT -H 'X-CSRF: x' --data 'value=false' --digest 'http://192.168.1.15/restapi/relay/outlets/5/state/'"
command_state: "curl -s -k -u username:pwd -H 'Accept:application/json' --digest 'https://192.168.1.15/restapi/relay/outlets/5/state/'"
value_template: "{{ value }}"
friendly_name: 'Outlet 6 - Not in Use