Cox Communications - Internet Usage

With Cox Communications introducing a 1TB data usage limit this month I decided I needed to track it a little closer.

I did a little research and found it was easier than I expected to get into HA. I’m by no means any good at Python, but it’s working.

If you have Cox for your internet you can view the project here: Github - Cox_Data_Usage

HA_Example

5 Likes

Ok!

I would love to get similar data from my ISP here in Australia! Could you help out?

I’ve found a project that gets similar data from my ISP: https://github.com/aleksw/iinetpy/blob/master/iinet.py

Could we swing something together do you think? Maybe even make it somewhat modular so that others could create their own python backends to read in the values you need to place in the component UI?

Thanks!

This is great, thanks!

1 Like

Ya, I’ll take a look tomorrow, like I said I’m so python guru. If you run that script you linked now what does the output look like? If it’s something usable all you’ll need to do is get it into a JSON format then let HA consume it.

Looks like Cox changed the content so I had to modify the code to find the script. Github updated.

I need to find a better way to find the text/javascript element which has the var utag_data variable in it.

Edit: Just updated the code again with a much better way of finding the data element we need in the head. Update your code.

Due to my inability to figure out how to upgrade Python 2 inside the Docker version of Home Assistant (I believe I was hitting an outdated OpenSSL issue), I ended up revising the script to use Mechanical Soup, since Mechanize is no longer being actively developed.

Looks like your revisions are along the same path as I took when I noticed Cox changed their code and the position of the needed script element moved.

I also decided to output the JSON to a file, so the sensors don’t need to trigger recursive connections to Cox’s site.
Here’s my revision, in case anyone finds it useful.

1 Like

Thanks, i’ll check out mechanicalsoup.

I like the idea of outputting to a JSON file so it doesn’t hit Cox multiple times. I’ll implement that too.

@alexb What does the HA config look like for reading from a file?

Thanks!

@ntalekt I’m still struggling to get my IoT VLAN to send requests to Cox out the Cox WAN, rather than the VPN WAN, because Cox seems to refuse authentication requests from my VPN connection… so I haven’t implemented the HA side yet, but I was intending to use HA’s RESTful Sensor. I think you could output the JSON file into HA’s www folder, so HA would serve up the file itself.

I had the same problem before changing my network architecture and where I terminate my VPNs. In the past I had a DD-WRT router and was able to to setup policy based routing via IPTABLES. That why I could choose which devices traversed the VPN vs. the normal WAN.

Figured out my firewall config issues and knocked out the rest of the integration.

The TL;DR of it is:

  • Automation triggers once every hour
  • Automation calls a shell_command for the python script
  • Python script scrapes the utilization data and writes the JSON to a file
  • File Sensors read the JSON file and make the data available in HA

A few things learned:

  • The File Sensors choked on the JSON output when “indent=4” was in the json.dump parameters. So I removed that.
  • My Cox billing cycle restarts today. Got to see that edge case difference in their data. Added conditionals to the File Sensors to cover that.
  • The File Sensor component doesn’t have icon_template functionality. I may abstract the File Sensor data into Template Sensors so that I can have conditional icon display for when my data is running low.

Would it be possible to build this into a HASS.io add-on?

I’m sure it’s possible but I haven’t made the transition to hassio yet so I’m of no help.

Thanks for this, nice way to track ISP utilization – I forked this for my ISP, CableONE as well on GitHub – I might recommend putting some of your values in sensor.template as I did – maybe a little easier to read and one less json read and parse, but probably fine either way.

Walt

Trying to tie this into my setup. Currently having a problem with the shell command running. I’m using

- id: query_cox_data_usage
  alias: Query Cox Data Usage
  initial_state: 'on'
  trigger:
    platform: time
    minutes: '/60'
    seconds: 00
  action:
    service: shell_command.query_cox_data_usage

To run the script every hour, but it’s not working. I get the following outputted to my home-assistant.log

2018-07-18 17:40:03 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `python /home/homeassistant/.homeassistant/cox_usage.py`, return code: 1
NoneType: None

If I run manually from the home-assistant virtualenv it updates fine? Any ideas why it isn’t running when Home Assistant triggers it?

Updated my configuration to call the update script from cron and that worked for a while but now it’s failing with:

Traceback (most recent call last):
  File "/cox/cox-data-usage.py", line 42, in <module>
    login_form.input({'username': cox_user, 'password': cox_pass})
  File "/srv/cox/lib/python3.6/site-packages/mechanicalsoup/form.py", line 59, in set_input
    i = self.form.find("input", {"name": name})
AttributeError: 'NoneType' object has no attribute 'find'

I imagine this is a missing module on the container that this is running on but I can’t find the local change that introduced this failure. Any ideas?

Is it possible to get this working in HASS.IO?

Wow i wish i searched here before i made my own sensor.
No big deal, but i plan on building out out a little more to include all the info that this example contains.
If others want to use my sensor feel free to.

2 Likes

Thank you this custom component is working great!

I would like to display this in a graph. Maybe in Grafana so I can quickly see how we are doing. So a line with average usage and then where we are in relation to that, so we can quickly see if we are pushing it. Any suggestions?