title: Saving and Restoring Lights (HMCharacteristics)
body: <p>I'm using lights connected with HomeKit for notifications. I'd like to take the current state (color, brightness, on/off) of each light being used, and restore the lights to that state after the notification is finished. However it doesn't seem to be working.</p>
  
  <p>Here's how I'm trying to accomplish this:</p>
  
  <p>I have a dictionary that maps a dictionary of characteristics and their values to each light (HMService).</p>
  
  <p>I run the following code to process and save the current state of the lights:</p>
  
  <pre><code>for service in services {
      for characteristic in service.characteristics {
          if characteristic.characteristicType == HMCharacteristicTypeHue ||
              characteristic.characteristicType == HMCharacteristicTypeSaturation ||
              characteristic.characteristicType == HMCharacteristicTypeBrightness {
              characteristic.readValue(completionHandler: { (error) in
                  if error != nil {
                      print(error!.localizedDescription)
                  } else {
                      var characteristicsStore: [String : Any?] = [:]
                      if let existingCharacteristics = self.services[service] {
                          characteristicsStore = existingCharacteristics
                      }
                      characteristicsStore.updateValue(characteristic.value, forKey: characteristic.characteristicType)
                      self.services.updateValue(characteristicsStore, forKey: service)
                  }
              })
          }
      }
  }
  </code></pre>
  
  <p>Then, after the notification is complete, I run this code which is supposed to set the characteristics of the lights back to the values I've stored, but isn't working:</p>
  
  <pre><code>for (service, characteristics) in services {
      for (characteristicType, characteristicValue) in characteristics {
          if let characteristic = service.characteristics.first(where: { (aCharacteristic) -&gt; Bool in
              aCharacteristic.characteristicType == characteristicType
          }) {
              characteristic.writeValue(characteristicValue, completionHandler: { (error) in
                  if error != nil {
                      print(error!.localizedDescription)
                      didSucceed = false
                      numberOfErrors += 1
                  } else {
                      print("Updated \(characteristic.characteristicTypeToString()) to \(characteristicValue as! Float)")
                  }
              })
          } else {
              print("Did not find the characteristic")
          }
      }
  }
  </code></pre>
  
  <p>I get the error: Unconfigured Parameter, when I attempt to read or write the values.</p>
  
  <p>UPDATE:</p>
  
  <p>I was actually making progress, and then performed an update on my Hue lights. Now I'm worse off than I was before. I can't access any of the HMService objects 'accessory' properties, making it impossible to check to see if the accessory is reachable without a ton of rewriting.</p>
  
  <p>If anyone could help me figure out what I'm doing wrong or if there's something I'm completely missing, it would be greatly appreciated.</p>