I am new to HA. I have installed hass.io in a VM on Windows Server and everything is running. I have ssh access to the Home Assistant shell. I’m a little confused with hass.io/supevisor/ha itself and this is the first time I’ve seen yaml.
I would like to bring in some air quality information as a sensor into HA. I have a local station that publishes the raw pollutant data and I have written a short Python script to scrape it and process it to calculate some indices (it’s a piecewise linear function with some choice of maximal params). I can output this to the shell or a text file, or whatever is best. One thing to note is that I have multiple data from each scrape (levels of different pollutants) and I would like to feed them to different sensors (attributes?) in HA.
I would like to understand a hassionic way of bringing this in as a sensor. I can see a few scenarios:
Scrape directly in HA through the scrape sensor and replicate the mathematical operations in HA. Not sure if this is possible/appropriate in YAML. Is there some better method?
use the command_line sensor to run the python script and get its output. I don’t see how to get multiple sensors out without running the scrape multiple times and filtering the output differently for each sensor. Might get annoying for the data server.
Run the python script as a cron job every 15 mins, outputting to a text file and use the file as a data source for the sensor. Where would I set up cron? It doesn’t seem available in the HA shell, Maybe it needs to be at a lower level in the VMs? How do I access it?
Then there are some things in HACS called appdaemon and netdaemon: may this is a better way of achieving what I want? Haven’t investigated that part of HA yet.
But I may be barking up the wrong tree completely. Advice much appreciated!
There is no API at the weather station, so I have to scrape the data from a table on a webpage.
The problem is that the data are raw quantity measurements and I have to apply some simple arithmetic to get the index I want to present in the Dashboard.
Can I do algebraic transformations on scraped data within HA? It’s just a bunch of min/max functions and multiplying by constants, so it’s not very complicated, but I don’t see where I could do it.
Algebraic operations can be done using Template Sensors or predefined Min/Max or Statistics sensors, that consume other sensor’s values (like your scrape sensor).
here’s a simple script, python_script.overview_entities:
count_all = 0
domains = []
attributes = {}
for entity_id in hass.states.entity_ids():
count_all = count_all + 1
entity_domain = entity_id.split('.')[0]
if entity_domain not in domains:
domains.append(entity_domain)
attributes['Domains'] = len(domains)
attributes['--------------'] = '-------'
for domain in sorted(domains):
attributes[domain] = len(hass.states.entity_ids(domain))
attributes['friendly_name'] = 'Entities'
attributes['icon'] = 'mdi:format-list-numbered'
hass.states.set('sensor.overview_entities', count_all, attributes)
you can run a python script with the service python_script.name_of_the_script which will show up after you have saved the script, and reloaded the python scripts with the service python_script.reload
use the service in a HA script or automation:
run_after_delayed_startup:
alias: Run after delayed startup
mode: restart
sequence:
- service: python_script.average_indoor_temp
# give even extra time for system to settle before doing:
- delay:
seconds: >
{{states('input_number.ha_delayed_startup')|int}}
- service: python_script.overview_entities
- service: python_script.overview_components
- service: script.update_entities_uun
Thanks for the pointers. So I have studied the Jinja2 template language and it turns out it’s quite flexible. In case anyone else is looking a this in the future, I leave here my implementation of a simple piecewise linear conversion of pollutant concentration to a CAQI subindex as a macro (function) in the template language