How can I read a value from a file on my PC into Home Assistant?

Hi All,

I want to run a python script daily which calculates a value to supply as an input number into home assistant.

Is it possible to read a value from a csv (or txt) file written to a location on my PC.

Thanks in advance,

Will

Would this work

1 Like

Take a look at the file integration:
File - Home Assistant

Has to be set up via the GUI once you installed it - and youā€™re looking for the File Sensor option once you trying to set it up:

image

Doesnā€™t this require the file to be in the home assistant environment though?

Mine is on my desktop. Outside the VM Iā€™m running HA on.

Thatā€™s where mine is, correct.

I use the Samba Add-On if I need to manipulate/store the file there from my PC, though.
Never even thought about storing it anywhere else, because the HA system is the only user of this file.

So Iā€™m trying write a file to my C drive and automate the import and subsequent assignment of the file contents to an input numberā€¦

Why are you not writing it to the HA Samba location directly?

Or set up, e.g. Syncthing between your HA instance and your PC.

Hadnā€™t heard of Syncthing before now, looks ideal. Thanks. Iā€™ll give it a go and report back.

Iā€™d started looking at pyscript to run the code inside HA but that got very fiddly. So I just googled importing a file in HA which lead me to the File integration.

Just a hint:

One thing I experienced when setting up Syncthing was strange.

I didnā€™t want the option ā€˜Global Discoveryā€™ to be enabled on my HA machine or on my NAS, so I unchecked the box in the settings:

Seemed like it prevented the two devices to connect, so I enabled it in both places, waited for the sync to start and disabled it again. Seems to be working fine now.

1 Like

Failed at the first hurdle. I installed syncthing on my PC but the HA integration is not accepting the API Keyā€¦

Guess, I should have been more specific:
You need to install the Syncthing add-on on your HA machine to have the files synced.
The integration that you need the API key for is only monitoring an existing installation on either your HA machine or another place.

1 Like

Just curious: Did you get it to work?

Samba share did the trick, I can now write a file to the config folder.

So hopefully I can use the File integration to update a sensor based on the content of the file and schedule a job to to create the file on the network drive that is mapped to the Samba share.

I need to have another read on the Syncthing set up, I assumed the add-on installed Syncthing onto HA.

Good to hear you got it sorted out.

Got to keep in mind:

  1. The Syncthing Add-On installs the Syncthing application onto your HA machine.
  2. The Syncthing Integration allows you to monitor a Syncthing installation from HA, no matter if that installation is on the same HA machine or e.g. your PC or NAS.

You only need an API Key for 2), so thatā€™s why I thought you had the integration installed, instead of the add-on.

1 Like

So Iā€™ve just been migrating to VMWare after having teething issues with Virtualbox on a new machine. I have it stable and running.

Iā€™m now trying to setup a File sensor with the File integrationā€¦

Screenshot 2024-12-27 164831

Updating the example from Documentation page I think I need the following

  • Name: ā€˜Price_per_kwhā€™
  • File path: /home/user/.homeassistant/config/export_slots.csv
  • Value template: ā€˜{{ value.split(ā€œ,ā€)[1] }}ā€™
  • Unit of measurement: ā€œGBP/kwhā€

However when I try to add this to my config

Screenshot 2024-12-27 170912

and do a yaml restart I get this error

Integration error: allowlist_external_dirs - Integration 'allowlist_external_dirs' not found.

So I clearly have a syntax error in referencing the config folder. Can anyone shed any light on how to reference in both the allow_external_dirs entry in my config and the File sensor set up if they differ.

Cheersā€¦

/homeassistant/config

Isnā€™t working eitherā€¦

Unless you actually need a file, sending the data using a rest call to a webhook right from the python script might be easier. HA would get the information instantly instead of needing to poll the file (which the python script might be writing simultaneously).

1 Like

Webhook is an alternative solution which Iā€™ll definitely look at, but I feel like Iā€™m just the right syntax on the file path away from automating what I want.

I donā€™t think youā€™d need the allowlist at all - I think I have it in my configuration.yaml for another purpose (but donā€™t remember at the moment what it is :confused:).

But if you do need it, it needs to be under a ā€˜homeassitant:ā€™ topic like this so itā€™s identified as a configuration setting, not an integration as your error indicates:

homeassistant:
  allowlist_external_dirs:
    - /media
    - "/config"
1 Like

Looks like Iā€™m not the only one. Webhooks 101 here I come!

Literally 3 lines of code and a webhook payload to trigger an automation did the job.

Step 1 Define a webhook string in an automation, with ā€œWebhook payload has been receivedā€ as the trigger. Add "{{ trigger.json.**boost_rate** }}" to the value of the input number I wish to amend.

Step 2

Use the webhook string from step 1 to formulate a url, define a key:value pair using the same key used in Step 1, pass these as arguments into requests.post. The value ā€œboostā€ is calculated earlier in my script.

webhookurl='http://my_homeassistant_ipaddress/api/webhook/webhook_string_from_step1'
data = { '**boost_rate**':boost}
r = requests.post(webhookurl, data=json.dumps(data), headers={'Content-Type': 'application:/json'})

webhooks from python

webhooks with Home Assistant

1 Like