Templates in command line sensor + Octopus Energy API

Good news, everyone!

I’ve looked at all the posts above and decided the way I’ve made these sensors is a total mess. A sensor that requires a separate input_number AND automation AND script to run properly?! Rubbish. And a pain in the backside to set up.

So I’ve kicked myself into learning a bit more bash scripting and am pleased to present new scripts that remove the need for both the automation and the input_number.

Here’s what you need:

configuration.yaml:

sensor:
  - platform: command_line
    name: electricity
    unit_of_measurement: 'kWh'
    scan_interval: 600
    command: 'sh /config/bash_scripts/electricity.sh'

  - platform: command_line
    name: gas
    unit_of_measurement: 'm3'
    scan_interval: 600
    command: 'sh /config/bash_scripts/gas.sh'

config/bash_scripts/electricity.sh:

STARTDATE=`date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%dT00:00:00"`
ENDDATE=`date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%dT23:59:59"`
STARTDATEPREV=`date -d @$(( $(date +"%s") - 172800)) +"%Y-%m-%dT00:00:00"`
ENDDATEPREV=`date -d @$(( $(date +"%s") - 172800)) +"%Y-%m-%dT23:59:59"`
COUNT=`curl -H "Authorization: Basic [YOUR AUTH CODE AS PER POST #15]" "https://api.octopus.energy/v1/electricity-meter-points/[COPY FROM OCTOPUS API SITE]/meters/[COPY FROM OCTOPUS API SITE]/consumption/?period_from=$STARTDATE&period_to=$ENDDATE" | python -mjson.tool | grep -c "consumption"`

if [[ "$COUNT" == 48 ]]
then
curl -H "Authorization: Basic [YOUR AUTH CODE AS PER POST #15]" "https://api.octopus.energy/v1/electricity-meter-points/[COPY FROM OCTOPUS API SITE]/meters/[COPY FROM OCTOPUS API SITE]/consumption/?period_from=$STARTDATE&period_to=$ENDDATE" | jq '[.. | objects | .consumption] | add' | awk '{printf "%0.2f\n",$1}' 
else
curl -H "Authorization: Basic [YOUR AUTH CODE AS PER POST #15]" "https://api.octopus.energy/v1/electricity-meter-points/[COPY FROM OCTOPUS API SITE]/meters/[COPY FROM OCTOPUS API SITE]/consumption/?period_from=$STARTDATEPREV&period_to=$ENDDATEPREV" | jq '[.. | objects | .consumption] | add' | awk '{printf "%0.2f\n",$1}'  
fi

config/bash_scripts/gas.sh:

STARTDATE=`date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%dT00:00:00"`
ENDDATE=`date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%dT23:59:59"`
STARTDATEPREV=`date -d @$(( $(date +"%s") - 172800)) +"%Y-%m-%dT00:00:00"`
ENDDATEPREV=`date -d @$(( $(date +"%s") - 172800)) +"%Y-%m-%dT23:59:59"`
COUNT=`curl -H "Authorization: Basic [YOUR AUTH CODE AS PER POST #15]" "https://api.octopus.energy/v1/gas-meter-points/[COPY FROM OCTOPUS API SITE]/meters/[COPY FROM OCTOPUS API SITE]/consumption/?period_from=$STARTDATE&period_to=$ENDDATE" | python -mjson.tool | grep -c "consumption"`

if [[ "$COUNT" == 48 ]]
then
curl -H "Authorization: Basic [YOUR AUTH CODE AS PER POST #15]" "https://api.octopus.energy/v1/gas-meter-points/[COPY FROM OCTOPUS API SITE]/meters/[COPY FROM OCTOPUS API SITE]/consumption/?period_from=$STARTDATE&period_to=$ENDDATE" | jq '[.. | objects | .consumption] | add' | awk '{printf "%0.2f\n",$1/11.36}' 
else
curl -H "Authorization: Basic [YOUR AUTH CODE AS PER POST #15]" "https://api.octopus.energy/v1/gas-meter-points/[COPY FROM OCTOPUS API SITE]/meters/[COPY FROM OCTOPUS API SITE]/consumption/?period_from=$STARTDATEPREV&period_to=$ENDDATEPREV" | jq '[.. | objects | .consumption] | add' | awk '{printf "%0.2f\n",$1/11.36}'  
fi

This should leave you with sensor.gas and sensor.electricity which will update on their own as and when the previous day’s energy usage figures get updated. No need for automations or input_numbers!

Quite a few bits of code to replace in this - see post #15 and https://octopus.energy/dashboard/developer/ for all the snippets you need.

Hope this is useful and tidies things up a bit for people - let me know if anything doesn’t work for some reason and I’ll be happy to help clear things up. Good luck!

6 Likes