Hi,
I’ve now moved it to a Node-Red flow (a HASS.IO Addon) which I have attached below. Just copy this JSON to the IMPORT CLIPBOARD function within NODE-RED.
If you would prefer to use a bash script, I’ve included that below the JSON, and below that, I’ve included the sensor config from my HA YAML.
Basically I’m using the script/Node-Red flow to grab the tocken and put it into a secrets file - which then is referenced by the Sensor config YAML.
Hope that helps - give me a yell if you need a hand
JP
JSON for Node Red
[{"id":"3a50faf1.e07736","type":"tab","label":"AGL Data","disabled":false,"info":""},{"id":"30ab0e41.2627b2","type":"inject","z":"3a50faf1.e07736","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 01 * * *","once":false,"onceDelay":0.1,"x":146.5,"y":141,"wires":[["132e9ec8.6f54a1","f819c787.182738"]]},{"id":"7a8fe06e.c41a3","type":"www-request","z":"3a50faf1.e07736","name":"Get token","method":"POST","ret":"obj","url":"https://command.aglsolar.com.au/api/v2/Account/LoginUser","follow-redirects":true,"tls":"","x":547.5,"y":141,"wires":[["98ed0682.2bde3"]]},{"id":"132e9ec8.6f54a1","type":"function","z":"3a50faf1.e07736","name":"Inject Headers","func":"msg.headers = {\n \"Content-type\" : \"application/json\",\n \"Accept\": \"application/json, text/plain, */*\"\n };\nmsg.payload = {\n \"email\":\"[email protected]\",\n \"password\":\"Barraagld!\"\n};\nreturn msg;","outputs":1,"noerr":0,"x":345.5,"y":141,"wires":[["7a8fe06e.c41a3"]]},{"id":"dac950d4.88632","type":"file","z":"3a50faf1.e07736","name":"Create secrets.yaml","filename":"/config/sensors/secrets.yaml","appendNewline":false,"createDir":false,"overwriteFile":"true","x":668.5,"y":236,"wires":[[]]},{"id":"f02d371e.52471","type":"function","z":"3a50faf1.e07736","name":"Create Text File","func":"files = \"# Created \" + msg.payload.date + \"\\n\";\nfiles = files + \"agl_token: Bearer \" + msg.payload.access_token + \"\\n\";\nfiles = files + \"agl-graph-reference: https://command.aglsolar.com.au/api/v2/graph/3de94b39-2a3a-4a48-a302-44be6f5e9c44?endDate=\" + msg.payload.date + \"&granularity=Minute&metrics=read&units=W&startDate=\" + msg.payload.date + \"\\n\";\nmsg.payload = files;\nreturn msg;","outputs":1,"noerr":0,"x":419.5,"y":236,"wires":[["dac950d4.88632","3e2629b3.2140ee"]]},{"id":"98ed0682.2bde3","type":"function","z":"3a50faf1.e07736","name":"Date string","func":"today = new Date();\ndd = today.getDate();\nmm = today.getMonth()+1; //January is 0!\nyyyy = today.getFullYear();\n\nif(dd<10) {\n dd = '0'+dd\n} \n\nif(mm<10) {\n mm = '0'+mm\n} \n\ndateString = yyyy + '-' + mm + '-' + dd;\nmsg.payload.date = dateString.toString()\nreturn msg;","outputs":1,"noerr":0,"x":214.5,"y":236,"wires":[["f02d371e.52471"]]},{"id":"f819c787.182738","type":"file","z":"3a50faf1.e07736","name":"Delete secrets.yaml","filename":"/config/sensors/secrets.yaml","appendNewline":false,"createDir":false,"overwriteFile":"delete","x":344,"y":79,"wires":[[]]},{"id":"3e2629b3.2140ee","type":"api-call-service","z":"3a50faf1.e07736","name":"Restart HASS","server":"c299f3c.0d3871","service_domain":"hassio","service":"host_reboot","data":"","mergecontext":"","x":652.5,"y":290,"wires":[[]]},{"id":"c299f3c.0d3871","type":"server","z":"","name":"Home Assistant","url":"http://hassio/homeassistant","pass":"d7cf7e7eb88b4731bee6d35a6c296929"}]
Bash Script
This relies upon you loading the JQ JSON processor
#! /bin/bash
cd /home/james/.homeassistant
wget --header='Content-Type: application/json' \
--header='Accept: application/json, text/plain, */*' \
--post-data='{"email":"YOUR EMAIL ADDRESS","password":"YOUR PASSWORD"}' \
--no-check-certificate \
https://command.aglsolar.com.au/api/v2/Account/LoginUser
token=$(jq .access_token LoginUser)
token2=${token#'"'}
token3=${token2%'"'}
echo agl_token: Bearer $token3
reference1='https://command.aglsolar.com.au/api/v2/graph/3de94b39-2a3a-4a48-a302-44be6f5e9c44?endDate='
reference2='&granularity=Minute&metrics=read&units=W&startDate='
datestring="$(date +'%Y-%m-%d')"
cp secrets_template.yaml sensors/secrets.yaml
echo "# Token last updated: $(date +'%Y-%m-%d')" >> sensors/secrets.yaml
echo agl_token: Bearer $token3 >> sensors/secrets.yaml
echo agl-graph-reference: $reference1$datestring$reference2$datestring >> sensors/secrets.yaml
systemctl restart hass
rm LoginUser
The various sensors I setup based on the AGL API Data
sensor:
#AGL Energy Consumed (W)
- platform: rest
resource: https://command.aglsolar.com.au/api/v2/live_read/3de94b39-2a3a-4a48-a302-44be6f5e9c44?isUpdate=true&lastSuccessfulReadTimestampInUtc={{ utcnow().strftime("%Y%m%d%H%M") }}
method: GET
unit_of_measurement: 'W'
value_template: '{{ value_json[0].energy_consumed|round }}'
name: Power Use
headers:
Referer: https://command.aglsolar.com.au/
Accept-Encoding: gzip, deflate, sdch, br
Accept: application/json, text/plain, */*
Cookie: _gat_UA-55822146-2=1; _ga=GA1.4.1412009925.1482291643
Authorization: !secret agl_token
#AGL Energy Produced Statistics (W)
- platform: statistics
entity_id: sensor.power_use
name: Power Use Stats
max_age:
minutes: 10
#AGL Energy Produced Mean (W)
- platform: template
sensors:
power_use_stats_median:
entity_id: sensor.power_use_stats_mean
# name: Power Use Median
unit_of_measurement: "W"
value_template: "{{ states.sensor.power_use_stats_mean.attributes.median | round }}"
#AGL Energy Produced (W)
- platform: rest
resource: https://command.aglsolar.com.au/api/v2/live_read/3de94b39-2a3a-4a48-a302-44be6f5e9c44?isUpdate=true&lastSuccessfulReadTimestampInUtc={{ utcnow().strftime("%Y%m%d%H%M") }}
method: GET
unit_of_measurement: 'W'
value_template: '{{ value_json[0].energy_generated|round }}'
name: Solar
headers:
Referer: https://command.aglsolar.com.au/
Accept-Encoding: gzip, deflate, sdch, br
Accept: application/json, text/plain, */*
Cookie: _gat_UA-55822146-2=1; _ga=GA1.4.1412009925.1482291643
Authorization: !secret agl_token
# #AGL Power Produced (kWh)
- platform: rest
resource: !secret agl-graph-reference
method: GET
unit_of_measurement: 'kWh'
value_template: '{{ (float(value_json.reads.energyGenerated)/1000) | round(1) }}'
name: Solar Today
headers:
Referer: https://command.aglsolar.com.au/
Accept-Encoding: gzip, deflate, sdch, br
Accept: application/json, text/plain, */*
Cookie: _gat_UA-55822146-2=1; _ga=GA1.4.1412009925.1482291643
Authorization: !secret agl_token
#AGL Power Consumed (kWh)
- platform: rest
resource: !secret agl-graph-reference
method: GET
unit_of_measurement: 'kWh'
value_template: '{{ (float(value_json.reads.energyConsumed)/1000) | round(1) }}'
name: Power Use Today
headers:
Referer: https://command.aglsolar.com.au/
Accept-Encoding: gzip, deflate, sdch, br
Accept: application/json, text/plain, */*
Cookie: _gat_UA-55822146-2=1; _ga=GA1.4.1412009925.1482291643
Authorization: !secret agl_token
# #AGL Power Consumed (kWh)
- platform: rest
resource: !secret agl-graph-reference
method: GET
unit_of_measurement: 'kWh'
value_template: '{{ (float(value_json.reads.energyExported)/1000) | round(1) }}'
name: Power Exported Today
headers:
Referer: https://command.aglsolar.com.au/
Accept-Encoding: gzip, deflate, sdch, br
Accept: application/json, text/plain, */*
Cookie: _gat_UA-55822146-2=1; _ga=GA1.4.1412009925.1482291643
Authorization: !secret agl_token
# AGL Power Imported (kWh)
- platform: rest
resource: !secret agl-graph-reference
method: GET
unit_of_measurement: 'kWh'
value_template: '{{ (float(value_json.reads.energyImported)/1000) | round(1) }}'
name: Power Imported Today
headers:
Referer: https://command.aglsolar.com.au/
Accept-Encoding: gzip, deflate, sdch, br
Accept: application/json, text/plain, */*
Cookie: _gat_UA-55822146-2=1; _ga=GA1.4.1412009925.1482291643
Authorization: !secret agl_token