Check the Shelly knowledge base for examples. I believe the following is your exact use case:
https://www.shelly.com/blogs/scripts-library/dynamic-load-shedding-script-for-energy-management
as well as examples and snippets on GitHub: https://github.com/ALLTERCO/shelly-script-examples
When drafting scripts directly in the app or through the web-interface of your device there should also be an option to open the library of examples.