One of the most important challenges of this decade is the Internet of Things (IoT) that pursues the integration of real-world objects in the virtual world of the Internet. One property that characterises IoT systems is that they have to react to variable and continuous changes. This means that IoT systems need to work as self-managed systems to effectively manage context changes. The autonomy property inherent to software agents makes them a suitable choice for developing self-managed IoT systems. By embedding agents in the devices that compose the IoT is possible to realize a decentralized system with self-management capacities. However, in this scenario new problems arise. Firstly, current agent development approaches lack mechanisms to deal with the heterogeneity present in the IoT domain. Secondly, agents must simultaneously deal with potentially conflicting changes in their behaviour, concerning self-management and application goals. In order to afford these challenges we propose to use an approach based on Dynamic Software Product Lines (D-SPL) and preference-based reasoning. The D-SPL provides to the preference-based reasoning of the agent with the necessary information to adapt its behaviour at runtime making a trade-off between the self-management of the system and the accomplishment of its application goals.