Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File action read_file documentation #37702

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions source/_integrations/file.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,49 @@ This would require the following settings to extract the temperature:
- File path: `/home/user/.homeassistant/sensor.csv`
- Value template: {% raw %}`'{{ value.split(",")[1] }}'`{% endraw %}
- Unit of measurement: `"°C"`

## Action `file.read_file`

Reads a file and returns the data in a response.

| Data attribute | Optional | Description |
| -------------- | -------- | ----------- |
| `file_name` | No | The path of the file and name to read. Files should be utf-8 encoded. Example: `config/www/myfile.yaml` |
| `file_encoding`| No | The content type of the file (`json` or `yaml`). Example: `yaml` |

> **Note:** The file paths should be relative to the Home Assistant configuration directory.
{% important %}

File paths must be added to [allowlist_external_dirs](/integrations/homeassistant/#allowlist_external_dirs) in your `configuration.yaml`.

The action returns a dictionary with a data element containing the parsed content from the file.

Example, read a JSON file out of the `www` directory.

```yaml
- action: file.read_file
data:
file_name: config/www/myfile.json
file_encoding: json
response_variable: file_content
```
Contents of myfile.json
```json
{
"latitude": 32.87336,
"longitude": -117.22743,
"gps_accuracy": 1.2
}
```

Response:

```yaml
data:
latitude: 32.87336
longitude: -117.22743
gps_accuracy: 1.2
```
Loading