Unravelling formation mechanism of materials used in sensing can enhance performance gas sensors and lithium-ion batteries