Command line Sensor

The command_line sensor platform simply issues specific commands to get its data. This makes it a very 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 string Required

The action to take to get the value.

name string (Optional)

Name of the command sensor.

unit_of_measurement string (Optional)

Defines the unit of measurement of the sensor, if any.

value_template string (Optional)

Defines a template to extract a value from the payload.

scan_interval integer (Optional, default: 60)

Defines number of seconds for polling interval.

command_timeout integer (Optional, default: 15)

Defines number of seconds for command timeout

json_attributes string | list (Optional)

Defines a list of keys to extract values from a JSON dictionary result and then set as sensor attributes.

unique_id string (Optional)

An ID that uniquely identifies this sensor. Set this to a unique value to allow customization through the UI.


The command is executed within the configuration directory.

If you are using Home Assistant Operating System, the commands are executed in the homeassistant container context. So if you test or debug your script, it might make sense to do this in the context of this container to get the same runtime environment.

With a 0 exit code, the output (stdout) of the command is used as value. In case a command results in a non 0 exit code or is terminated by the command_timeout, the result is only logged to Home Assistant log and the value of the sensor is not updated.


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

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"
    # If errors occur, make sure configuration file is encoded as UTF-8
    unit_of_measurement: "°C"
    value_template: "{{ value | multiply(0.001) | round(1) }}"

Monitoring failed login attempts on Home Assistant

If you’d like to know how many failed login attempts are made to Home Assistant, add the following to your configuration.yaml file:

# Example configuration.yaml entry
  - platform: command_line
    name: badlogin
    command: "grep -c 'Login attempt' /home/hass/.homeassistant/home-assistant.log"

Make sure to configure the Logger integration to monitor the HTTP integration at least the warning level.

# Example working logger settings that works
  default: critical
    homeassistant.components.http: warning

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 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 interfacing with devices which are exposing a RESTful API.

The one-line script to retrieve a value is shown below. Of course it would 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/"

Usage of templating in command:

Templates are supported in the command configuration variable. This could be used if you want to include the state of a specific sensor as an argument to your external script.

# Example configuration.yaml entry
  - platform: command_line
    name: wind direction
    command: "sh /home/pi/.homeassistant/scripts/ {{ states('sensor.wind_direction') }}"
    unit_of_measurement: "Direction"

Usage of JSON attributes in command output

The example shows how you can retrieve multiple values with one sensor (where the additional values are attributes) by using value_json and json_attributes.

# Example configuration.yaml entry
  - platform: command_line
    name: JSON time
      - date
      - milliseconds_since_epoch
    command: "python3 /home/pi/.homeassistant/scripts/"
    value_template: "{{ value_json.time }}"