Create sensor from curl

I have a file/data I can fetch via curl. It returns something like this:

 <lastupdated>2019-03-22 13:57:03</lastupdated>

It’s longer than 255 characters so I can’t read it in.

How would I go about making this a sensor with attributes?

the command is this:

"curl -c cookie.txt -b cookie.txt -sf -L -d 'login_username=***redact***' -d 'login_password=***redact***' --url ''"
1 Like

Hi @DavidFW1960

I was able to do something similar with the command_line sensor (may need to install jq)

- platform: command_line  
  command: 'curl -s -m 20 -H "Content-Type: application/json" -X GET http://192.168.1.x/solar_api/v1/GetPowerFlowRealtimeData.fcgi | jq .Body.Data.Site'
  name: power_solar
  unit_of_measurement: 'W'
    - E_Day
    - P_Grid
    - P_Load
    - rel_Autonomy
    - rel_SelfConsumption
  value_template: '{{ value_json.P_PV |int }}'
  scan_interval: 20
  command_timeout: 30

I hope that helps

I just edited to include the curl command (from secrets file)… Not sure it can be adapted as you have done…
Thanks for responding

Can you request the data in json instead of xml? That would make it more straightforward’"

nope. only xml

I found this little python script, which seems to do the job

import xmltodict
import pprint
import json

with open(testdata.xml) as fd:
    doc = xmltodict.parse(
    pp = pprint.PrettyPrinter(indent=4)

Nice… I was in the process of writing a bash script to reformat the file (I made the back curl write to a file)

Can you show me how I’d configure that python script to create a sensor with attributes?

Sorry, that’s beyond my copy and paste skills :frowning:

I was able to come up with this in bash, but I can’t get the braces at first and last character

| grep -oP '<.+?<'|cut -d\> -f1-2 --output-delimiter='": "'|cut -d\< -f1-4 --output-delimiter='"'

I can write the data to a json file no problem… but what then?

How about using the HTTP sensor?
It lets you use curl to post your json data and create the sensor

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: '{{ }}'
  - 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 }}'


  # Whitelisted Directories
    - /config
  abbusagemeas: /bin/bash /config/

Automation to read usage

- id: 'readabbusage'
  alias: Update ABB Usage
  - event: start
    platform: homeassistant
  - platform: time_pattern
    minutes: '/15'
  - 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

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…