Saving hue scenes with curl

Hi,

I am trying to save the status of my hue lights in a scene of the hue bridge.
Because the service hue.hue_save_scene does not exist, I am trying to write a script to save my scene.
To do so I use the following code:
Configuration.yaml:

shell_command:
  executecurl: "curl -X PUT -d '{{ msgbody }}' {{ url }}"

In my script savehuescene.py I use the following code:

   urlstring = 'http://10.0.0.xxxx/api/xxxmysecretkeyxxx/scenes/' + scene_id
   msgbody: '{"lights": ["1","2","3","5","6","7","8","9","10","11","12","14","15","16","29"],"storelightstate": true}'
   service_data = { "url": urlstring , "msgbody": msgbody }
   hass.services.call("shell_command", "executecurl", service_data, False)

(with scene_id is the number of the scene i want to create)

This works which is great.
My problem is that I need to sum up all hue lights in the living room to be able to save the scene. This is not user friendly as I have to change the code when a new light is added in my hue bridge.

In my bridge I can see the hue lights in my living room with the following curl command:

curl -X GET http://10.0.0.xxx/api/mysecretkey/groups/living

Response:
{"name":"Group 0","lights":["1","2","3","5","6","7","8","9","10","11","12","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29"],"sensors":["1","2","5","7","10","12","15","17","18"],"type":"LightGroup","state":{"all_on":true,"any_on":true},"recycle":false,"action":{"on":true,"bri":3,"hue":7170,"sat":225,"effect":"none","xy":[0.5266,0.4133],"ct":500,"alert":"none","colormode":"ct"}}

My problem is that I have no clue how to catch the response of the curl in my (first) python script. I tried the code:

from requests import get
response = get(url, headers=headers)

but “import” gives me an error when running the python script.
I am only interested in catching the following part of the response “[“1”,“2”,“3”,“5”,“6”,“7”,“8”,“9”,“10”,“11”,“12”,“14”,“15”,“16”,“17”,“18”,“19”,“20”,“21”,“22”,“23”,“24”,“25”,“26”,“27”,“28”,“29”]” of the curl command.
Is there an easy way to do this?

Thanks for helping me out in my first script.

The python_script integration doesn’t support importing.

What I propose isn’t particularly elegant (it’s a kludge, really) but should work.

Create a RESTful Sensor to get the quantity of Hue lights. Then your python_script can read the sensor’s value.

The default polling interval for a RESTful Sensor is around 30 seconds (maybe even 15) which is far too often for this particular application. When configuring it you can set its scan_interval option to a large number (in seconds) so it updates only once a day (or whatever you prefer). Should you ever want to force it to update itself immediately (perhaps just prior to reading the sensor’s value for use within the python_script), there’s a service call for that (homeassistant.update_entity).

I want to avoid sending the hue values to each light from Hass to avoid an overflow of telegrams send to the hue bridge. I think there will be a risk of “packet loss”. I really like the idea of working with scenes stored in the hue bridge.

I already tried the RESTful sensor, but it wasn’t a great success. Can you configure the RESTful sensor to give you the ID’s of the lights in a certain hue bridge group?

I hope there is also a cleaner way to get this information. Any thoughts?

If you have a URL that returns the desired data, the answer is yes.