Fix where clause.
Add map of devices to entities table.