Export sensor data to text file

Of course I want to learn, a lot!!
Currently I am not even an amateur about HA…

Yes I read the warning note, but I tried to use states(‘sensor.temperature’) instead of states.sensor.temperature.state but then I got the following error when configuring the automation via the GUI:

Message malformed: template value should be a string for dictionary value @ data[‘action’][0][‘data’]

The message that I try and get the error is the following:

service: notify.exporttxt
data:
  message: '{{states('sensor.bme280_temperature_3'}}'

whereas this message it actually works:

service: notify.exporttxt
data:
  message: '{{states.sensor.bme280_temperature_3}}'

That is the reason why I could not follow the warning note recommendation.

Your problem are the quotes (use different ones outside and inside the template),

service: notify.exporttxt
data:
  message: "{{ states('sensor.bme280_temperature_3') }}"
           ^                                           ^

and a missing closing bracket.

Your message that actually works, returns a state object and not the state.
This would work:

  message: '{{ states.sensor.bme280_temperature_3.state }}'

And another tip:
Check your templates in Developer Tools/TEMPLATE

1 Like

Thank you very much! finally got it running flawlessly!
I did not know what was the developer tools for, now I see how useful is to test your templates and others…

I am only one step away from victory now. I need the .txt file to have the only so that when the last state changes, this should be erased and the new one written in the same line, so that there is only one line of the state value.

So for example, if the last temperature was 11.5 and now changes to 11.3, the .txt file should only show:

11.3

instead of showing

11.5
11.3

Maybe there is a way to only read the last line in the Blue Iris Security software Macros but if I get the .txt file like that would be the best solution.

Do you have any suggestion?

Regards!

I use shell_command in an automation to rename my text files monthly. I imagine you could use the same process to delete the file before writing a new one.

I’m looking for a long term data storage solution for a few sensors and I wanted to understand how the notify to an external file works.

In my config I have the recorder setup to store only 7 days of data. If I use notify to write to the external file at the end of each day, will it be appending day #8 to #7? Or will I be only getting days #2 thru #8?

I’ve been playing with influxdb but would honestly prefer not to if I can avoid it.

It depends on which entity’s value you use. I use a history_stats sensor to give me a total “yesterday” value. I write that out at something like 1AM. This way I always get only one day’s worth of data.

Here’s how I’ve defined a history_stats sensor to give me yesterday’s total run time of my boiler’s burner.

sensor:
  - platform: history_stats
    name: Burner on yesterday
    entity_id: binary_sensor.boiler_burner
    state: 'on'
    type: time
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      hours: 24

@CaptTom,
I love your idea of using a shell command in an automation to rename or delete text files in a an automation. Would you be so kind to post an example? Thanks!

That binary_sensor.boiler_burner in the above shows on/off indicating whether my boiler burner is firing or not. In that example my history_stats sensor returns the total run-time of the boiler for yesterday.

Continuing with that example, a daily automation has an action which saves that previous days’ value to a text file

  action:
  - service: notify.burner_summary
    data_template:
      message: '{{ now().strftime(''%x'') }},{{ states.sensor.burner_on_yesterday.state
        }}'

The file containing these daily burner run-time values is defined using the notify integration in configuration.yaml:

notify:
  - platform: file
    name: burner_summary
    filename: burner_summary.txt
    timestamp: False

After a while there are a lot of lines in this file, one for each day. On the first of every month, I run this automation to rename that file, so next time the above runs, it’ll start a new file:


- id: id_burner_rename
  alias: run_burner_rename
  trigger:
  - at: 00:01:00
    platform: time
  condition:
  - condition: template
    value_template: '{{ now().day == 1 }}'
  action:
    service: shell_command.rename_burner_log
- id: id_boiler_zone1

The automation above references a shell_command I have defined in configuration,yaml:


shell_command:
  rename_burner_log: "mv -n burnerlog.txt {{ (as_timestamp(now(),0) - (60*60*24*27)) | timestamp_custom('burnerlog_%y_%m.txt') }}"

Eventually I get a number of monthly burnerlog files, and I’ll occasionally pull them off the HA device and save them somewhere long-term.

image

3 Likes

It’s not quite the same thing as what’s in this thread, but if you also landed here trying to find a simple way to export sensor data to a text file / CSV, here’s a custom component that makes it easy:

I tried the solution above, but my CSV file is not being created, and nothing is being exported from the sensors.

notify:
  - name: filenotify
    platform: file
    filename: Solar.csv
#    timestamp: true
- alias: sensor_values_to_file
  trigger:
  - platform: time
    at: '03:26:00'
  action:
  - service: notify.filenotify
    data_template: 
    message: '{{now().strftime("%d.%m.%Y")}};{{now().strftime("%H:%M:%S")}};{{ states.sensor.total_pv.state }};{{ states.sensor.solarman_daily_production.state }};{{ states.sensor.solarman_daily_energy_bought.state }};{{ states.sensor.solarman_daily_load_consumption.state }};{{ states.sensor.today_max_pv }};{{ states.sensor.max_pv_daily.state }};{{ states.sensor.max_pv_weekly.state }};{{ states.sensor.max_pv_monthly.state }};{{ states.sensor.max_pv_yearly.state }}'

May you please assist as I cant figure out what I am doing wrong?

I know this is from February. But, am new here and catching up.

Saw no answer, so I thought I’d chime in.

I believe I had to manually create the file for it to be used.