How do I create a CSV file and append values to it?

Hi there, as the title explains, I just need help with CSV files. I’m pretty sure there is some sort of CSV functionality in Home Assistant, but I can’t seem to figure it out. Most people either on forums or YouTube deal with Google Sheets but not a plain CSV file, so if anyone could help me out with this, then that’d be greatly appreciated

1 Like

Use the file notification service:

notify:
  - name: write_to_csv
    platform: file
    filename: my_csv_file.csv

In use:

action:
  - service: notify.write_to_csv
    data:
      message: "some,csv,data,to,write,here"

You can use templates in the message, e.g.

action:
  - service: notify.write_to_csv
    data:
      message: "{{ now(),states('sensor.temperature'),states('sensor.humidity') }}"
1 Like

When it says that “This would require the following entry in the configuration.yaml file”, I don’t understand this. I know where the configuration file is and I know how to access it, but is that really where I put the block of YAML they give? By the looks of it, the YAML seems to be referring to an action that resides within an automation.

Only this bit goes in your configuration.yaml file, to enable the notification service:

You have to restart after adding it.

The other examples in my post go in your automations or scripts and can be done using the UI editors or YAML.

I just have a fair bit of questions so please do bear with me.

If I want to make another CSV file, do I have to make a completely new notify block of code, or can I just add another filename attribute? And in the YAML you provided, what would be the technical name of the notify and filename YAML codes?

Where do the CSV files get saved? Is there a way to specify the path that it gets saved to?

In the action YAML, I’m assuming the now() just gives the current time. As for the states('sensor.temperature'), if I wanted to output lets say a device or entity, would it be the same syntax of device('some.device')? What about when displaying the friendly name of a device?

Again, I know it’s a lot of questions, but I’m new to Home Assistant and just wanted some clarification on these topics is all.

Yes you can do more than one file like this:

notify:
  - name: write_to_csv_file_1
    platform: file
    filename: my_csv_file_1.csv
  - name: write_to_csv_2
    platform: file
    filename: my_csv_file_2.csv
  - name: write_to_sensors_csv
    platform: file
    filename: sensors_csv_file.csv

Each will create their own notification service.

You can make the names and the file names whatever you want as long as they are unique. It’s a good idea to at least make the name similar to the file name to avoid confusion.

The technical name?

Not exactly sure what you are asking, but they are both configuration options of the file notification service.

Files without a path will be saved to your /config directory (where your configuration.yaml file is). You can include a path in the filename to move it elsewhere in that directory, e.g.

    filename: config/sensor_csv_files/temperatures.csv

Yes now() will print the time and date.

Use entity ids not devices. It will be a lot simpler.

For help on templating see:

Regarding the technical name, I was just wondering what I call the file and notify YAML codes, as opposed to referring to them as “the notify YAML code” and whatnot. But it makes sense now I think. To add more functionality to Home Assistant, you add specific “services” (like the notification service) to the configuration file. And then to customize said service, you would modify the configuration options (filename .etc). Do let me know if I’m on the right track with my thinking here.

I just have another question. So in the file service documentation on the Home Assistant website, they have the “notification” and “sensor” services. Is the notification used for writing to a file, and sensor is used for reading from a file?

1 Like

Correct on all points. Do note the limitation of the file sensor though:

Only the last line of the file is used

1 Like

Alright thank you so much! You answered all my questions and I appreciate you taking the time to respond to me. Hope you have a great day my friend :slight_smile:

3 Likes

UPDATE for version 2024.6

As per the new notify entity platform the action configuration has changed. Here is a revised version of the previous example:

action:
  - service: notify.send_message
    target:
      entity_id: notify.write_to_csv
    data:
      message: "some,csv,data,to,write,here"
  • service is always notify.send_message
  • entity_id is whatever you have defined in configuration.yml

UPDATE: If the data is still not written into the file check for errors under Integrations → File. Apparently there have been some breaking changes and I needed to add allowlist_external_dirs config and reconfigure the file entity.

Blog post about the new feature:

2 Likes

Is this still working, Tom? The service won’t be created through code. And through integration, I get an error with the path (Access to the selected file path is not permitted).

I’m trying to upload certain sensors to a CSV file. Any ideas?

See the post directly above yours.

My ‘config.yaml’ reads amongst other things:

homeassistant:
  allowlist_external_dirs:
    - "/config"
  packages: !include_dir_named packages
notify:
  - name: gmail
    platform: smtp
    server: smtp.gmail.com
    port: 587
    timeout: 15
    encryption: starttls
    sender: !secret my_e-mail
    username: !secret my_username
    password: !secret my_password
    recipient: !secret my_recipient
    sender_name: !secret my_sender_name
  - name: write_to_csv_gang_0v
    platform: file
    filename: occupancy_gang_0v.csv

My ‘File’ Integration reads:

Notify [/config/occupancy_gang_0v.csv]

My ‘Occupancy Gang 0V CSV’ Automation reads:

alias: Occupancy Gang 0V CSV
description: ""
triggers:
  - entity_id: binary_sensor.bd_gang_0v_occupancy
    trigger: state
    to: "on"
conditions: []
actions:
  - target:
      entity_id: notify.write_to_csv_gang_0v
    data:
      message: now(),states('binary_sensor.bd_gang_0v_occupancy')
    action: notify.send_message
mode: restart

The Automation indicates the Trigger works, however, nothing is written to the file ‘occupancy_gang_0v.csv’ in my ‘config’ folder.
Where do I make a mistake?
Many thanks in advance.

Dear Tom_l,
Apologies to bother you again with this apparently ‘solved’ CSV-write-problem.
I still can’t write any data to my occupancy_gang_0v.csv file.
FYI: all of my HA is up-to-date running on an Intel I3 NUC.
When I SAVE below YAML code as per your (v-dogg Veikko Mäkinen) instruction:

action:
  - service: notify.send_message
    target:
      entity_id: notify.write_to_csv_gang_0v
    data:
      message: "some,csv,data,to,write,here"

The YAML code is changed by HA to:

actions:
  - target:
      entity_id: notify.write_to_csv_gang_0v
    data:
      message: some,csv,data,to,write,here
    action: notify.send_message

When I activate the Automation to my mobile using:

actions:
  - action: notify.mobile_app_sm_g991b
    data:
      message: some,csv,data,to,write,here

the message “some,csv,data,to,write,here” is send perfectly to my mobile as expected.

In home-assistant.log I read:

2025-05-24 07:30:03.879 WARNING (MainThread) [homeassistant.helpers.service] Referenced entities notify.write_to_csv_gang_0v are missing or not currently available
2025-05-24 09:37:53.919 ERROR (MainThread) [homeassistant.components.notify] Error setting up platform file
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/notify/legacy.py", line 112, in async_setup_platform
    raise HomeAssistantError("Invalid notify platform.")  # noqa: TRY301
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant.exceptions.HomeAssistantError: Invalid notify platform.

In Developer Tools I see: notify.send_message.
I do not see: notify.write_to_csv_gang_0v.
Should I see: notify.write_to_csv_gang_0v in Developer Tools?
Is something wrong in the creation of: notify.write_to_csv_gang_0v ?
Any help is highly appreciated.

Yes you should see notify.write_to_csv_gang_0v. You don’t because it was not set up correctly:

Use the UI to set up your notification entity, see: Notify.file - Setup, so confused, please help - #2 by tom_l

Then my guess you have not done this:

https://www.home-assistant.io/integrations/file/#notifications

configuration.yaml file:

homeassistant:
  allowlist_external_dirs:
    - /config

Then clear the system log, do a config check, check the log is still empty (no errors related to your config) and restart home assistant.

Dear Tom_l,
Apologies to bother you again with this apparently ‘solved’ CSV-write-problem.
I still can’t write any data to my occupancy_gang_0v.csv file.
Have taken all steps per your instructions.
Log file remains empty after validation in Developer Tools.
After restart of HA I see below ERROR in Log file:

2025-05-26 03:14:31.106 WARNING (MainThread) [homeassistant.helpers.service] Referenced entities notify.write_to_csv_gang_0v are missing or not currently available
2025-05-26 03:16:40.302 WARNING (MainThread) [homeassistant.helpers.service] Referenced entities notify.write_to_csv_gang_0v are missing or not currently available
2025-05-26 03:26:03.426 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry Notify [/config/occupancy_gang_0v.csv] for notify
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 963, in async_unload
    result = await component.async_unload_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/notify/__init__.py", line 126, in async_unload_entry
    return await hass.data[DATA_COMPONENT].async_unload_entry(entry)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 200, in async_unload_entry
    raise ValueError("Config entry was never loaded!")
ValueError: Config entry was never loaded!

I guess the ‘unload_entry’ is the problem(?), however, I can’t figure out how to solve this.
Can you point me in the proper direction how to solve this?

I’m out of ideas, sorry.