Http sender 3th party
-
Hi Mark,
I think you'll run into trouble trying to use the HTTP sender for this, since you need total control of the parameter names and values.
I see two possible solutions,
1.
You could definitely send those messages out of a TCP/IP data source, though. What I would do is create a TCP/IP data source like this,host: 192.168.178.100 port: 8081 delimiter: configuration in hex: false
and a data point like,
data type: Alphanumeric queryable: false read command: settable: true write command: GET /json.htm?%VALUE% HTTP/1.1\nHost: 192.168.178.100:8081\n\n
You may need to add some more headers into the HTTP GET in the write command. You'll have to figure that out testing against the other device, but it sounds like you can get a working set of headers by checking the GET sent by your browser when you put it in the URL.
Next we'll set up a virtual alphanumeric point to set values to, and for simplicity let's say that point is set with values like, "6-On". Since this data source will only be used for user input, let's disable polling on its virtual data source.
And finally we'll create a point link from the virtual point to the TCP/IP point, which should have an update event of "Update" have a script like,
var info = /(\d+)-(On|Off)/.exec(source.value); if(info === null) return UNCHANGED; //Complain about bad input? return "type=command¶m=switchlight&idx=" + info[1] + "&switchcmd=" + info[2];
And now if i set the virtual point to '6-On' I see this in my test device:
GET json.htm?type=command¶m=switchlight&idx=6&switchcmd=Off HTTP/1.1 Host: 192.168.178.100:8081
- Solution 2
Create a virtual point to set values to, and for simplicity let's say that point is set with values like, "6-On"
Now create a meta point that updates on context for that point. In the body of the Meta point, we'll use the HttpBuilder script utility to send the get. This will have the advantage of letting us report the status of the write however way want (as opposed to events on the TCP/IP data source and point) and the value we store in the Meta point can indicate it was successful. So our meta point script might look like,
var info = /(\d+)-(On|Off)/.exec(p123.value); if(info === null) return UNCHANGED; //Complain about bad input? var headers = {"Host":"192.168.178.100:8081"}; var parameters = { "type":"command", "param":"switchlight", "idx": info[1], "switchcmd": info[2] } HttpBuilder.get("http://192.168.178.100:8081/json.htm", headers, parameters).resp(function(statusCode, headers, content) { // handle the response, maybe return "Success!" or whatever }).err(function(statusCode, headers, content) { // handle error responses }).execute();
-
Dear Phil,
Thank you very much! The 2nd option works fine for me. It is now very easy to switch multiple points in domoticz.
Thanks again!
-
Here is an update of the above script. I have set the ID number of the lighting fixed.
I have slightly modified the script so that it can also be used to dim lighting.
-
Awesome! Glad you were able to do what you needed to!
-
Dear Phil and Mark,
Dont know if you're still folowing this, trying to use your solution, and in the process maybe skipping a step, but i dont know yava script.I am using solution 2, the point i am referencing has state 1 or 2., trying to ad an extra variable. This is wat i got; is this a posibylity?
var pointinfo = /(1|2)/.exec(p20.value);
var info = "off"
if (pointinfo = 1)
var info = "on"
if (pointinfo = 2)
{
var headers = {"Host":"192.168.178.12:8080"};
var parameters = {
"type":"command",
"param":"switchlight",
"idx":"7",
"switchcmd": info
};
HttpBuilder.get("http://192.168.178.12:8080/json.htm", headers, parameters).resp(function(statusCode, headers, content) {
// handle the response, maybe return "Success!" or whatever
}).err(function(statusCode, headers, content) {
// handle error responses
}).execute();Hope to hear from you
-
Hi cor, welcome to the forum!
It looks as though the first few lines of your script could be touched up. A single = is the assignment operator, where == is a comparison operator. Also I realize you are taking the regex from the previous examples, but if you just need to see if the value is 0 or 1 you can simply compare upon the value itself, like,
var info = p20.value == 1 ? "off" : "on"; //set the value off only if p20.value is 1
or if you don't want anything but a 1 to mean 'on' you could do,
var info; if(p20.value == 1) info = "off"; else if(p20.value == 2) info = "on"; else throw "Unusable value in p20: " + p20.value; //cause a script error since bad input
Everything else seems like it'd be the same as in Mark's usage.
-
thank you! wil play with it some more!
-
@phildunlap 2 years ago I received a script from you for switching lighting. I now want to use this for another command. I would like to execute the following command:
Can you help me with the right script?
-
I may be able to be of some assistance here...
Alter your url to behttp://192.168.178.66/relay/1
And amend your parameters to have the 'turn' parameter
var parameters = { "turn": relay.value==1?'on':'off' }
I'm not quite sure if you're using the exact same configuration as before with a virtual datasource point but in a nutshell, calling the query like that will give you your toggle command.
If however you want to keep the code consistent, just have 100 for on and 0 for off, this way you can recycle the old code and just amend your URL.Fox
-
@mattfox said in Http sender 3th party:
var parameters = {
"turn": relay.value==1?'on':'off'I have just tried the following script but unfortunately this does not work.
-
The url is inside the get call. Not the host part of the header.
You want to 'get' calll the page, you're calling the 192.168... at the root level of the server, not the relay page.Secondly, your context point var is not used to correlate to being on or off. Since the point is an alphanumeric and not a numeric/binary, your parameters should be:
var parameters = { "turn": info }
You're calling a script but I think you are unaware of how everything works. I take it it has been a long time since you've done this.