Export sensor data to text file

I am trying to export the output of 3 temperature sensors and a relay to a csv file at a regular interval (like every 10 minutes). I know that the recorder and history has this information in a sqlite database - but to avoid that database getting too large I have to purge every 24 hours.

I would like a continuous running store for years of these temperatures so I can run some machine learning on it for predicting one of the temps based on the other two and the state of the relay. But I need a lot of data for that :).

Thanks,

Tim

2 Likes

I use influxdb for storing long term data - 18 months worth so far. I haven’t tried, but I would think you could export that data to a text file.

You can use the file notification for this.
All you have to do is build the message like you want.
eg:

notify:
  - platform: file
    name: filenotify
    filename: /your/path/to/file.csv
    #timestamp: true

automation:
  - alias: sensor_values_to_file
    initial_state: 'on'
    trigger:
      - platform: time
        at: '23:59:55'
    action:
      - service: notify.filenotify
        data_template: 
          message: "{{ states.sensor.test_sensor1.state }};{{ states.sensor.test_sensor2.state }}"

Not tested, just play with it. :slightly_smiling_face:

17 Likes

That works great thanks! At first I missed the .state part and logged quite a bit of junk :smile: but now it works perfectly!

You use hassio and influxdb addon?

I just have an old hasbian installation.

what would be every hour automation?

thanks.

How would you add a time stamp?

EDIT, found

notify:
  - platform: file
    name: filenotify
    filename: /share/history/10sec_power_reading.csv
    #timestamp: true

automation:
  - alias: sensor_values_to_file
    initial_state: 'on'
    trigger:
      - platform: time
        seconds: 10
    action:
      - service: notify.filenotify
        data_template: 
          message: '{{ states.sensor.efergy_802171.state }}; {{now().strftime("%Y%m%d-%H%M%S")}}'
2 Likes

Also, if you want to make csv replace the ; with ,.

Here is what worked for me (every 5 min):

notify:
  - platform: file
    name: filenotify
    filename: /home/homeassistant/.homeassistant/training.csv
    timestamp: true

automation:
  - alias: sensor_values_to_file
    initial_state: 'on'
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    action:
      - service: notify.filenotify
        data_template: 
          message: ", {{ states.sensor.outside_air_intake_temperature.state }},{{ states.sensor.dark_sky_temperature.state }},{{ states.sensor.dark_sky_humidity.state }},{{ states.sensor.dark_sky_cloud_coverage.state }},{{ states.sensor.dark_sky_uv_index.state }},{{ states.sensor.dark_sky_wind_speed.state }}"
2 Likes

a curiousity, why do you keep track of those dark sky values?

The first value is the temperature inside my fresh air intake for the house - the dark sky relates to outside temps. When I open my fresh air intake it takes air passively through the intake (as the house HVAC runs some fresh air comes in) - so the temp of the intake air is not usually the same as the outside air. I want to train an regressor that can predict intake air temperature based on outside conditions (like if it is sunny the roof will be hot and therefore the air that is taken in will go through the hot attic before coming in). This way I can open the air intake and draw in air only when the air that comes is close to the temp of the house. So I can get fresh air without having to worry about heating or cooling the house with it.

3 Likes

This is my current implementation - just looking at temp over the day and figuring out the times best to open (green) and close (red) the air intake. But my temp sensor in the air intake shows that the intake temp is very different from the forecasted temps…

1 Like

I know this is an old thread but it’s exactly what I am trying to do. I am getting the error message

" Invalid config for [automation]: [minutes] is an invalid option for [automation]. Check: automation->trigger->0->minutes. (See /config/configuration.yaml, line 190)."

I also have all the below in the confi.yaml

notify:
  - platform: file
    name: filenotify
    filename: /config/TEMPHISOTRY.csv
    timestamp: true

automation:
  - alias: sensor_values_to_file
    initial_state: 'on'
    trigger:
      - platform: time
        minutes: 5
        seconds: 0
    action:
      - service: notify.filenotify
        data_template: 
          message: "{{ states.sensor.lumi_lumi_weather_640d3002_temperature.state }};{{ states.sensor.netatmo_meadowcroft_outdoor_temperature.state }}"

You need the time-pattern-trigger.

Thank you, that got rid of the error message but still nothing is being written to the file. Any ideas? This is setup to write the data to a file every 5 minutes, so this will not pull history just the data going forward?

I spoke to soon, the moment I posted that reply it wrote data to the file.

It’s only exporting once an hour not every 5 minutes. Thoughts on why?

2020-09-08T22:05:00.010603+00:00 67.6;84.9
2020-09-08T23:05:00.011470+00:00 67.6;84.9

It needs to be: minutes: "/5"

Hopefully last question. Ideas on why the timestamp would be 5 hours off in my export. My home assistant is showing the correct time, but the export is showing as below. As a reference it’s 9/8/2020 7:30PM here now.

2020-09-08T23:55:00.010952+00:00 68.4;84
2020-09-09T00:00:00.010803+00:00 65.7;84
2020-09-09T00:05:00.009439+00:00 65.7;84
2020-09-09T00:10:00.008813+00:00 65.8;83.8
2020-09-09T00:15:00.012108+00:00 67.8;83.8
2020-09-09T00:20:00.011138+00:00 68.4;83.7
2020-09-09T00:25:00.011668+00:00 65.5;83.7