home-assistant.io/source/_components/command_line.markdown
Franck Nijhof c464056402
Making our website faster, cleaner and prettier (#9853)
* 🔥 Removes octopress.js

* 🔥 Removes use of root_url var

* 🔥 Removes Octopress generator reference from feed

* 🔥 Removes delicious support

* 🔥 Removes support for Pinboard

* 🔥 Removes support for Disqus

* 🔥 Removes support for Google Plus

* ↩️ Migrate custom after_footer to default template

* ↩️ Migrate custom footer to default template

* ↩️ Migrate custom header to default template

* 🔥 Removes unused template files

* 🚀 Places time to read directly in post template

* 🚀 Removes unneeded capture from archive_post.html template

* 🔥 🚀 Removes unused, but heaving sorting call in component page

* 🚀 Merged javascripts into a single file

* 🔥 Removes more uses of root_url

* 🚀 Removal of unneeded captures from head

* 🔥 🚀 Removal of expensive liquid HTML compressor

* 🔥 Removes unneeded templates

* 🚀 Replaces kramdown with GitHub's CommonMark 🚀

* 💄 Adds Prism code syntax highlighting

*  Adds support for redirect in Netlify

* ↩️ 🔥 Let Netlify handle all developer doc redirects

* ✏️ Fixes typo in redirects file: Netify -> Netlify

* 🔥 Removes unused .themes folder

* 🔥 Removes unused aside.html template

* 🔥 Removes Disqus config leftover

* 🔥 Removes rouge highlighter config

* 🔥 Removes Octopress 🎉

* 💄 Adjust code block font size and adds soft wraps

* 💄 Adds styling for inline code blocks

* 💄 Improve styling of note/warning/info boxes + div support

* 🔨 Rewrites all note/warning/info boxes
2019-07-15 22:17:54 +02:00

140 lines
3.7 KiB
Markdown

---
title: "Command line Binary Sensor"
description: "Instructions on how to integrate Command binary sensors within Home Assistant."
logo: command_line.png
ha_category:
- Utility
ha_release: 0.12
ha_iot_class: Local Polling
redirect_from:
- /components/binary_sensor.command_line/
---
The `command_line` binary sensor platform issues specific commands to get data.
## Configuration
To use your Command binary sensor in your installation, add the following to your `configuration.yaml` file:
```yaml
# Example configuration.yaml entry
binary_sensor:
- platform: command_line
command: 'cat /proc/sys/net/ipv4/ip_forward'
```
<div class='note'>
It's highly recommended to enclose the command in single quotes `'` as it ensures all characters can be used in the command and reduces the risk of unintentional escaping. To include a single quote in a command enclosed in single quotes, double it: `''`.
</div>
{% configuration %}
command:
description: The action to take to get the value.
required: true
type: string
name:
description: Let you overwrite the name of the device.
required: false
type: string
default: "*name* from the device"
device_class:
description: Sets the [class of the device](/components/binary_sensor/), changing the device state and icon that is displayed on the frontend.
required: false
type: string
payload_on:
description: The payload that represents enabled state.
required: false
type: string
default: ON
payload_off:
description: The payload that represents disabled state.
required: false
type: string
default: OFF
value_template:
description: Defines a [template](/docs/configuration/templating/#processing-incoming-data) to extract a value from the payload.
required: false
type: string
scan_interval:
description: Defines number of seconds for polling interval.
required: false
type: integer
default: 60
command_timeout:
description: Defines number of seconds for command timeout.
required: false
type: integer
default: 15
{% endconfiguration %}
## Examples
In this section you find some real-life examples of how to use this sensor.
### SickRage
Check the state of an [SickRage](https://github.com/sickragetv/sickrage) instance.
```yaml
# Example configuration.yaml entry
binary_sensor:
- platform: command_line
command: 'netstat -na | find "33322" | find /c "LISTENING" > nul && (echo "Running") || (echo "Not running")'
name: 'sickragerunning'
device_class: moving
payload_on: "Running"
payload_off: "Not running"
```
### Check RasPlex
Check if [RasPlex](http://www.rasplex.com/) is `online`.
```yaml
binary_sensor:
- platform: command_line
command: 'ping -c 1 rasplex.local | grep "1 received" | wc -l'
name: 'is_rasplex_online'
device_class: connectivity
payload_on: 1
payload_off: 0
```
An alternative solution could look like this:
```yaml
binary_sensor:
- platform: command_line
name: Printer
command: 'ping -W 1 -c 1 192.168.1.10 > /dev/null 2>&1 && echo success || echo fail'
device_class: connectivity
payload_on: "success"
payload_off: "fail"
```
Consider to use the [`ping` sensor ](/components/binary_sensor.ping/) as an alternative to the samples above.
### Check if a system service is running
The services running is listed in `/etc/systemd/system` and can be checked with the `systemctl` command:
```
$ systemctl is-active home-assistant@rock64.service
active
$ sudo service home-assistant@rock64.service stop
$ systemctl is-active home-assistant@rock64.service
inactive
```
A binary command line sensor can check this:
```yaml
binary_sensor:
- platform: command_line
command: '/bin/systemctl is-active home-assistant@rock64.service'
payload_on: 'active'
payload_off: 'inactive'
```