How to use the Scratchpad?

I came across the Scratchpad in the main ui.
I found a couple of documentation pages and forum posts that say that it is very useful and how to open it.
However I was unable to find any documentation on what the code needs to look like.

I opened it and wanted to print out all things, so I used the following code:

things.getThings()

When I ran the code, I only got a notification saying “Script cannot be run if it is uninitialized!”.

I then tried this, but it did not work either:

console.log(JSON.stringify(things.getThings()))

What exactly do I have to do to run scripts from there (and how to print out all things)?

When you first create the -Scratchpad- script you choose a language. Based on the code you include above I assume you choose JS Scripting, but you could have chosen any language, even Blockly.

The -Scratchpad- is just a Script. And as documented, a Script in this context is just a special type of rule consisting of a single Script Action without any triggers or conditions.

Is that how you would print out all the Things in a regular rule?

Did you save it before trying to run it? What version of OH are you on. Versions prior to OH 4.2 M2 do not automatically save when you hit the run button.

“did not work” encompasses almost an infinity. Doesn’t work how? Errors in the log? It’s not clear, are you even looking at the logs? That’s where console.log is going to print.

There is nothing special about -Scratchpad-. It’s a Script like any other. In fact you can find it under Settings → Scripts.

Because it starts with “-”, it’s always at the top of the list.

Your code above worked kind of.

2024-04-29 14:48:43.322 [INFO ] [nhab.automation.script.ui.scratchpad] - [{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}},{"rawThing":{}}] 

But because of the way stuff like Items and Things are created, they are not supported by JSON.stringify. You’ll need to work with the yourself. For example to print a comma separated list of all the Thing UIDs:

console.log(things.getThings().map(t => t.uid).join(', '));
2024-04-29 14:52:28.171 [INFO ] [nhab.automation.script.ui.scratchpad] - chromecast:audiogroup:b2580e81-b68f-454c-b9db-405a381f4f48, vesync:airHumidifier:vesync-bridge:mbr-humidifier, shelly:shellydevice:25a289, openweathermap:weather-api:owm_account, zwave:serial_zstick:zw_controller, zigbee:device:zg_coordinator:0022a3000040d061, chromecast:chromecast:dcc2d5421fb9bfc30a7b39834f9a76af, zigbee:device:zg_coordinator:000d6f0005075c02, chromecast:chromecast:730a587d94d4cf879cbd4db1cb5c3d56, mqtt:topic:mosquitto:jenn_office_sensors, network:servicedevice:10_10_1_140_80, zigbee:device:zg_coordinator:000d6f000a79badf, openweathermap:uvindex:owm_account:local, openweathermap:weather-and-forecast:owm_account:local, honeywellhome:account:honeywell-account, vesync:bridge:vesync-bridge, zigbee:coordinator_ember:zg_coordinator, mqtt:topic:broker:family, zigbee:device:zg_coordinator:000d6f000a798ee4, zigbee:device:zg_coordinator:282c02bfffe8f8e9, mqtt:topic:broker:dads_oheb, chromecast:chromecast:e81b2b4563f964fbc4a2a63659028988, mqtt:topic:broker:muninn_sensor_reporter, http:url:nws, chromecast:audiogroup:21632c10-9553-4136-80c3-21202c34e900, zigbee:device:zg_coordinator:000d6f0004a86850, chromecast:chromecast:8ac883645140bb10a498af280595dd5c, chromecast:chromecast:dbb0ad19a4c0529fea592d35be738889, network:servicedevice:84264efc3amosquitto, openweathermap:air-pollution:owm_account:local, mqtt:topic:mosquitto:natebr_sensors, network:servicedevice:10_10_1_135_80, mqtt:topic:mosquitto:basement_sensors, zigbee:device:zg_coordinator:282c02bfffee0601, openweathermap:onecall-history:owm_account:local, chromecast:chromecast:5c7574a1a1e38a6d1756bdd58cb25e6e, chromecast:chromecast:dc4307b6b8f6cf51ddb42afc1be6d6d1, mqtt:topic:broker:basement_waveplus, chromecast:audiogroup:f0bb1698-e49e-478d-8f64-76e2ad2f442c, mqtt:topic:mosquitto:mbr_sensors, zwave:device:zw_controller:node12, honeywellhome:thermostat:honeywell-account:LCC-B82CA07C9CB5, zwave:device:zw_controller:node11, astro:moon:moon, zigbee:device:zg_coordinator:000d6f00053ac32d, zwave:device:zw_controller:node13, zwave:device:zw_controller:node7, zwave:device:zw_controller:node6, amazonechocontrol:smartHomeDevice:alexa_account:backyard-lights, zwave:device:zw_controller:node8, astro:sun:local, amazonechocontrol:account:alexa_account, mqtt:topic:mosquitto:cerberos_sensor_reporter, amazonechocontrol:smartHomeDevice:alexa_account:d32f1827-2556-4f16-8f88-b1f0bd26e4a3, mqtt:topic:mosquitto:main_floor_sensors, zigbee:device:zg_coordinator:282c02bfffe8f86f, zwave:device:zw_controller:node2, zigbee:device:zg_coordinator:282c02bfffe726cd, zwave:device:zw_controller:node4, thedoctor:doctor:healthAndStatus, ipcamera:generic:garage_camera, zigbee:device:zg_coordinator:7cb94c6356c60000, networkupstools:ups:office_nut, mqtt:topic:broker:TimeTest, chromecast:audio:5bb92386bf75f281613a7b49d5a6f576, zigbee:device:zg_coordinator:282c02bfffe7c35a, network:pingdevice:10_10_1_123, openweathermap:onecall:owm_account:local, shelly:shellyplus1:b8d61a8c51d0, network:pingdevice:10_10_1_122, mqtt:topic:mosquitto:hydra_sensor_reporter, chromecast:chromecast:c38875ea9560579aaad971f5efb7bc3b, chromecast:chromecast:ba33b6f122838958aaf2095502706084, zigbee:device:zg_coordinator:7cb94c6335c00000, mail:smtp:gmail, http:url:adguard, network:servicedevice:10_10_1_137_80, amazonechocontrol:echoshow:alexa_account:rich-echo, mqtt:topic:broker:sleepasandroid, mqtt:broker:broker    

See JavaScript Scripting - Automation | openHAB for details and reference information on how to work with Things in a JS Scripting rule.