That all works fine but if I remove a device from the settings.valvesToManage, I have orphaned entries in state.devices. The simple solution is to iterate through state.devices and see if the device exists in settings.valvesToManage. For some reason, I can't get that to work.
settings.valvesToManage is an array not a map. In your first piece of code you are iterating through the array getting each ID but then in your orphaned code you are expecting it to be a map searching for a key thus it doesn't work. Since you are looping through each of the valvesToManage why not push each ID to an array within that code and then use it after that each loop to remove the orphans like my example: