Problem with sendHttpPostRequest action

646 views
Skip to first unread message

Aitor Iturrioz Rodríguez

unread,
Jul 30, 2014, 2:49:00 PM7/30/14
to ope...@googlegroups.com
Hi guys!

I'm having some problems with the sendHttpPostRequest action but I don't know what I'm doing wrong, so I need your help :(

My idea is to post my power consumption values (I get them from a Zwave GreenWave Reality PowerNode) to emoncms (a local installation in my server) and for that I'm using the input API with json. The API command for uploading a value works like this:


"power" is the name of the feed, "200" the value and "xxxxxxxxxxxxxxxxxxxxxxx" is my api key. If I enter this url on my web browser, everything works correctly.

To use this on OpenHab I've thought of using the sendHttpPostRequest action in a rule. Here you have what I've tried:

rule "Post to EmonCMS"
when
Item mediaserver_consumption changed 
then
var String url
var double value = new Double(mediaserver_consumption.state.toString())
sendHttpPostRequest(url)
end

For testing purposes I'm sending a predefined feed and value (power:200) and not the correct information (mediaserver_consumption:value), but obviously I will change that once I get this working. The problem is that everytime the rule is executed I see the following error in the openhab.log file:

20:32:34.415 ERROR o.o.c.s.ScriptExecutionThread[:50]- Error during the execution of rule 'Post to EmonCMS': Invalid uri 'http://192.168.1.100/input/post.json?json={power:200}&apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': Invalid query

What does "Invalid query" mean? Is there any syntax error in my URL? If the URL is working correctly in the browser, why doesn't it behave correctly in sendHttpPostRequest? Do you know any alternative method to post the information to EmonCMS without using external scripts? I don't want to use the commandLine action and a python script for doing this, the perfect solution would be to use a native rule...

Many thanks for your replies and help, I appreciate them a lot.

Best regards,

Aitor


Thomas E.-E.

unread,
Jul 31, 2014, 9:46:39 AM7/31/14
to ope...@googlegroups.com

Hi,

it seems that curly braces "{ }" are not allowed as query parameter value (see http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding). I would intuitively send the JSON document as content. You could use sendHttpPostRequest(String url, String contentType, String contentfor that. 

Hope this helps,

Thomas E.-E.

Aitor Iturrioz Rodríguez

unread,
Jul 31, 2014, 8:44:45 PM7/31/14
to ope...@googlegroups.com
Fantastic Thomas! Many thanks for your help!!

Just for future references, I've solved my problem replacing the "{" and "}" characters by "%7B" and "%7D" and it works perfectly ;)

Best regards,

Aitor

dany...@gmail.com

unread,
Oct 31, 2014, 10:13:47 AM10/31/14
to ope...@googlegroups.com
Can you write me the full rules that worked?

Aitor Iturrioz Rodríguez

unread,
Oct 31, 2014, 11:44:38 AM10/31/14
to ope...@googlegroups.com, dany...@gmail.com
Hi Dany!

I don't know what you need exactly, could you please tell us what are you trying to do and what type of errors do you see?

Tell me more information about your setup, files, goals, errors,... and I will be more than happy to help you ;)

Best regards,

Aitor

Dániel Pájer

unread,
Oct 31, 2014, 1:35:41 PM10/31/14
to ope...@googlegroups.com
Hi Aitor, 

I would like to create a wireless sensor network in my flat. I want to measure temperature, humidity, switching lighting etc.

In first round, I want to monitoring some temperature, and I will post to my emoncms.

I have the same problem, as you, but I don't know, how can I use correctly curly braces...i tried replace, but i made syntax error.

If you have been solved, please enter the correct rule to me! It would be a great help!

THX for your answers!

Best Regards,
Dany

Greg

unread,
Oct 31, 2014, 3:26:51 PM10/31/14
to ope...@googlegroups.com, dany...@gmail.com
I have mine using get requests, and instead of having to put the value and name in the beginning, you can put the node # at the end after the api key:
var Number valueToSend = item.state as DecimalType
sendHttpGetRequest("http://192.168.1.100/emoncms/input/post.json?csv=" + valueToSend + "&apikey=xxxxxxxxxxxxxxxxxxxxxxxxx&node=1")
Reply all
Reply to author
Forward
0 new messages