Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95459

Help with python script run in command line sensor

$
0
0

@SteveDinn wrote:

Hey everyone. I’ve written a python script to scrape the covid-19 data for my local region(s) from the Canadian government’s website (because I have no clue how to use the scrape sensor).

It all works great when I run it as my HA user from within my python virtual environment. However, when I try to set it up as a command line sensor, it doesn’t work and I’m not sure why.

Here is the script itself. If you’re Canadian, this might be useful to you as well, if we can get it working. Just keep in mind that I’m no python developer, most of it has been cobbled together with bits from StackOverflow :slight_smile:

import json
from bs4 import BeautifulSoup
import urllib.request

webrequest = urllib.request.urlopen("https://www.canada.ca/en/public-health/services/diseases/2019-novel-coronavirus-infection.html")
html_bytes = webrequest.read()
html_data = html_bytes.decode("utf8")
webrequest.close()

header = ">Current situation</h2>"
start_table = "<table>"
end_table = "</table>"
header_start = html_data.find(header) + len(header)
table_start = html_data[header_start].find(start_table) + len(start_table) + header_start
table_end = html_data[table_start:].find(end_table) + len(end_table)

table = html_data[table_start:table_start+table_end]
rows = BeautifulSoup(table, "html.parser")("tr")
datarows = rows[1:]
data = [[cell.text for cell in row("td")] for row in rows[1:]]

dumpdata = dict()
for row in data:
    dumpdata[row[0].lower().replace(" ", "_")] = {
        "confirmed": row[1].replace("\n", "").replace(" ", "").replace(",", ""),
        "probable": row[2].replace("\n", "").replace(" ", "").replace(",", ""),
        "deaths": row[3].replace("\n", "").replace(" ", "").replace(",", "")
    }

json_data = json.dumps(dumpdata)
print(json_data)

When run on the command line, it outputs exactly what I want; basically just a JSON dictionary of all the provinces and their confirmed, probable, and death numbers:

(hass-3.8) hass@orion ~/config $ python ./python_scripts/corona_parser.py
{"british_columbia": {"confirmed": "617", "probable": "0", "deaths": "13"}, "alberta": {"confirmed": "358", "probable": "0", "deaths": "2"}, "saskatchewan": {"confirmed": "72", "probable": "0", "deaths": "0"}, "manitoba": {"confirmed": "11", "probable": "10", "deaths": "0"}, "ontario": {"confirmed": "588", "probable": "0", "deaths": "8"}, "quebec": {"confirmed": "221", "probable": "792", "deaths": "4"}, "new_brunswick": {"confirmed": "18", "probable": "0", "deaths": "0"}, "nova_scotia": {"confirmed": "51", "probable": "0", "deaths": "0"}, "prince_edward_island": {"confirmed": "3", "probable": "0", "deaths": "0"}, "newfoundland_and_labrador": {"confirmed": "4", "probable": "31", "deaths": "0"}, "yukon": {"confirmed": "2", "probable": "0", "deaths": "0"}, "northwest_territories": {"confirmed": "1", "probable": "0", "deaths": "0"}, "nunavut": {"confirmed": "0", "probable": "0", "deaths": "0"}, "repatriated_travellers": {"confirmed": "13", "probable": "0", "deaths": "0"}, "total": {"confirmed": "1959", "probable": "833", "deaths": "27"}}
(hass-3.8) hass@orion ~/config $

And here’s the YAML for the sensor:

sensor:
  - name: coronavirus_canada_ca
    platform: command_line
    command: python /home/hass/config/python_scripts/corona_parser.py
    scan_interval: 3600
    value_template: >
      {{ value_json.nova_scotia.confirmed | int }}
    json_attributes:
      - british_columbia
      - alberta
      - saskatchewan
      - manitoba
      - ontario
      - quebec
      - new_brunswick
      - prince_edward_island
      - nova_scotia
      - newfoundland_and_labrador
      - yukon
      - northwest_territories
      - nunavut
      - repatriated_travellers
      - total

Finally, here’s the error I get from HA’s logs. I tried upping the log level for the command line sensor to debug, but I only get one more entry that tells me that it’s about to run the command, so that was useless.

2020-03-25 11:19:25 ERROR (SyncWorker_9) [homeassistant.components.command_line.sensor] Command failed: python /home/hass/config/python_scripts/corona_parser.py
2020-03-25 11:19:25 WARNING (SyncWorker_9) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

I have no idea why the command is failing since it appears to work just fine under the same conditions that HA would call it. Any help is appreciated.

Posts: 15

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 95459

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>