Command Line Sensor

The command_line sensor platform that issues specific commands to get data. This might become our most powerful platform as it allows anyone to integrate any type of sensor into Home Assistant that can get data from the command line.

To enable it, add the following lines to your configuration.yaml:

# Example configuration.yaml entry
  - platform: command_line
    command: SENSOR_COMMAND

Configuration variables:

  • command (Required): The action to take to get the value.
  • name (Optional): Name of the command sensor.
  • unit_of_measurement (Optional): Defines the unit of measurement of the sensor, if any.
  • value_template (Optional): Defines a template to extract a value from the payload.


In this section you find some real life examples of how to use this sensor.

Hard drive temperature

There are several ways to get the temperature of your hard drive. A simple solution is to use hddtemp.

$ hddtemp -n /dev/sda

To use those information, the entry for a sensor in the configuration.yaml file will look like this.

# Example configuration.yaml entry
  platform: command_line
  name: HD Temperature
  command: "hddtemp -n /dev/sda"
  unit_of_measurement: "°C"

CPU temperature

Thanks to the proc file system, various details about a system can be retrieved. Here the CPU temperature is of interest. Add something similar to your configuration.yaml file:

# Example configuration.yaml entry
  - platform: command_line
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ value | multiply(0.001) }}'

The correction_factor will make sure that the value is shown in a useful format in the frontend.

Details about the upstream Home Assistant release

You can see directly in the frontend (Developer tools -> About) what release of Home Assistant you are running. The Home Assistant releases are available on the Python Package Index. This makes it possible to get the current release.

  - platform: command_line
    command: python3 -c "import requests; print(requests.get('').json()['info']['version'])"
    name: HA release

Read value out of a remote text file

If you own a devices which are storing values in text files which are accessible over HTTP then you can use the same approach as shown in the previous section. Instead of looking at the JSON response we directly grab the sensor’s value.

  - platform: command_line
    command: python3 -c "import requests; print(requests.get('http://remote-host/sensor_data.txt').text)"
    name: File value

Use an external script

The example is doing the same as the aREST sensor but with an external Python script. It should give you an idea about interacting with devices which are exposing a RESTful API.

The one-line script to retrieve a value is shown below. Of course would it be possible to use this directly in the configuration.yaml file but need extra care about the quotation marks.

$ python3 -c "import requests; print(requests.get('').json()['return_value'])"

The script (saved as that is used looks like the example below.

from requests import get
response = get('')

To use the script you need to add something like the following to your configuration.yaml file.

# Example configuration.yaml entry
  platform: command_line
  name: Brightness
  command: "python3 /path/to/script/"
  unit_of_measurement: "°C"