Loading Sensor Data from Text files, with Date-Times

I get a CSV file from my home’s weather station on ten-minute intervals with records having the local date-time, and a series of weather metrics as of that time. I want these data in sensors. Loading the data is not the issue, the time-stamp is the issue.

How do I load a data value with its time-stamp into a sensor from a CSV or a JSON file so HA knows that value A was created at time D?

I see the File Sensor, but its examples omit date-times from the feed. I can turn the CSV into JSON if needed.
Thx

what do you mean ‘with it’s timestamp’? Are you saying that the filename has a timestamp on it or the data itself is paired inside the file with a timestamp?

Each record has local date-time, and a series of comma separated metrics as of that date time. I want to import the various metrics into HA with HA knowing that each value is as of the record’s date-time. It appears that if use a File sensor that the values are time-stamped as of when imported. I need to import:
{{date-time}}, {{value}} and have HA know that value is as-of date-time.

can you post the contents of the file?

Here’s the CSV version (date, time, temp, humidity, etc):
3/29/2019,7:30 PM,71.00,51.50,0,73,51.00,51.00,43.00,30.24,45.0,0.00,1.24,0.58,0.00
I’m happy to reformat, but every 10 minutes another record is posted

Home assistant records the time that it senses data.

You’ll have to have it as 2 separate sensors, 1 for the time it was updated. 1 for each other variable you want to pull out.

This will get you the date as a datetime object, but it’s kinda pointless because it will be a string in the sensor.

sensor:
  - platform: file
    file_path: /home/user/.homeassistant/sensor-data.txt
    value_template: >
      {{ strptime(value.split(',')[:2] | join(', '),"%m/%d/%Y, %H:%M %p") }}

This will be any value you want to peel out, just change the number 2 to any other index. 2 in the example you provided is the first number after 7:30pm, 71.00.

sensor:
  - platform: file
    file_path: /home/user/.homeassistant/sensor-data.txt
    value_template: >
      {{ value.split(',')[2] }}

This is the only way to get the date into home assistant. As @nickrout said, it will not be usable inside your history or show up in your logs at that time.

Thank you both - wow, code and all on the fly. HA, and you guys, rock. Looks like I need to have some near realtime processing to get my metrics loaded near to their time-stamps.

Thanks @Petro.I recall now (and as seen in your example), that these are zero-based arrays. Its working great. Here’s one of the sensor configs

- platform: file
  name: 'Acurite Barometric Pressure'
  file_path: /config/externaldata/acuriteweatherdata.txt
  value_template: >
    {{ (value.split(',')[9] | float) | round(2) }}
  unit_of_measurement: 'inHg'

Just to complete the thought, I added:

  whitelist_external_dirs:
    - /config/externaldata

to the homeasssitant: section of my configuration.yaml file for my hass.io installation.

2 Likes

ciao, ho visto questo post e fa proprio al caso mio, infatti io devo leggere dei dati presenti in un file in formato .csv solo che il file invece che in locale ce l’ho dentro un server web tipo: https://raw.githubusercontent.com/andamento.cvs
come faccio a connettermi a questo file ?