Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 1, 2024
0 parents commit bedf852
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.chart {
width: 100vw;
}
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IOT Charts</title>
<script src="plotly-2.30.0.min.js" charset="utf-8"></script>
<script type="module">
import { createPlot } from './index.mjs'
createPlot()
</script>
</head>
<body>
<div id="tester" class="chart"></div>
</body>
</html>
54 changes: 54 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const SENSOR_LABELS = {
'sensor.atc_fe05_temperature': 'Garage Temp',
'sensor.atc_bb8c_temperature': 'Bunny Temp',
'sensor.blue1dht22temp': 'Shed Temp (outside)',
'sensor.sonoff_snzb_02d_f1f36efe_temperature': 'Greenhouse Temp',
}

export async function createPlot() {
const url =
'https://ball-started.pockethost.io/api/collections/sensor/records?perPage=500&sort=-created'

const response = await fetch(url)
const json = await response.json()

const data = Object.groupBy(json.items, ({ entity_id }) => entity_id)

const tempSensorIds = Object.keys(data).filter((sensorId) =>
sensorId.includes('temp'),
)

console.log('Temp sensors:', tempSensorIds)

const traces = tempSensorIds.map((sensorId) => ({
x: data[sensorId].map(({ created }) => dateStr(new Date(created))),
y: data[sensorId].map(({ data }) => data),
name: SENSOR_LABELS[sensorId] ?? sensorId,
mode: 'lines',
connectgaps: true,
}))

console.log('data:', traces)

const chartEl = document.getElementById('tester')
Plotly.newPlot(chartEl, traces, {
margin: { t: 0 },
legend: { orientation: 'h' },
xaxis: {
tickformat: '%I:%M %p',
},
// yaxis: { range: [0, 100] },
})
}

/** @param date {Date} */
function dateStr(date) {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hour = String(date.getHours()).padStart(2, '0')
const minute = String(date.getMinutes()).padStart(2, '0')
const second = String(date.getSeconds()).padStart(2, '0')

return `${year}-${month}-${day}T${hour}:${minute}:${second}`
}
8 changes: 8 additions & 0 deletions plotly-2.30.0.min.js

Large diffs are not rendered by default.

0 comments on commit bedf852

Please sign in to comment.