Create sensor from curl

Won’t read from a file

So in my bash script, these extra lines:

grep "down1" /config/abbusage.log | sed 's/<down1>/{"download":/g' | sed 's/<[/]down1>/}/g' > /config/abbdownload.log
grep "up1" /config/abbusage.log | sed 's/<up1>/{"upload":/g' | sed 's/<[/]up1>/}/g' > /config/abbupload.log
grep "allowance1_mb" /config/abbusage.log | sed 's/<allowance1_mb>/{"allowance":/g' | sed 's/<[/]allowance1_mb>/}/g' > /config/abballowance.log
grep "lastupdated" /config/abbusage.log | sed 's/<lastupdated>/{"updated":"/g' | sed 's/<[/]lastupdated>/"}/g' | sed 's/[ ]/T/g' | sed 's/T{/ {/g' > /config/abbupdated.log
grep "rollover" /config/abbusage.log | sed 's/<rollover>/{"rollover":/g' | sed 's/<[/]rollover>/}/g' > /config/abbrollover.log

Then I can use a file sensor like this:

# ABB Usage
  - platform: file
    name: "ABB Allowance"
    file_path: /config/abballowance.log
    value_template: '{{ value_json.allowance }}'
  - platform: file
    name: "ABB Download"
    file_path: /config/abbdownload.log
    value_template: '{{ value_json.download }}'
  - platform: file
    name: "ABB Upload"
    file_path: /config/abbupload.log
    value_template: '{{ value_json.upload }}'
  - platform: file
    name: "ABB Last Updated"
    file_path: /config/abbupdated.log
    value_template: '{{ value_json.updated }}'
  - platform: file
    name: "ABB Rollover"
    file_path: /config/abbrollover.log
    value_template: '{{ value_json.rollover }}'

And

homeassistant:
  # Whitelisted Directories
  whitelist_external_dirs:
    - /config
shell_command:
  abbusagemeas: /bin/bash /config/abbusage.sh

Automation to read usage

- id: 'readabbusage'
  alias: Update ABB Usage
  trigger:
  - event: start
    platform: homeassistant
  - platform: time_pattern
    minutes: '/15'
  action:
  - service: shell_command.abbusagemeas
  initial_state: 'true'

Now onto Lovelace…
So bummed the file sensor only reads the last line of the file… so I had to break it up…

I have a python that reads a json, also for a sensor… If still interested

Of course… the sucky way I’ve done it… totally open to a better way… It reads a JSON file right? Yes please

look at this post, i am using that one also to change the state of a switch in my case, based on a json value that i read
the python also includes logging in, thats not needed for you i think? so you need to drop some code :slight_smile:

For working with xml I recommend xmltodict. However you can probably use https://www.home-assistant.io/components/sensor.scrape/

The problem is I don’t see any way to read a from a file - only a URL and I don’t see any way of converting the xml I am getting from the URL and reading it as a attribute for a sensor…

I think you have 2 sensible options:

  1. Custom bash/python/whatever script
  2. Fork the scrape sensor and modify for parsing an xml file -> maybe check on github incase anyone created a custom component for this already

Not a bad idea Rob… I might see if that’s feasible…

If you are using a bash or python script to get the data (in JSON format), instead of writing it to a file, why not publish it to an MQTT topic? You can then use an MQTT Sensor to subscribe to the topic (or multiple MQTT sensors, each one parsing out whatever it needs from the received payload).

Simple Python MQTT Publish and Subscribe Example Script

For a shell script, you’ll need to use mosquitto_pub.
Using The Mosquitto_pub and Mosquitto_sub MQTT Client Tools- Examples

1 Like

Once again, 4th time this thread, it’s NOT JSON it’s xml as per the post at the top of the thread.

However… maybe I can do this via mqtt… it would certainly be more elegant…

mosquitto_pub -h localhost -t test -m "{\"value1\":20,\"value2\":40}"

So that would publish to a topic called test right? (and 2 values) Then in HA I would need to subscribe to it…

OK so I solved this… 123 is getting the credit here for pointing me in the right direction.

My script running on the host via a cronjob every 5 minutes:

#!/bin/bash

curl -c cookie.txt -b cookie.txt -sf -L -d 'login_username=***username***' -d 'login_password=***pass***' --url 'https://my.aussiebroadband.com.au/usage.php?xml=yes' -k -o abbusage.log

# This next bit builds JSON out of the XML 

grep "down1" abbusage.log | sed 's/<down1>/{"download":/g' | sed 's/<[/]down1>/,/g' > abbusageedit.log
grep "up1" abbusage.log | sed 's/<up1>/"upload":/g' | sed 's/<[/]up1>/,/g' >> abbusageedit.log
grep "allowance1_mb" abbusage.log | sed 's/<allowance1_mb>/"allowance":/g' | sed 's/<[/]allowance1_mb>/,/g' >> abbusageedit.log
grep "lastupdated" abbusage.log | sed 's/<lastupdated>/"updated":"/g' | sed 's/<[/]lastupdated>/",/g' | sed 's/[ ]/T/g' | sed 's/T"/ "/g' >> abbusageedit.log
grep "rollover" abbusage.log | sed 's/<rollover>/"rollover":/g' | sed 's/<[/]rollover>/}/g' >> abbusageedit.log

# Publish to MQTT Broker

mosquitto_pub -h 10.90.11.100 -u ***username**** -P '***password***' -t abb/abbusage -f abbusageedit.log -r

This then creates 1 sensor with attributes if I include this in config yaml:

sensor:

# ABB Usage
  - platform: mqtt
    name: "ABB Last Updated"
    state_topic: "abb/abbusage"
    device_class: timestamp
    value_template: "{{ value_json.updated }}"
    json_attributes_topic: "abb/abbusage"

Result:
image

Now onto a lovelace card…

(I’m running the script on the host and I had to install mosquitto-clients package. From memory, the last time I tried a mosquitto_pub command via ssh addon, it is not available)

What?!? XML?!?! How did I not see that!?!

Just kidding. :wink:

As you’ve already discovered, it’s not a hardship to convert XML to JSON.

FWIW, there are python packages to perform the conversion (examples) and, for shell script, there are command-line tools available (like xml2json or yq and others). The key piece of the puzzle was to get the data into Home Assistant and that’s where MQTT makes it easy.

1 Like

I’m sure but it’s simpler to just do it in the bash script along with the curl command… plus I can just run the one cronjob to update the sensor every 15 minutes… But it was a genius idea to use MQTT - I hadn’t thought of that and it was simple PLUS no issues with permissions and no kludgey multiple text files and text sensors… Would be easier if the file sensor could read multiple lines… but I already use MQTT so it’s a no brainer to leverage that.

Thanks again.

1 Like

Why not just make this into a custom component? Provide the login info in the config, make the GET request on each update, put each field into a separate sensor or make one sensor with a bunch of attributes.

Because I have no ability to do that and how many people would use it?

1 Like

I was (originally) thinking along the lines of uses pipes to connect curl to yq to mosquitto_pub.

Something like:
curl <options> | yq <options> | mosquitto_pub <options>

Anyway, it’s moot now because you already have a working script. :+1:

It’s really not that complicated as it’s mostly all boilerplate. You really only need to implement the update method, which in this case is just a GET request and parsing the XML. Honestly, it’s probably less work than what it took to do it this way. Unless, of course, you don’t know Python and don’t want to learn it. Which is understandable. But it does make things like this so much easier.

Anyway, sounds like you got it working so :man_shrugging:

I don’t know Python… happy to learn… but /bin/bash…