Need help reading data from file sensor

So I have been able to setup my config.yaml to read the .CSV file but it only seems to want to read it as a string. What I see on the front end is “77.7” °F, rather than 77.7 °F

sensor:
  - platform: file
    name: Acurite Outdoor Temperature
    file_path: external_data/AcuRite Weather Station/acuriteweather.CSV
    value_template: >
      {{ (value.split(',')[1])}}
    unit_of_measurement: '°F'

using the above in the config will read the values from the file. However, they are being read as a string rather than an integer or a float.

sensor:
  - platform: file
    name: Acurite Outdoor Temperature
    file_path: external_data/AcuRite Weather Station/acuriteweather.CSV
    value_template: >
      {{ (value.split(',')[1] | float)}}
    unit_of_measurement: '°F'

If I use the | float as I have seen in other examples it reads nothing and comes back with a 0°F. Rather than “77.7” °F. So it does turn it into a number but then reads nothing.

In the other examples I have seen they wrote it as:

{{ (value.split(',')[1] | float) | round(2)}}

I am not very good with the template aspect of HA and nay help would be appropriated.

Can you paste an example of your CSV data?

As I had the same thought as I got up to get a drink. Thank you for the reply.

Old:

"3/19/2020 11:00:00 AM","77.7","78","69","80","78","29.32327","3.83","3.728227","3.106856","5.592341","112.5","76.4","62"

New:

3/19/2020 11:12:00 AM,76.1,81,70,76,76,29.32327,3.83,2.485485,3.106856,5.592341,112.5,76.2,62

I may have to run a script to remove the quotes from the .CSV file. I have had to do this for a few things at work. Though most system are smart enough to ignore the quotes in .CSV files.

Now it is reading properly.

Now my only question is how often does HA read the file? My Weather station outputs every 12mins. so Easy enough to run the cleaning script every 12 mins.

You don’t need the script. Try this template:

value_template: >
  {{ (value.split(',')[1]|replace('"','') }}

But FYI from the sensor component source the update for polling sensors is 30 seconds (by default):

SCAN_INTERVAL = timedelta(seconds=30)

It is possible to change this.

2 Likes

Thank you thats what I needed.

That is actual very similar to how I do it in my script.I am running HA in Hyper-V on Windows Server so I would have used a PowerShell script.

$DataRaw = Get-Content -Path 'X:\AcuRite Weather Station\acuriteweather.CSV'
$DataClean1 = $DataRaw -replace '"' , ''
$DataClean1 | Set-Content -Path 'X:\AcuRite Weather Station\acuriteweather.CSV'

Which does also work but if I can do it all from inside HA that would be much easier.

Again thank you. You just made my day.

2 Likes

Thanks a lot, this post helped me too. In case if someone else is trying to setup had to make a small correction.

sensor:
  - platform: file
    name: Acurite Outdoor Temperature
    file_path: /media/AcuRite Weather Station/acuriteweather.CSV
    value_template: >
        {{ (value.split(',')[1])|replace('"','') }}
    unit_of_measurement: '°C'
1 Like

Hello I hope someone could help me or give me a hint.
I think it has something to do with the german version or keybord layout, bcause i cant replace “”" with “”.

Here is my config.yaml of the file sensor.

sensor	
  - platform: file
    name: blnet-kollektor
    file_path: /share/blnet.csv
    value_template: '{{ value.split(";")[0] | replace(""" , "")}}'

I get the following error:

Invalid config for [sensor.file]: invalid template (TemplateSyntaxError: unexpected char '"' at 40) for dictionary value @ data['value_template']. Got '{{ value.split(";")[0] | replace(""" , "")}}'. (See ?, line ?).

but this version doesnt work either:

sensor	
  - platform: file
    name: blnet-kollektor
    file_path: /share/blnet.csv
    value_template: '{{ value.split(";")[0] | replace('"' , "")}}'

So maybe someone knows which character i have to use instead of the "

Thanks in advance!