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 }}"
4 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) }}"

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.

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