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
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') }}"
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?
Correct on all points. Do note the limitation of the file sensor though:
Only the last line of the file is used
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
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 alwaysnotify.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: