Are you trying to do this from a custom driver or a custom app?
You can only get a device reference if the device is a child of you (device or app) or if a specific list of device(s) was selected in a selector drop-down (app).
If it's a child of your app or driver, you can use getChildDevices() and then iterate over them as needed using accessors from the Device Object.
If it is from your app but not a direct child device, you can use the capability selector as shown in the Hubitat examples, like this one. Then you can use the Device Object methods as before.