Best (only?) way to send JSON data to an API?


#1

I’m trying to talk directly to the Philips Hue API and I have piston working but with a problem.

I’ve setup a virtual switch so when it’s turned on i’ve written to the scene and then recalled another scene. Then when it’s switched off I recall the first scene again.

The problem is for the JSON data i need to use a variable and “scene” is already used when the button turns on. Is there no way for me to send the JSON data with “scene” different for each button press? Surely I don’t need to create a separate piston for the off motion? Whenever I select “scene” as the variable for the JSON data in the web request now it selects BOTH scene variables which obviously doesn’t work.

Is there a way to add the JSON body data within the web request section without the variables? I don’t see a way to do it if so, I only see header authorisation and the variable section.


#2

Hmm, why not add another task that sets scene variable content on button push so it will always have the right content and you can reuse the variable name?


#3

Could you set the variable within your if statement (and set it different for each condition)?

If Switch 8 Changes to on,
  with Location
    Set scene = "xxxxxxxxxx"
if Switch 8 Changes to off,
  with Location
    Set scene = "xxxxxxxxxx"
with Location   # place this outside the two if statements
  Make PUT request to x with JSON encoded data

#4

I’ve tried to do that but for some reason it’s not working…


#5

I’m not familiar with the Philips API. Does the storeLightState command save to a new scene number each time?

Does the first change (Switch 8 on) work?


#6

Neither work now i’ve used the Set variable command.

The first stores the same scene each time - none of it variable to be honest, but I don’t see I way to just hardcore the JSON form data in the web request as that would be the best way to do it. It only lets me select variables though?


#7

Hmm, I suppose you could try a get request and encode the variables directly into the URL, but I’m guessing the API wouldn’t like that. I don’t know if a way to do that, unless you run it through something like IFTTT or nodered.


#8

No it has to be in this format.

I just don’t understand why I can’t just input the JSON form directly in the web request. I guess the only way to do it is with two separate pistons, one for on and one for off.


#9

Just for giggles, set the scene variable to string instead of dynamic


#10

Hmmm doesn’t work either - really strange. I guess i’ll just split the piston into two.

Edit: Yes works fine as separate pistons.


#11

I tried it with a mock api call and the scenes make it just fine… not sure why it’s not working for the actual hue api.

image

image


#12

How do you get “Request Body” like that in Webcore?


#13

That’s not from webcore, it’s a web service that mocks an api call. https://beeceptor.com


#14

Ah right yes - I mocked it all up in Insomnia on the Mac first (a similar sort of thing) and all works well - if I could just add directly JSON code to the request body it’d be easier.


#15

Curious if you got this working? Also can you give a little more detail on the hue api? I love “living scenes” that animate the colors but hate the hue app. I have been using iConnectHue app but it doesn’t integrate with ST or Alexa.

Anyway, I have been wondering if I could “load” and run a script to the hue hub similar to iConnectHue, so webCoRE wouldn’t be continually sending messages to change colors.


#16

I got it working by having two separate pistons - it just seems the web requests are a bit limited still bit it works.

I’m actually using Homekit to control nearly everything now and a Homebridge plugin to expose all my ST things stuff to Homekit.

Now I can do anything - create any scene I want with effects, colour loop, transition times. There’s also an iOS shortcut which will get you the CURL command to give you on/off for Living Scenes so I can even control Hue Labs stuff with Homekit and Siri now.


#17

I have for a long tried to run a scene on Hue bridge from Webcore.
I can’t get it to work. Groups and Lights is working.
Now I have tried with your piston. That does not work either :frowning:
What do you write after /api/AuthorizedUserCode/ ???

Thanks…