Skip to content

Commit

Permalink
Use templates for files depending on TZ value
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasta74 committed Jul 29, 2023
1 parent 1045956 commit 6ba12d9
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 105 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ compose.env
grafana.env
pypowerwall.env
telegraf.local
VERSION
tz

# Local Dev files
*.bak
backups/*.tgz
influxdb/run-once-*.sql.done

# Created from templates
dashboards/dashboard-new.json
dashboards/dashboard-min-mean-max.json
dashboards/dashboard-no-animation.json
dashboards/dashboard-simple.json
dashboards/dashboard.json
influxdb/influxdb.sql
telegraf.conf

sandbox/

grafana/alerting/
Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"name": "VAR_TZ",
"type": "constant",
"label": "Timezone",
"value": "America/Los_Angeles",
"value": "$TZ",
"description": ""
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"name": "VAR_TZ",
"type": "constant",
"label": "Timezone",
"value": "America/Los_Angeles",
"value": "$TZ",
"description": ""
}
],
Expand Down Expand Up @@ -1388,7 +1388,7 @@
]
],
"tags": [],
"tz": "America/Los_Angeles"
"tz": "$TZ"
},
{
"alias": "off_grid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"name": "VAR_TZ",
"type": "constant",
"label": "Timezone",
"value": "America/Los_Angeles",
"value": "$TZ",
"description": ""
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"name": "VAR_TZ",
"type": "constant",
"label": "Timezone",
"value": "America/Los_Angeles",
"value": "$TZ",
"description": ""
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"name": "VAR_TZ",
"type": "constant",
"label": "Timezone",
"value": "America/Los_Angeles",
"value": "$TZ",
"description": ""
}
],
Expand Down Expand Up @@ -1439,7 +1439,7 @@
]
],
"tags": [],
"tz": "America/Los_Angeles"
"tz": "$TZ"
},
{
"alias": "sync",
Expand Down
4 changes: 2 additions & 2 deletions influxdb/influxdb.sql → influxdb/influxdb.sql.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ CREATE RETENTION POLICY grid ON powerwall duration INF replication 1
CREATE RETENTION POLICY pod ON powerwall duration INF replication 1
CREATE RETENTION POLICY alerts ON powerwall duration INF replication 1
CREATE CONTINUOUS QUERY cq_autogen ON powerwall BEGIN SELECT mean(home) AS home, mean(solar) AS solar, mean(from_pw) AS from_pw, mean(to_pw) AS to_pw, mean(from_grid) AS from_grid, mean(to_grid) AS to_grid, last(percentage) AS percentage INTO powerwall.autogen.:MEASUREMENT FROM (SELECT load_instant_power AS home, solar_instant_power AS solar, abs((1+battery_instant_power/abs(battery_instant_power))*battery_instant_power/2) AS from_pw, abs((1-battery_instant_power/abs(battery_instant_power))*battery_instant_power/2) AS to_pw, abs((1+site_instant_power/abs(site_instant_power))*site_instant_power/2) AS from_grid, abs((1-site_instant_power/abs(site_instant_power))*site_instant_power/2) AS to_grid, percentage FROM raw.http) GROUP BY time(1m), month, year fill(linear) END
CREATE CONTINUOUS QUERY cq_kwh ON powerwall RESAMPLE EVERY 1m BEGIN SELECT integral(home)/1000/3600 AS home, integral(solar)/1000/3600 AS solar, integral(from_pw)/1000/3600 AS from_pw, integral(to_pw)/1000/3600 AS to_pw, integral(from_grid)/1000/3600 AS from_grid, integral(to_grid)/1000/3600 AS to_grid INTO powerwall.kwh.:MEASUREMENT FROM autogen.http GROUP BY time(1h), month, year tz('America/Los_Angeles') END
CREATE CONTINUOUS QUERY cq_daily ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.daily.:MEASUREMENT FROM powerwall.kwh.http GROUP BY time(1d), month, year tz('America/Los_Angeles') END
CREATE CONTINUOUS QUERY cq_kwh ON powerwall RESAMPLE EVERY 1m BEGIN SELECT integral(home)/1000/3600 AS home, integral(solar)/1000/3600 AS solar, integral(from_pw)/1000/3600 AS from_pw, integral(to_pw)/1000/3600 AS to_pw, integral(from_grid)/1000/3600 AS from_grid, integral(to_grid)/1000/3600 AS to_grid INTO powerwall.kwh.:MEASUREMENT FROM autogen.http GROUP BY time(1h), month, year tz('$TZ') END
CREATE CONTINUOUS QUERY cq_daily ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.daily.:MEASUREMENT FROM powerwall.kwh.http GROUP BY time(1d), month, year tz('$TZ') END
CREATE CONTINUOUS QUERY cq_monthly ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.monthly.:MEASUREMENT FROM powerwall.daily.http GROUP BY time(365d), month, year END
CREATE CONTINUOUS QUERY cq_pw_temps ON powerwall BEGIN SELECT mean(PW1_temp) AS PW1_temp, mean(PW2_temp) AS PW2_temp, mean(PW3_temp) AS PW3_temp, mean(PW4_temp) AS PW4_temp, mean(PW5_temp) AS PW5_temp, mean(PW6_temp) AS PW6_temp INTO powerwall.pwtemps.:MEASUREMENT FROM (SELECT PW1_temp, PW2_temp, PW3_temp, PW4_temp, PW5_temp, PW6_temp FROM raw.http) GROUP BY time(1m), month, year fill(linear) END
CREATE CONTINUOUS QUERY cq_pw_tempsb ON powerwall BEGIN SELECT mean(PW7_temp) AS PW7_temp, mean(PW8_temp) AS PW8_temp, mean(PW9_temp) AS PW9_temp, mean(PW10_temp) AS PW10_temp, mean(PW11_temp) AS PW11_temp, mean(PW12_temp) AS PW12_temp INTO powerwall.pwtemps.:MEASUREMENT FROM (SELECT PW7_temp, PW8_temp, PW9_temp, PW10_temp, PW11_temp, PW12_temp FROM raw.http) GROUP BY time(1m), month, year fill(linear) END
Expand Down
38 changes: 26 additions & 12 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
# Stop on Errors
set -e

if [ ! -f VERSION ]; then
echo "ERROR: Missing VERSION file. Setup must run from installation directory."
echo ""
exit 1
fi
VERSION=`cat VERSION`
# Change to setup directory
cd $(dirname "$0")
VERSION=`git describe --tag --long --dirty=-custom`

echo "Powerwall Dashboard (v${VERSION}) - SETUP"
echo "Powerwall Dashboard (${VERSION}) - SETUP"
echo "-----------------------------------------"

# Verify not running as root
Expand Down Expand Up @@ -75,7 +72,21 @@ PW_ENV_FILE="pypowerwall.env"
COMPOSE_ENV_FILE="compose.env"
TELEGRAF_LOCAL="telegraf.local"
GF_ENV_FILE="grafana.env"
CURRENT=`cat tz`

# If tz file exists use its value
if [ -f tz ]; then
CURRENT=`cat tz`
else
# Try to infer TZ from system
if [ -L /etc/localtime ]; then
CURRENT=$(realpath --relative-base=/usr/share/zoneinfo /etc/localtime)
fi
# TODO: add other infer methods for systems not using /etc/localtime link
fi
if [ -z "${CURRENT}" ]; then
# Use a default
CURRENT="America/Los_Angeles"
fi

echo "Timezone (leave blank for ${CURRENT})"
read -p 'Enter Timezone: ' TZ
Expand Down Expand Up @@ -104,8 +115,8 @@ if [ ! -f ${PW_ENV_FILE} ]; then
echo "PW_EMAIL=${EMAIL}" > ${PW_ENV_FILE}
echo "PW_PASSWORD=${PASSWORD}" >> ${PW_ENV_FILE}
echo "PW_HOST=${IP}" >> ${PW_ENV_FILE}
echo "PW_TIMEZONE=America/Los_Angeles" >> ${PW_ENV_FILE}
echo "TZ=America/Los_Angeles" >> ${PW_ENV_FILE}
echo "PW_TIMEZONE=${TZ:-$CURRENT}" >> ${PW_ENV_FILE}
echo "TZ=${TZ:-$CURRENT}" >> ${PW_ENV_FILE}
echo "PW_DEBUG=no" >> ${PW_ENV_FILE}
fi

Expand All @@ -127,10 +138,10 @@ fi
echo ""
if [ -z "${TZ}" ]; then
echo "Using ${CURRENT} timezone...";
./tz.sh "${CURRENT}";
./tz.sh -y "${CURRENT}";
else
echo "Setting ${TZ} timezone...";
./tz.sh "${TZ}";
./tz.sh -y "${TZ}";
fi
echo "-----------------------------------------"
echo ""
Expand Down Expand Up @@ -172,6 +183,9 @@ if [ -f weather/weather411.conf ]; then
docker restart weather411
fi

# Record installed version
echo "$VERSION" > VERSION

# Display Final Instructions
cat << EOF
------------------[ Final Setup Instructions ]-----------------
Expand Down
4 changes: 2 additions & 2 deletions telegraf.conf → telegraf.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@
[[processors.date]]
tag_key = "month"
date_format = "Jan"
timezone = "America/Los_Angeles"
timezone = "$TZ"

[[processors.date]]
tag_key = "year"
date_format = "2006"
timezone = "America/Los_Angeles"
timezone = "$TZ"

# Alert Data
[[inputs.http]]
Expand Down
1 change: 0 additions & 1 deletion tz

This file was deleted.

63 changes: 33 additions & 30 deletions tz.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
#!/bin/bash
# Shell script to replace timezone values in powerwall.yml, telegraf.conf, influxdb.sql and dashboard.json
if [ $# -eq 0 ]
then
# Shell script to create files depending on TZ value from templates

# This is an hidden option to not print warning message
# used by setup.sh script and by upgrade.sh script when
# updating from version without templates
if [ "$1" == "-y" ]; then
PRINT_WARNING=false
shift 1
fi

if [ $# -eq 0 ]; then
echo "ERROR: No timezone supplied"
echo
echo "USAGE: ${0} {timezone}"
exit
fi

# Current and New TZ values
DEFAULT="America/Los_Angeles"
CURRENT=`cat tz`
NEW=$1
export TZ=$1

TEMPLATES=(
$(echo dashboards/*.json.template)
influxdb/influxdb.sql.template
telegraf.conf.template
)

# Replace TZ Function
updatetz() {
local from=${1}
local to=${2}
sed -i.bak "s@${from}@${to}@g" powerwall.yml
sed -i.bak "s@${from}@${to}@g" telegraf.conf
sed -i.bak "s@${from}@${to}@g" influxdb/influxdb.sql
for i in dashboards/dashboard*.json; do
sed -i.bak "s@${from}@${to}@g" $i
done
sed -i.bak "s@${from}@${to}@g" pypowerwall.env
if [ -d "dashboards" ]
then
for i in dashboards/*.json; do
sed -i.bak "s@${from}@${to}@g" $i
done
if ${PRINT_WARNING:-true}; then
echo "WARNING: the following files will be recreated from templates"
echo " using TZ=$TZ; any local changes will be lost."
printf -- " - %s\n" "${TEMPLATES[@]%.template}"
echo
read -r -p "Continue anyway? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
exit 1
fi
}
fi

for template in "${TEMPLATES[@]}"; do
envsubst '$TZ' < "${template}" > "${template%.template}"
done

# Replace TZ values
updatetz "${CURRENT}" "${NEW}"
updatetz "${DEFAULT}" "${NEW}"

# Record new TZ value
echo "${NEW}" > tz
# Record TZ value
echo "${TZ}" > tz
Loading

0 comments on commit 6ba12d9

Please sign in to comment.