get system cpu temperature in fahrenheit in typescript

To get the system CPU temperature in Fahrenheit in Typescript, we need to use the node.js os module and a third-party library called node-dht-sensor.

First, install the node-dht-sensor library using npm:

npm install node-dht-sensor
28 chars
2 lines

Then, in your Typescript file, import the required modules and perform the following steps:

import os from 'os';
import { promisify } from 'util';
import sensor from 'node-dht-sensor';
const readSensor = promisify(;

const cpuTemperatureFahrenheit = (osTemp: number) => ((osTemp * 9 / 5) + 32).toFixed(2);

const getCPUSensor = async () => {
  try {
    const { temperature } = await readSensor(22, 0);
    return temperature;
  } catch (e) {
    console.log('Error reading sensor:', e);
    return null;

const main = async () => {
  const osTemp = os.cpus()[0].temperature;
  const cpuTemp = await getCPUSensor();
  const cpuTempFahrenheit = cpuTemperatureFahrenheit(cpuTemp);
  console.log(`OS temp: ${osTemp}°C, CPU temp: ${cpuTemp}°C, CPU temp: ${cpuTempFahrenheit}°F`);

715 chars
26 lines

The getCPUSensor function uses node-dht-sensor to read the CPU temperature, and the cpuTemperatureFahrenheit function calculates the Fahrenheit equivalent. The main function calls these functions and logs the CPU temperature in Celsius and Fahrenheit.

gistlibby LogSnag