Need extra command line switch

Need some help here with the command line switch in the config.yaml:
I already have a standard domotic system connected to a webserver in my house that I can control with tablet or computer.Now I want to control everything with home assistant.
With Wireshark I was able to get all the actions I need.Problem is that is first need to login to the webserver from my domotic system to turn on a light :
url://[email protected]:8080 and then another url to turn on the light like:
url:://xxx.xxx.x.xxx:8080/action/6640, So, I need to make 2 commands to make it work. I already tried to combine like:
url://[email protected]:8080/action/6640 but this is not working. How to putt this in the config.yaml?

switch:
platform: command_line
switches:
lights:
command_on: “/usr/bin/curl -X GET http://xxx.xxx.x.xxx:8080/action/6640”
command_off: “/usr/bin/curl -X GET http://xxx.xxx.x.xxx:8080/action/6640”
value_template: ‘{{ value == “1” }}’
friendly_name: Kitchen Lightswitch

Can I add a extra command to log in first?

Thanks

curl -X GET url1 && curl -X GET url2

In future please format your code properly, as set out at the top of every forum page.

Thanks, I try this later

This not working,
When I copy the url from the config.yaml into my browser it works.So the url is valid and working.Ports on my router are also open.
What could be wrong here?
thanks

I think the reply from the login call will have some token to indicate that login is successful, and that token has to be included in the second call to signify it comes from a logged in user.

But I have no idea of the details, or how to implement that with curl.

This is what I see on Wireshark when I toggle the url

Think I figured it out:
When you login for the first time to the webserver from my domotic system need to fill in name & password and confirm to save the settings on that device.I found out that you receive a cookie(with token?) from the server.
So next time when you login it goes directly to the control buttons without need to login again.
How can I login now from home assistant to the webserver to receive that cookie and save it?

I am running hass.io on a raspberry

There is a curl man page here https://linux.die.net/man/1/curl

It looks like you need to use the -b and -c options to store and send cookies.

I can use the switch when I put this in terminal with curl :

Last login: Thu Nov 22 06:31:19 on ttys000
iMac-van-xxxxxx:~ xxxxxxxxxxxxxxxxxxxx$ curl ‘http://192.168.0.141:8080/action/6640/’ \

-XGET
-H ‘Cookie: PHPSESSID=xxxxxxxxxxxxxxxxxxxxxxxxx’
-H ‘Accept: /’
-H ‘Accept-Language: nl-be’
-H ‘Host: 192.168.0.141:8080’
-H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15’
-H ‘Referer: http://192.168.0.141:8080/’
-H ‘Accept-Encoding: gzip, deflate’
-H ‘Connection: keep-alive’
-H ‘X-Requested-With: XMLHttpRequest’

How to use now in the config.yaml on the command line switch with the curl
Thanks

Is it something like : ?
switch:
platform: command_line
switches:
lights:
command_on: "/usr/bin/curl -X GET http://192.168.0.141:8080/action/6640 -H Cookie:PHPSESSID=XXXXXXXXXX -H connection:keep-alive

I can use all my light switches in HA yaml.config file with the following command line switch with curl like:

`curl http://xxx.xxx.xxx.182:8080 -H 'Cookie: PHPSESSID=jba2mfj3cji6t7moq2c5jfpgg7'  -H 'Accept: */*'    -H 'Host:xxx.xxx.xxx.182:8080'  -H 'Referer: http://xxx.xxx.xxx.182:8080/'   -H 'Accept-Encoding: gzip, deflate'  -H 'Connection: keep-alive'  -H 'X-Requested-With: XMLHttpRequest' -L http://xxx.xxx.xxx.182:8080/action/6640 `

But after some time these do not work anymore (about 60 minutes)
How can I keep this working all the time?

This is my response in terminal when I curl
< HTTP/1.1 200 OK < Date: Fri, 09 Mar 2018 21:14:03 GMT < Server: Apache < X-Powered-By: PHP/5.3.3-7+squeeze14 < Expires: Thu, 19 Nov 1981 08:52:00 GMT < Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 < Pragma: no-cache < Vary: Accept-Encoding < Content-Encoding: gzip < Content-Length: 20 < Keep-Alive: timeout=15, max=98 < Connection: Keep-Alive < Content-Type: text/html

It sounds like the cookie you get from the login has a limited life time - which isn’t surprising as most login systems will log you out after a period of inactivity.

So you will need to get a new cookie using curl, and then send that cookie using a second curl command.

As I said before, curl seems to have that capability, but I can’t give the details, because I have never done it before, but in principle the line would be

curl --cookie-jar /tmp/cookies <your url including username & password> ; curl --cookie /tmp/cookies 'http://192.168.0.141:8080/action/6640/' + other headers

where /tmp/cookies is a file to store cookies. I have no idea if this is a valid filename on hassos, so you may have to experiment with that, and the first url to get the cookie.

Thanks gpbenton,

this is not working after a lot of trial and error.
Because I have a working curl (with a limited life time) is there any possibility that I can add an extra header like -H. 'Expire:0 ?

If you could, I think it would defeat the purpose of having to log in.:sob:

But you still need the phpsessid to log in, right?

I was thinking because I already have a working curl command (with a limited lifetime) to make one switch to turn on every hour (due to the lifetime of the phpsessid) for about x seconds and turn off again.
Can this work to keep the session alive?
Does anyone know how to do this?

You are using the wrong method to authorize with domoticz, check this: https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Authorization

Basically, you puth the authorization either as a header (I think it’s -H flag in curl, not sure). OR you add it as a parameter to the request.

Also, under settings you can specify local networks, for which you won’t need authorization in domoticz.
image

@ Chrio, Thanks but I am not using Domoticz

Ok, then I misunderstood this part:

I’ll go back and re-read the post more carefully. :slight_smile: