Scene_generator.py - Export YAML formatted state/attributes

DEMO:

OVERVIEW

This script allows you to export your current states/attributes into the YAML format for HASS Scenes!

  1. Set your devices to how you want them
  2. Call the scene_generator service with your JSON settings
  3. Go to the info tab to copy/paste the result
    OR
  4. Go to your configuration folder and open generated_scene.yaml if you set file_save to true

EXAMPLE JSON

{
    "domain": "light",
    "attributes": ["brightness", "color_temp", "xy_color", "rgb_color"],
    "save_file": true
}

FILE SAVING

If you would like scene_generator.py to save directly to a file in the HASS configuration directory, simply add a file notification declaration exactly as below. Currently the name is referenced by the script directly so do not change the name. You may change the filename to whatever you wish, however.

notify:
  - name: scene_generator
    platform: file
    filename: generated_scene.yaml
    timestamp: false

FUTURE ENHANCEMENTS

  1. Multiple domain service call support - get everything all at once
  2. Notifier service tie in - no longer use the info tab
  3. Possible hass.io add-on to autogenerate scene YAML files

GET IT

I’ll be updating this hopefully in the next few days so stay tuned: scene_generator.py

15 Likes

That sounds like a feature enrichment… Thanks I’ll test it soon …just a question. If I want several “items” run in a scene …how would that work? Let’s say light setting and AC setting ?

Currently I do not have support for multiple domains yet, you will just need to run the script again with updated JSON for now. I just made this last night so I’m planning to add that functionality in this week. I’ll keep this page updated when I do.

Great mate! I’ll keep this post in eye

1 Like

Alright guys, took me longer than a couple days lol but I finally made a minor update. Multiple domain support is now available. Updated the example on Github to reflect the update.

Hi! This is pretty awesome! Where would this file be placed to then be called in HASS?

Sorry for the slow response! You would place scene_generator.py in a subfolder under your main configuration folder called “python_scripts”. So you should have /main config folder/python_scripts/scene_generator.py

The second part is that you have to add the following to your configuration.yaml

python_script:

For a bit more in-depth information, check out https://home-assistant.io/components/python_script/

1 Like

How to put the json inside the script?

I will use always the same command (ask for lights brightness and color attribute), just want an easy to remember (push a button) for when I want to know the color/brightness of a light.

The JSON is configured via the Services section (the first icon on the bottom left). The gif on the source page should give you an idea of how that is entered. As for just a button push type of event, you could tie a switch to the service call?

ahh yes, do you mind helping me in doing this script

script:
  scene_generation:
    sequence:
    - service: python_script.scene_generator.py

how to put the json
?

Thank you - I’ll be coming back to this at some point. Bookmarked for now :slight_smile:

1 Like

Hi, looking forward to using this script to take a snapshot of my hue lights, but as a newbie to HA I am really struggling to get this working. Here’s my setup:

  1. Created a directory called python_scripts under the main config tree and copied the scene_generator.py file into it.

  2. added this line to the configuration.yaml file: python_script:

  3. created an automation which looks like this:

    alias: Hue snapshot
    description: ''
    trigger: []
    condition: []
    action:
      - service: python_script.scene_generator
    mode: single
    
  4. Note that I don’t know how to pass JSON in an automation, so I modified the script itself with my defaults like this:

    domains = 'light'
    attributes = ["brightness", "color_temp", "xy_color", "rgb_color"]
    save_file = 'true'
    
  5. Note that I tried true in quotes and without quotes. Either way I get this error:

    2021-12-05 08:01:44 ERROR (SyncWorker_3) [homeassistant.components.python_script.scene_generator.py] Error executing script: Unable to find service notify.scene_generator
    Traceback (most recent call last):
    File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 222, in execute
     exec(compiled.code, restricted_globals)
      File "scene_generator.py", line 66, in <module>
      File "/usr/src/homeassistant/homeassistant/core.py", line 1401, in call
     return asyncio.run_coroutine_threadsafe(
      File "/usr/local/lib/python3.9/concurrent/futures/_base.py", line 445, in result
     return self.__get_result()
      File "/usr/local/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
     raise self._exception
    File "/usr/src/homeassistant/homeassistant/core.py", line 1442, in async_call
     raise ServiceNotFound(domain, service) from None
    homeassistant.exceptions.ServiceNotFound: Unable to find service notify.scene_generator
    

Any advice on this would be greatly appreciated. I’d prefer to write to an output file but that doesn’t seem to work either. I am confused why it’s trying to call notify.scene_generator unless maybe that’s trying to write to a log file with the save_file = false ?

Thanks,

Dennis

EDIT: Refer to my next comment for an actual solution.

Wow, I am blown away people still find this little script useful! I’ll be honest tho, it has been years since I have touched it so I’m kinda back to guessing myself but try turning save file to off (without quotes as it’s a binary value). I feel the issue is the service notify.scene_generator IS this script so if you have any differences or if something has changed with HASS itself, it may not be able to call itself. If you try save_file = false I don’t think it even makes that call. LMK and if it’s still an issue, I can dust off the script myself and see what’s up again!

Ok, so after looking at this script again I see what your issue is. You need to setup a notify entry into your configuration.yaml for the file save to work.

notify:
  - name: scene_generator
    platform: file
    filename: generated_scene.yaml
    timestamp: false

If you set that up, you should no longer see the issue anymore. Also to help you out with data pass-thru, I am updating the script a little. First off it will include defaults so you don’t HAVE TO pass it anything. Second, I have included a services.yaml file now (which I just learned about today) to help have some documentation in the front-end.

Animation

Hope this helps!

Thanks for the help! I appreciate the revival of the old code - however useful it is!

OK, got it to work with your help!

I did add these lines around line 6:

# DEFAULTS
if not domains:
    domains = "light"
if not attributes:
    attributes = ['brightness','color_temp','xy_color','rgb_color']
if not save_file:
    save_file = True

to set defaults since I didn’t pass any variables to it. I got the light data and also those attributes and it generated the output file. Note that the last time I checked, python requires True not true for a boolean value.

If you did add defaults I couldn’t find them in the updated code.

Apologize, I did not get an email notification about your follow up or I accidentally archived it. The defaults were setup at the very top of the file:

# SETUP VARIABLES FROM HASS CALL
domains = data.get('domains', ['light','switch'])
attributes = data.get('attributes', ['brightness','color_temp','xy_color','rgb_color'])
save_file = data.get('save_file')

For instance domains has a defult of light and switch if it doesn’t find a corresponding “domains” object from data.get. Not very explicit but I was kinda just applying it as a patch for ya! Since there is interest I may expand upon this script a little more if anyone wants to see anything particular from it.

Hi,

This same project was also on my mind for a while and I did a few attempts. Today, just when I wanted to upload my result, I was alerted by the topic creation of a similar project. And indeed it is very similar.

I think my main differences are that it is HA native, more area or light group based and focusses only on lights (and anything that can be defined in a light group).

So I will study your project and maybe I can learn a bit from your approach and implementation.

Nice work!

2 Likes

That is awesome! I’m seriously amazed by how often this tiny little script I wrote in a day or 2 a couple years ago still gets some attention. @eiri it def looks like yours seems to cover much more than mine (I really haven’t updated since first post). If there’s use in mine, great, but if it seems like yours is just a more complete solution, I don’t mind depreciating my script and pointing folks to yours. Or if there is a level of combining the two, happy to offer my help in any way as well.

O no, please keep it. sometimes simplicity does the job. It is not my intention to actively maintain my script either. I just found it was handy, so I published it.

The idea is still valid so it seems after 5 years. :slight_smile:

New to HA. My first time installing a python script. Works great, very handy for exposing color codes. I really appreciate it. Thanks for sharing.

I have a Dashboard page I’ve named Documentation that contains handy info like entity names, etc. Would there be a way to pipe the results from this script into a card? That would be the full atomic bomb.