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: ""
  - platform: time
    at: "03:00:00"
condition: []
  - 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.:”


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:

  - 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:

    - service: notify.blaueshauslog
        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.