Custom page not working correctly

Moderator: leecollings

Post Reply
edwin1234
Posts: 287
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Custom page not working correctly

Post by edwin1234 »

I have a custom html page and is created by chatgtp its not working
Thats maybe not strange ;) but i cant figure out what is wrong.
Is there someone who knows html and see the where the errors are?

Code: Select all

 <!DOCTYPE html>
<html lang="nl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multi-Device Energy Display</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            background-color: #000;
            color: white;
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        .container {
            width: 90%;
            max-width: 800px;
        }
        .total-power {
            font-size: 50px;
            font-weight: bold;
            text-shadow: 0 0 10px #0ff;
            margin-bottom: 20px;
        }
        .device-list {
            width: 100%;
            text-align: left;
            margin-bottom: 20px;
        }
        .device {
            font-size: 24px;
            margin: 5px 0;
            padding: 10px;
            background: rgba(255,255,255,0.1);
            border-radius: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .bar {
            width: 50%;
            height: 10px;
            background: #222;
            border-radius: 5px;
            overflow: hidden;
        }
        .bar-fill {
            height: 100%;
            transition: width 0.5s ease-in-out, background 0.5s ease-in-out;
        }
        canvas {
            margin-top: 20px;
            width: 100%;
        }
    </style>
</head>
<body>

    <div class="container">
        <div class="total-power" id="totalPower">Totaal: 0 W</div>
        <div class="device-list" id="deviceList"></div>
        <canvas id="energyChart"></canvas>
    </div>

    <script>
        const devices = [
            { name: "Wasmachine", idx: 101, color: "#ff0" },
            { name: "Koelkast", idx: 102, color: "#0ff" },
            { name: "TV", idx: 103, color: "#f00" },
            { name: "Computer", idx: 104, color: "#f0f" }
        ];

        let historicalData = {};
        let labels = [];

        function fetchEnergyData() {
            let totalPower = 0;
            let deviceListHtml = "";

            const fetchPromises = devices.map(device =>
                fetch(`http://192.168.2.130:9091/json.htm?type=devices&rid=${device.idx}`)
                    .then(response => response.json())
                    .then(data => {
                        if (data.result && data.result.length > 0) {
                            let power = parseFloat(data.result[0].Usage.replace(" W", ""));
                            totalPower += power;

                            // Historische data opslaan
                            if (!historicalData[device.name]) {
                                historicalData[device.name] = [];
                            }
                            if (historicalData[device.name].length >= 24) {
                                historicalData[device.name].shift();
                            }
                            historicalData[device.name].push(power);

                            // HTML-lijst bijwerken
                            deviceListHtml += `
                                <div class="device">
                                    <span>${device.name}: ${power} W</span>
                                    <div class="bar"><div class="bar-fill" style="width: ${Math.min((power / 3000) * 100, 100)}%; background: ${device.color};"></div></div>
                                </div>
                            `;
                        }
                    })
            );

            Promise.all(fetchPromises).then(() => {
                document.getElementById("totalPower").innerText = `Totaal: ${totalPower} W`;
                document.getElementById("deviceList").innerHTML = deviceListHtml;

                // Historische labels bijwerken
                if (labels.length >= 24) {
                    labels.shift();
                }
                labels.push(new Date().toLocaleTimeString());

                updateChart();
            }).catch(error => console.error("Fout bij ophalen van gegevens:", error));
        }

        // Chart.js setup
        const ctx = document.getElementById("energyChart").getContext("2d");
        const energyChart = new Chart(ctx, {
            type: "line",
            data: {
                labels: labels,
                datasets: devices.map(device => ({
                    label: device.name,
                    data: [],
                    borderColor: device.color,
                    backgroundColor: `${device.color}33`,
                    borderWidth: 2,
                    pointRadius: 3,
                    tension: 0.3
                }))
            },
            options: {
                scales: {
                    y: { beginAtZero: true, max: 3000 }
                },
                plugins: {
                    legend: { display: true }
                }
            }
        });

        function updateChart() {
            energyChart.data.labels = labels;
            energyChart.data.datasets.forEach(dataset => {
                dataset.data = historicalData[dataset.label] || [];
            });
            energyChart.update();
        }

        // Update elke 5 seconden
        fetchEnergyData();
        setInterval(fetchEnergyData, 5000);
    </script>

</body>
</html>
The page only shows 0 kwh

Thanks

Regards
User avatar
waltervl
Posts: 5844
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Custom page not working correctly

Post by waltervl »

This is an old deprecated API call

Code: Select all

http://192.168.2.130:9091/json.htm?type=devices&rid=${device.idx}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
edwin1234
Posts: 287
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: Custom page not working correctly

Post by edwin1234 »

Ok thanks,
What should it be?
And is that the only mistake?

Regards
User avatar
waltervl
Posts: 5844
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Custom page not working correctly

Post by waltervl »

I hoped my hint would help you already....
See the API wiki what it should be (if I tell you you cannot find the the next error). https://wiki.domoticz.com/Domoticz_API/ ... and_newer)

Next to that check the original custom page example on your system in domotic\www\templates

I don't think the history function will work.

Perhaps you should use Dashticz as an alternative dashboard.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
edwin1234
Posts: 287
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: Custom page not working correctly

Post by edwin1234 »

Ok thanks for your answer ,
Im going to look for that.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest