Skip to content

Instantly share code, notes, and snippets.

@adlerweb
Last active October 2, 2019 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adlerweb/588fb15b2c100908eed0806905bf16a2 to your computer and use it in GitHub Desktop.
Save adlerweb/588fb15b2c100908eed0806905bf16a2 to your computer and use it in GitHub Desktop.
<?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