Read/Write Data to/from txt/csv file

Apologies, but I need a dummy explanation for the “platform: file” thing.

I have a sensor, sensor.t_h_sensor_2_temperature_2
I made a 24h average out of this, sensor.24h_temp_blaueshaus_linear
(Sorry, German involved)
I store that sensor value everyday at 0300 am using an automation

alias: AAA 24Temp OnceADay 0300
description: ""
trigger:
  - platform: time
    at: "03:00:00"
condition: []
action:
  - data_template:
      entity_id: input_number.aa_temp_24h_0300_morgens
      value: |
        {{ states("sensor.24h_temp_blaueshaus_linear")|float }}
    service: input_number.set_value
mode: single

All of this, so far, works fine.

Now, I would really like to enter this 03:00 sensor value into a csv file, either replacing or adding to preexisting data.

I have read this and this cerntainly helpful threads on the topic, but I’ve understood no further than “use the platform: file” thing, the documentation of which comes with funny instructions like “specify file path, e.g.:”

/home/user/.homeassistant/sensor-data.txt

and while I managed to get network access to my HA thanks to some very nice people on youtube, I don’t see a “home” direcory, or a “user” directory, and certainly not a -homeassistant directory. let’s say I want to put the data in /config/www/samplefile.csv, because I can access this directory.

What sourcecode do I have to put where, exactly?

Please, pretty please, I really need the dummy explanation. As in "In your config.yaml, add (THIS), in your sensors.yaml do (THIS), then make an automation that does yaml (THIS).

Thank you in advance. And how do I automate this every time a sensor value changes? How do I either add to the file or replace the value? Can I limit that to the last 72h?

Essentially, my ultimate goal is to register every time an automation actually triggers, I want a logfile of that.

1 Like

In your config.yaml, add this:

notify:
  - platform: file
    name: blaueshauslog
    filename: blaueshauslog.txt
    timestamp: True

You can also use timestamp: False if you prefer to put your own date and/or time format in the output file, or don’t want one at all.

Then, in your automation, add another action:

  action:
    - service: notify.blaueshauslog
       data_template:
        message: "{{ states('sensor.24h_temp_blaueshaus_linear')|float }}"

You might need to play with the order and indentation in this new action. There are so many ways to order YAML statements that it can get confusing.

This will create a new file called blaueshauslog.txt in your config folder, and add a new line to it each time the action is triggered.

Thank you, works perfectly.

Just for anyone who found this, like I did, recently -

:warning: The File YAML configuration is being removed in version 2024.12.0

You should set up file “notifications” in the UI using Add IntegrationFile

and then use the generic notify service to add to it -

service: notify.send_message
target:
  entity_id: notify.blaueshauslog
data:
  message: "{{ states('sensor.24h_temp_blaueshaus_linear')|float }}"
7 Likes

Good catch, thanks for updating this thread with the new definitions!

why i have this error


any idea ? im trying also to save a state to a file like json txt or whatever

I’ve used the file integration to read data from a file but don’t see a way to write or update that data. Is write possible?

You can write (append) to a file. Here’s how I append the daily runtime of my heating system boiler to a file:

  actions:
  - action: notify.send_message
    data:
      entity_id: notify.burner_summary
      message: "{{ now().strftime('%x') }},{{ states('sensor.burner_on_yesterday') | round(2) }}"
1 Like

Is burner_summary the name of your file?

What I’m wanting to do is write a “1” to sensor.file_1 as it is called in the integration.

burner_summary is an entity created in the File integration. This is now done in the UI, not in YAML any more. So old threads on this may not be helpful.

You can put whatever you want in the “message:” line. As far as I know that would append that to the bottom of the file, not replace what’s already there.

1 Like

Thanks. I guess where I’m not clear is on the notify.burner_summary. I created a file and can see the data that is in there but it’s not associated with any notification entity for me to write to.

I found the issue. I was creating a file and not a notification. That works.

1 Like

I have followed the steps here, but I am still not able to create a text file.

Here are the steps I have done:

  1. Created the folder and set the permissions as 776 (I don’t care which folder it is, I just need a folder to hold the text file)
  2. Added it to the allow (configuration.yml)
  3. In Integrations, added a file and set the path, then rename it…this seems very strange, shouldn’t there be a file path, file name, and overall name?
  4. Created an automation, for now just trying to write out anything:
    alias: WriteFileTest
    description: “”
    triggers:
  • trigger: time
    at: “16:11:00”
    conditions:
    actions:
  • action: notify.send_message
    metadata: {}
    data:
    message: “"Test"”
    target:
    entity_id: notify.FurnaceOpTime
    mode: single

Any suggestions as to where I am not understanding this?

Looks right to me. Not sure what I’m missing.

For troubleshooting steps, you might try a few things:

  1. Instead of a sub-directory, put the file right in /share.
  2. Instead of /share, try /config
    (That “share” directory has some special considerations in HA.)
    (You’d have to make these changes in both the File integration and the allowlist.)
  3. Use Developer Tools / Actions to test the notify.send_message syntax before trying it in an automation.

Thanks Tom, I got it to work now. I must say that the Developer Tool → Action works very well. From that, I was able to determine a few things that I will note here in case it helps someone in the future.

When you create a File (Integration) and add an Entry (“Setup Notification Service”), it will ask for the file path, ensure to include the path and the name of the file you want (Note 2 on the enclosed screen capture). An important note is that once you set that file path and file name, it can’t be changed (so it seems, correct me if I’m wrong on this). Next, rename the Entry (Notes 4 and 5) to match what you have in the entity ID (in this case shown on the screen capture, it would be NotifyName, entity_id: notify.NotifyName). You can leave the file path in the square bracket as a reminder to the file path and file name used, but that is only a comment/note, it doesn’t control the file path and name).

I used the /share folder as it seemed to be one that allowed files to be created. There are likely other folders that work, but I’ll go with it for now.

Interesting. I’m not a big fan of “dumbing things down” to use the UI instead of a config file. This would seem to be a good example of why.

Just for fun, I went digging to see where these file entities end up being defined. It’s in the .storage directory, in a file called core.config_entries. One line in that file contains (for example) the string {"file_path":"/config/burner_summary.txt","name":"burner_summary","platform":"notify"}

This seems to confirm your finding that the name and the file path can differ. I recall having to delete and re-add these file notify entities a few times myself, now that you mention it.

Thanks Tom, yes a few strange things with the UI. It would be better if they’re going to use a GUI, that it has the ability to modify the file path and other settings.

I don’t have the .storage directory you noted, perhaps you’re using HA in a Docker? Also, a strange thing is that in my automation I have to use entity “file” and not what I named the file (furnaceoptime) Click on entitiy and then click on the “Cog wheel” settings and change the name there. Very convulated indeed but at least I have figured it out!

At least it works and is updating the text file correctly.

Chris

Thanks for posting what you’ve found!

FYI, the .storage directory is there, just hidden (hence the “.” in front.) It’s hidden because we, as users, aren’t supposed to touch it. And I don’t. But I figured that had to be where the settings are for the notify file integration, and at least we can see what HA is seeing.

The key word is “see.” Don’t try to edit anything in there, you could really damage your HA install.

1 Like