File Sensor


The file sensor platform reads the entries from a plain-text file and shows the found value. Only the last line of the file is used. This is similar to do $ tail -n 1 sensor.txt on the command-line. Note that file paths must be added to whitelist_external_dirs.

Configuration

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

# Example configuration.yaml entry
sensor:
  - platform: file
    file_path: /home/user/.homeassistant/sensor-data.txt

Configuration Variables

file_path

(string)(Required)Path to file that stores the sensor data.

name

(string)(Optional)Name of the sensor to use in the frontend.

Default value: file name

unit_of_measurement

(string)(Optional)Defines the units of measurement of the sensor, if any.

value_template

(template)(Optional)Defines a template to extract a value from the payload.

Examples

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

Entries as JSON

Assuming that the log file contains multiple values formatted as JSON like shown below:

[...]
{"temperature": 21, "humidity": 39}
{"temperature": 22, "humidity": 36}

This would require the following entry in the configuration.yaml file to extract the temperature:

# Example configuration.yaml entry
sensor:
  - platform: file
    name: Temperature
    file_path: /home/user/.homeassistant/sensor.json
    value_template: '{{ value_json.temperature }}'
    unit_of_measurement: '°C'