Rule Machine Not Setting Global Variable From HTTP GET

I'm using HTTP to control devices through RM 4.0 but now I want to capture the result so I can make use of it

If I run the HTTP GET command in a web browser I usually get a response in JSON format e.g. {"result":"success", ...

As far as I'm aware I can assign that response to a global string variable in RM and then use it in other rules so I've created one and assigned it an initial value

I've also created a rule which consists of the action
Set TestVariable from GET response http://.....

When I run the action the device carries out the command and that action shows up in the live log but the variable becomes empty. I assume it's now empty because it doesn't show up in the list of variables in RM but I can select it from the list if I choose to set a global variable

There's no error in the log file to suggest anything went wrong so I ran a packet capture and I do see a response coming back with that JSON information but for whatever reason it just results in an empty global variable

So what's the problem?

2 Likes

Did you get this resoved? I am having the same problem with the same results.

No I didn't get any replies

If and when I get the time I'm going to look into seeing if I can write my own app

What address is your rule querying when you run it to get the variable value and what is the response from that webserver?

I don't think you quite understand...having an action to set the variable based on the response to an HTTP get request means that when the rule is triggered, Hubitat will query that webserver and set the variable based on the response form that server. If you are trying to use HTTP calls TO hubitat to control devices, you have to do it in a different way. Try the maker API. And if you're trying to set a global variable, use a Global Variable connector in concert with the maker API.

1 Like

You don't have to write your own app, just use the ones that are available correctly. You aren't understanding what HTTP GET means. It means that it reaches out and GETS the value from another server. So, when you perform the action of HTTP Get request, it will use the address specified to get the value from the other server. Not allow you to use that URL to set the value with an unsolicited request to Hubitat.

The action sends an HTTP GET request to a web server
That server interprets the request, talks to another device, gets a result back and sends it back to the requester in JSON format
So I'm trying to store that result in a global string variable

1 Like

Well, in order to debug what is going wrong, we're going to have to see your rule. How else can someone help you?

What are you using for the trigger on your rule? Have you turned on logging to see if the trigger is actually occurring?

It's just an HTTP GET request but it contains private information so I can't post it
If I paste the URL into a web browser I get a JSON response back
If I manually run the Action the variable I'm asking to be set becomes null
I've captured this in a Wireshark session by monitoring the Hubitat port and I can see the JSON information coming back as I'd expect
But it's not ending up in the string variable as I'd expected

3 Likes

I haven't revisited this in a while but I'm wondering if it just accepts the 200 response and ignores the JSON data

Just to clarify
The first rule I created had a timed trigger to run at Sunrise but I've been doing my testing by running the action manually
The action in the rule sends an HTTP GET to another vendor's hub
This has a built in web server and it's how the vendor lets you control their smart devices
The URL being sent contains device information as well as an instruction to carry out
The hub relays that command to the device, gets a response back then sends information to the client in JSON format and that includes things like what the result was

I wanted a means of capturing that result so I assumed I could store that JSON information in a string variable
So for the next step I created a global string variable and give it an initial value
I then created a rule which set a value for that variable from the HTTP response
If I manually run the action, the device does what is being asked in the URL
But the variable ends up being set to Null
I've logged events, triggers and actions in the rule and all I see in the logs is an Info entry to show that the Action ran

Is there a way to debug that action because I know there's JSON information being sent by the hub but it's just not making it into that variable or Hubitat doesn't like it?

I believe the JSON is the problem. The response has to be in plain text in order for it to work correctly. There's no JSON parser in Rule Machine.
You are setting it up like this, correct?

When I use this to a website that returns JSON it is changed to NULL. You can get the variable back by going to the variable editing screen and setting it equal to an initial value again, there's no problem there.

Why not have the "other hub" you mention initiate the message? It could send the command to Hubitat through the maker API. Then there wouldn't be a delay to this action being performed either. You seem to be awfully coy not using any proper names. If you were to give some more specifics about what you are trying to interface to, there might be another way.

Yes that's what I'm doing

If I run the URL in a browser it gives me a response of something like
{"result":"success","version":"2.0.6","position":100}

I thought I would end up with that being the value of the variable because it's a string

But now that I look at this it could also be getting confused because the response contains quotes

1 Like

But it's not a string the resonse is JSON. The headers say so. If the headers said plain text then it would accept it.

Is that a firmware version number it's passing back? That's awfully silly.

If you want to do this with a custom driver to get the info directly into the device it wouldn't be very hard to do. You can look at some of the examples of HTTP GET drivers out there. You could do it async with a callback method or with a try/catch HTTP request.

Understood

Thanks

Did that resolve your problem from the OP? :slight_smile:

I resolved my on the server side. The response has to be plain text

<?php
$data = 2;
header('Content-Type: text/plain; charset=UTF-8');
http_response_code(200);
echo utf8_encode($data);
?>

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.