home-assistant.io/source/components/sensor.command_sensor.markdown
Fabian Affolter 67c4f562eb Update
2015-10-11 12:18:18 +02:00

3.5 KiB

layout, title, description, date, sidebar, comments, sharing, footer
layout title description date sidebar comments sharing footer
page Command line sensors support Instructions how to integrate commandline sensors into Home Assistant. 2015-09-13 10:10 false false true true
A sensor platform that issues specific commands to get data. This might very well become our most powerful platform as it allows anyone to integrate any type of switch into Home Assistant that can be controlled from the command line, including calling other scripts!

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

# Example configuration.yaml entry
sensor:
  platform: command_sensor
  command: SENSOR_COMMAND
  name: Command sensor
  unit_of_measurement: "°C"
  correction_factor: 0.4921
  decimal_places: 0

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.
  • correction_factor (Optional): A float value to do some basic calculations.
  • decimal_places (Optional): Number of decimal places of the value. Default is 0.

{% linkable_title Examples %}

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

{% linkable_title 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
sensor:
  platform: command_sensor
  name: HD Temperature
  command: "hddtemp -n /dev/sda"
  unit_of_measurement: "°C"

{% linkable_title 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_sensor
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    correction_factor: 0.001

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

{% linkable_title 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('http://10.0.0.48/analog/2').json()['return_value'])"

The script (saved as arest-value.py) that is used looks like the example below.

#!/usr/bin/python3
#
from requests import get
response = get('http://10.0.0.48/analog/2')
print(response.json()['return_value'])

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

# Example configuration.yaml entry
sensor:
  platform: command_sensor
  name: Brightness
  command: "python3 /path/to/script/arest-value.py"
  unit_of_measurement: "°C"