How to format JSON array in Rule Machine

Hi Hubitat community,

Does anyone know whether Rule Machine can send arrays in JSON and, if so, how to format the JSON body in RM?

I'm trying to control an Elgato Key Light using Hubitat. The JSON message for turning on a light is {"lights":[{"on":0}], "numberOfLights":1 }. However, Rule Machine deletes the '[' and ']' array symbols, reducing the JSON message to {"lights":{"on":0}, "numberOfLights":1 }. The Elgato API requires an array for the lights field, even if there's only one light, preventing RM from controlling the light.

Thoughts?

I really need to break down and dig into RM (so take this with a huge grain of salt), but have you tried creating the JSON array in a string variable and using that to construct the message? Another thing that might work is to put a “\” before the brackets; ie.

{"lights":\[{"on":0}\], "numberOfLights":1 }

Thanks for your suggestions. These are good ideas but they don't work unfortunately:

RM doesn't interpret backslash as an escape character, so instead it passes the '' to the JSON body while still filtering out the square brackets:
[info](http://192.168.0.2/installedapp/configure/2716)Action: Send POST to: http://192.168.0.9:9123/elgato/lights -->(json) body: {"numberOfLights":1,"lights":\{"on":0}\}

RM also doesn't seem to recognize variables in the JSON body, as it sent the variable name in the JSON body:
[info](http://192.168.0.2/installedapp/configure/2716)Action: Send POST to: http://192.168.0.9:9123/elgato/lights -->(json) body: %JSON_message%

Any other ideas from folk, or is this a bug in RM?

Why don't you show how you are trying to do this?

Apologies, I forgot to include screen grabs previously. Thanks for the nudge.

Here's the Send POST action in edit mode, with the square bracket characters visible in the "lights" variable:

Here's the rule after I click 'Done', with the square brackets suppressed:

When I re-edit the action, the square brackets are visible again in the editor.

Thoughts?

So is it just a visual presentation issue; i.e. the brackets are there but aren't shown?

If you need to tightly control the string, put it in a string variable, and then reference the string variable in the post with %var-name%.

Thank you thebearmay and bravenel for your suggestions.

After much tinkering, the issue appears to be an HTTP incompatibility between the device and HE, as the former only accepts PUT requests while the latter appears to only offer POST for sending HTTP. The HE/Node-RED integration solved this for me.

Hi Jason,

I have a Elgato Keylights as well and recently purchased HE. Are you able to provide me a guide on what you did start to finish to control your EK with HE? Also, are you able to control it via voice command through Google Home or Alexa by any chance?

I created this flow (which is analogous to a Hubitat Rule Machine rule) in Node-RED.

The steps to use this are:

  1. If you want to control the key lights when your home computer is off, then buy a raspberry Pi and set it up on your local network. Else skip this step
  2. Install Node-RED on the Pi (or on your home computer if you don’t need 24/7 control)
  3. Add the built-in Maker API app on your Hubitat
  4. Add the Hubitat palette to Node-RED
  5. Copy my flow (link above) and import it to Node-RED
  6. Follow the instructions on my flow summary to set up the light and connect it to Hubitat

Yes. Add the Amazon Echo skill (built-in HE app) to your Hubitat. Share to Alexa the virtual color bulb you created in step 6 above. You can then control on/off, brightness and color temperature with your voice.

Let me know how you get on or if you have further questions.

I'll give this a shot. Don't need the lights controlled 27/7. Just when my pc is on. So when I'm ready to record a video or live stream, I can tell Google to "Active Studio mode" and it will Dim my basement lights, Turn on my EK, turn on other switches, etc...

How has this been working for you? Are you able to control the temperature of the lights and it's brightness as well?

It's been rock solid.

Yes, you can control both color temperature and brightness. Here are the controls and attributes you'll get in Hubitat for each Elgato Key Light:

On Alexa, you can set color temperature intuitively via color name. E.g. "Alexa, set the [light name] to soft white." Alexa recognizes the following color temperature names:
image

That will make your project much simpler, as the only time consuming step in my guide above is setting up a RPi. Instructions for installing Node-RED on a PC are here: Running on Windows : Node-RED

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

Download the Hubitat app