Skip to content

Instantly share code, notes, and snippets.

@adlerweb

adlerweb/vzapi.php

Last active Oct 2, 2019
Embed
What would you like to do?
<?php
/*
Usage:
- platform: command_line
name: foo
command: "php -f /home/homeassistant/vzapi.php 1"
unit_of_measurement: "W"
Todo:
- Rewrite in Python
- Use UUID instead of ID
*/
if($argc != 2) die('id?');
$sensor = (int)$argv[1];
error_reporting(E_ALL);
$mysql_serv = 'localhost';
$mysql_user = 'vz-read';
$mysql_pass = '*****';
$mysql_datb = 'volkszaehler';
$sql = new mysqli($mysql_serv, $mysql_user, $mysql_pass, $mysql_datb);
//Yes, I know this can be used for SQL injections.
$data = $sql->query("SELECT * FROM `data` WHERE `channel_id` = ".$sensor." ORDER BY `timestamp` DESC LIMIT 1 ");
if($data->num_rows != 1) die();
$data = $data->fetch_object();
if($data->timestamp/1000 <= time()-300) die();
echo $data->value;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment