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 }}"
3 Likes

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