Send PUT command but keep some existing paramters

Hi there, I’m totally new to all of this. I found one thread which had some pointers, but didn’t answer all my questions (or I didn’t interpret it properly…) (Add parameter to JSON Body PUT Request while retaining parameters from GET request)

In a nutshell, i’m trying to control my air conditioner remotley. At the moment I’m using webooks and IFTTT. It works perfectly. I can turn it on/off change temp etc.

The only issue I have is to do with zones. The command to change zones is "enabledZones":[1,0,1,0,0,0,0,0]}} in that example, zone 1/3 are on and all others are off.

The issue I have is when sending the put command I have to fill in all the data. If I want to turn on zone 4, for example, I need a way to send [X,X,X,1,X,X,X,X] where the X represents “leave this zone in whatver mode it already is” and the only change is the fourth one… I have no idea how to do this / if its possible. I was hoping there would be some sort of special character that would help me out… haha.

Incase it helps, when I run a GET command this is what it spits out:

{"result":1,"error":null,"id":0,"data":{"vid":2,"did":5,"device_type":"airconditioner","default_name":"Air Conditioner Zones","tags":"aircon","is_sensor":1,"is_actuator":1,"is_silent":0,"has_time_series":0,"has_subdevice_count":0,"has_state":0,"gid":"0","node":"*************","shortName":"Air Conditioner Zones","meta":{},"subDevices":{},"last_data":{"DA":[1,1,1,0,0,0,0,0],"timestamp":1603774102115}}}

Hey @M575,
I’m assuming you want to read the current state from the response, and let’s say the DA key has the current state then you might want to set it as a variable in your Postman Test Scripts as
pm.globals.set("zones", pm.response.json().data.last_data.DA)

In another request, maybe the PUT request, in the pre-request script you can simply access by

modifiedZones = pm.variables.get("zones")
//Say I want to set zone 3 and let the rest be
modifiedZones[2] = 1 //Assuming array index starts from 0
pm.globals.set("newZones", modifiedZones)

In your PUT request body, you can add the body
{{newZones}}

What you’re describing is a PATCH method for an API. Basically it means you can update just a single part of the data without passing in the whole object.

That would be up to the developers of the API if they chose to implement that or not. Maybe it’s in the docs?

The other way around it would be to first do a GET to load the current state. You’d get all the zones from the GET then you would modify them to turn on/off what you want in a subsequent call.

Thanks for the replies guys.

I think the best option would be to build a GET + PUT method, as the only reason I’m able to do this is because of a poorly built system, not because the developer actively promotes it.

I guess the solution I’ll therefor be looking for is, when google receives a command. “Turn on Zone 1” then GET current status. Store that status somewhere. Then PUT that same status but change zone 1 to a 1…

Again, ideally using IFTTT for this, and again, reiterating this is all new to me, but willing to learn.

I’m halfway there!

I’ve setup a JSON enquiry through google sheets which is easily triggered and spits out a cell with the CURRENT settings i.e. “1,1,1,0,0,0,0,0” - I can easily use that to create a formatted version of the new script, which now I have to somehow use to execute a PUT command… :\ i.e I have the appropriate put command generated in google sheets, now I need it to somehow execute.

2 Likes

Great work! I’m excited to hear about when you get it completed :slight_smile:

Solved! What I lack in JavaScript skills I more than makeup for in excel abilities :wink:

The setup is as follows.

Using IFTTT - I ask Google to “Turn on/off Zone X” This causes a google sheet to be updated with “X, On” (or off).

At this moment, the same spreadsheet runs a JSON Get, and thus I have the CURRENT status of the zones and also the details of the zone I’d like to CHANGE.

From here it’s a bunch of If/else and concatenate functions to output an appropriately formatted JSON PUT command. Unfortunately google sheets can’t actually run this, so…

We go back to IFTTT which is triggered by the sheet edit, and runs a Webhook which gets it’s body command (the JSON PUT) from the google sheet.

It’s not elegant, but it works. :wink:

It’s also not instant like all my other tasks, but that’s ok, it seems to be working within 30-60sec.

2 Likes

Sounds like you’re having a lot of fun over there =)

Congratulations on getting it working. It’s always a great feeling when you finally solve the problem!