File Sensor: Plain Text File

What does the character - signify? No reading? No water in the river?

I wonder if giving the template a default would help?Or an if … then construct (if - then ignore)

The default value could be the existing (ie previous) value.

Something like (and I haven’t tried)

value_template: '{% set reading = value.split(",")[82] %}
                 {% if reading = '-'  %}
                     {{states('sensor.river_height')}}
                  {% else %}
                     {{ reading }}'
1 Like

Yeah, I believe it means no update, as I have seen 0 in the historical data.
I will give this a try when I get home, and let you know.
Cheers

I am really new to yaml - and the formatting is doing my head in.

I entered the code:

value_template: '{% set reading = value.split(",")[81] %}
    {% if reading = '-' %}
      {{states('sensor.River_Height')}}
    {% else %}
      {{ reading }}'

and got this error

I have tried:

sensor:
  - platform: file
    name: River_Height
    file_path: /config/downloads/river-gauge-data.csv
    value_template: >
      {% set reading = value.split(",")[81] %}
      {% if reading = '-' %}
      {{states('sensor.River_Height')}}
      {% else %}
      {{ reading }}
    unit_of_measurement: "m"

Which results in this error instead


And when I run the yaml check on it via deveoper tools, I get

I seem to have forgotten to put an {% endif %} at the end

Take a look at the first example here Templating - Home Assistant

Sorry for the error.

1 Like

Not at all, this has gotten me somewhere, and I am learning as I go.
Updated my last bit of code to have the endif with the curly brackets, and no errors from the code editor, but checking through developer tools, I get this warning

Code


# file sensor entry
sensor:
  - platform: file
    name: River_Height
    file_path: /config/downloads/river-gauge-data.csv
    unit_of_measurement: "m"
    value_template: >
      {% set reading = value.split(",")[81] %}
      {% if reading = '-' %}
        {{states('sensor.River_Height')}}
      {% else %}
        {{ reading }}
      {% endif %}


Configuration warnings
Invalid config for ‘file’ from integration ‘sensor’ at configuration.yaml, line 31: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘=’) for dictionary value ‘value_template’, got ‘{% set reading = value.split(“,”)[81] %} {% if reading = '-' %}\n {{states('sensor.River_Height')}}\n{% else %}\n {{ reading }}\n{% endif %}\n’

I indented, as per the link you provided, I think the indent on the { reading } (action bit) is right, but not indenting makes no difference to the warning.

I just noticed you have sensor.River_Height.

All entities are lower case. Try sensor.river_height.

Didn’t realise that the sensor was case sensitive, I corrected to match the name I gave it. But now the error is

It still refers to the capitalized version. I don’t know if this is the only problem, but that is still wrong.

You do realize you should be restarting home assistant after changing your yaml?

I apologise for being painful.
Yes, I am restarting HA after a change, not just reloading the yaml.

I have now change the reference to the sensor further up in the code (name), so that all references are river_height (lowercase), as I had changed it in the if statement.
And get the same error, with lowercase name now.

Invalid config for ‘file’ from integration ‘sensor’ at configuration.yaml, line 32: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘=’) for dictionary value ‘value_template’, got ‘{% set reading = value.split(“,”)[81] %} {% if reading = '-' %}\n {{states('sensor.river_height')}}\n{% else %}\n {{ reading }}\n{% endif %}\n’

# file parser entry
notify:
  - name: river-gauge-data-file
    platform: file
    filename: /downloads/river-gauge-data.csv
    timestamp: true

# file sensor entry
sensor:
  - platform: file
    name: river_height
    file_path: /config/downloads/river-gauge-data.csv
    unit_of_measurement: "m"
    value_template: >
      {% set reading = value.split(",")[81] %}
      {% if reading = '-' %}
        {{states('sensor.river_height')}}
      {% else %}
        {{ reading }}
      {% endif %}

I think I got it

{% if reading == '-' %}

Note the ==

Progress, I think.
With the ‘==’ in place, a different error. One that to me signifies that it is not handling the '-" and still trying to read it as a int and than a float. I think

Log details (ERROR)
Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:368
Integration: Sensor (documentation, issues)
First occurred: 2:39:48 PM (1 occurrences)
Last logged: 2:39:48 PM

Error while setting up file platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 639, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '"-"'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 642, in state
    numerical_value = float(value)
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: '"-"'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 368, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 752, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1282, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 945, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1066, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1003, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 951, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 646, in state
    raise ValueError(
ValueError: Sensor sensor.river_height has device class 'None', state class 'None' unit 'm' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '"-"' (<class 'str'>)

Thought that maybe if I initialise the value/reading, it would pick up from there. So manually set the ‘-’ in the csv file to ‘0.52’ by copying and pasting a value from another column, and get the same error - but only the float one.

Perhaps if you give me the URL of the resource you are grabbing it may help.

1 Like

The address is
https://www.data.brisbane.qld.gov.au/data/dataset/01af4647-dd69-4061-9c68-64fa43bfaac7/resource/78c37b45-ecb5-4a99-86b2-f7a514f0f447/download/gauge-data-20240130t111704.csv?fbclid=IwAR1tfov695aLdV37OJdsEJ2P9hMOTcZuS5p24mG3B2IgrN8ps0ENgs8nG1c

And I have an automation downloading it every 5 minutes (as when it is flooding, it is read frequently).


I hope this helps, and thanks for all the help so far.
The column I am after within the csv is E1702, or the data after the 81st comma. But if you get it working for any, then we should be able to adapt.

Looking further, there does seem to be a data api of some sort (well out of my wheelhouse), and some information can be found at

There will now be data in the file… we are having a rain event.

http://www.bom.gov.au/fwo/IDQ65389/IDQ65389.540122.plt.shtml

Any thoughts?