Show current temp/value on homepage

Must be cleaned all topics go to the right subforum
Post Reply
Cribbe
Posts: 24
Joined: Saturday 24 June 2017 19:39
Target OS: Raspberry Pi
Domoticz version: 3.5866
Location: Sweden
Contact:

Show current temp/value on homepage

Post by Cribbe » Saturday 12 August 2017 22:40

I am trying to build a homepage that are made for an old smartphone that will be mounted on the wall.
I want to get current temp from my different temp sensors and perhaps wind and rain sensors as well.
Trying to do this by json but I just get day/month or year and not the just the current value.
Could somebody perhaps help me? Mayby I shall use something else but json?

Thanks!

Egregius
Posts: 2311
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Show current temp/value on homepage

Post by Egregius » Saturday 12 August 2017 22:47

Json should do just fine. Maybe my PHP Floorplan could give you some ideas? Altough, the current version is totally based on using apcu cache together with pass2php.

Main thing to do is put the devices you want in a roomplan and query that with json.
Loop thru the reply to display the stuff.

Cribbe
Posts: 24
Joined: Saturday 24 June 2017 19:39
Target OS: Raspberry Pi
Domoticz version: 3.5866
Location: Sweden
Contact:

Re: Show current temp/value on homepage

Post by Cribbe » Saturday 12 August 2017 23:15

So I must do a roomplan for this? I can´t "just" query current temp by json and print that value on a page?

Egregius
Posts: 2311
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Show current temp/value on homepage

Post by Egregius » Saturday 12 August 2017 23:32

That's the best way because you then only need one api call.
For one device: /json.htm?type=devices&rid=IDX
For all temp devices: /json.htm?type=devices&filter=temp&used=true
For a roomplan: /json.htm?type=devices&used=true&plan=IDX (IDX of the roomplan)
For all used devices: /json.htm?type=devices&used=true

Cribbe
Posts: 24
Joined: Saturday 24 June 2017 19:39
Target OS: Raspberry Pi
Domoticz version: 3.5866
Location: Sweden
Contact:

Re: Show current temp/value on homepage

Post by Cribbe » Saturday 12 August 2017 23:58

Well so much I have figured out :-)
But if I for exampel do /json.htm?type=devices&rid=IDX I get a reply with lots of values.
I see that "Temp" is one of them, but I can´t figure out the php code for this to show on my webpage.
For example on the webpage:
Pool - (current temp, json script)
Inside - (current temp, json script)
Outside - (current temp, json script)

Egregius
Posts: 2311
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Show current temp/value on homepage

Post by Egregius » Sunday 13 August 2017 9:46

Give me few minutes to write some code...

Egregius
Posts: 2311
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Show current temp/value on homepage

Post by Egregius » Sunday 13 August 2017 9:57

Code: Select all

//Lets grab all temp devices
$domoticz=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=devices&filter=temp&used=true'),true);
//Check if we got a result
if($domoticz){
	//loop thru the result
	foreach($domoticz['result'] as $dom){
		//set a variable with the name
		$name=$dom['Name'];
		//if we have a temp device set a variable with the name of the device with it's value
		if($dom['Type']=='Temp')${$name}=str_replace(' C','',$dom['Data']);
	}
}
//echo some data on the page. Ex alex_temp is the name of a thermometer
echo '<table>
<tr><td>Alex</td><td>'.$alex_temp.' °C</td></tr>
<tr><td>Tobi</td><td>'.$tobi_temp.' °C</td></tr>
<tr><td>Living</td><td>'.$living_temp.' °C</td></tr>
<tr><td>Kamer</td><td>'.$kamer_temp.' °C</td></tr>
<tr><td>Badkamer</td><td>'.$badkamer_temp.' °C</td></tr>
</table>';
This will print a small table with the values:

Code: Select all

Alex	21.1 °C
Tobi	21.1 °C
Living	21.1 °C
Kamer	21.2 °C
Badkamer	21.3 °C

Cribbe
Posts: 24
Joined: Saturday 24 June 2017 19:39
Target OS: Raspberry Pi
Domoticz version: 3.5866
Location: Sweden
Contact:

Re: Show current temp/value on homepage

Post by Cribbe » Sunday 13 August 2017 21:38

Thanks!
Got it to work, but got stuck on one sensor that is both temp & humidity.
I´ve tried this:

Code: Select all

<?php
//Lets grab all temp devices
$domoticz=json_decode(file_get_contents('http://192.168.1.77:8080/json.htm?type=devices&filter=temp&used=true'),true);
//Check if we got a result
if($domoticz){
	//loop thru the result
	foreach($domoticz['result'] as $dom){
		//set a variable with the name
		$name=$dom['Name'];
		//if we have a temp device set a variable with the name of the device with it's value
		if($dom['Type']=='Temp + Humidity')${$name}=str_replace(' C','',$dom['Data']);
	}
}

//echo some data on the page. Ex alex_temp is the name of a thermometer
echo '<table>
<tr><td>Ute</td><td>'.$Ute.' °C</td></tr>
</table>';
But that give me this as result:

Ute 14.2, 71 % °C

I can´t figure out how to give me the result like this (if it is possible):
Ute 14,2 °C, 72% humidity or something similar :D

gordonb3
Posts: 477
Joined: Friday 22 January 2016 11:15
Target OS: Linux
Domoticz version: custom
Location: The Netherlands
Contact:

Re: Show current temp/value on homepage

Post by gordonb3 » Monday 14 August 2017 10:17

This is what I use for an iFrame on my homepage. It is derived from an old wunderground 'include on your home page' code snippet which is where the weather icon images (##.png) came from that are being referred to.

Because in my home I have issues with the range of the RFXTRX433 I tend to lose contact with several weather devices from time to time. For this reason I use a nearby wunderground site for backup data. The script will use data from any device that was active within the last 30 minutes (values of the last device in the list will overwrite those that came first), but it will discard the temperature reported by a wind meter (wrong hight, influenced by direct sun, etc).

This is what it looks like:
Image

Code: Select all

<?php

// Constants
$JSON="http://192.168.10.1:8080/domoticz/json.htm";
$imgpath="/media/weather/";

$currenttime = date("H:i:s");
$lastseenOK=date("Y-m-d H:i:s",time()-1800);

$iconmapday =      Array("Cloudy" => "26.png",  "Partly Cloudy" => "30.png",       "Clear" => "32.png", "Rain" => "11.png", "Sunny" => "32.png");
$iconmapnight =    Array("Cloudy" => "26.png",  "Partly Cloudy" => "27.png",       "Clear" => "31.png", "Rain" => "11.png", "Sunny" => "31.png");
$localizeNLday =   Array("Cloudy" => "Bewolkt", "Partly Cloudy" => "Lichtbewolkt", "Clear" => "Zonnig", "Rain" => "Regen",  "Sunny" => "Zonnig", "Overcast" => "Zwaar bewolkt");
$localizeNLnight = Array("Cloudy" => "Bewolkt", "Partly Cloudy" => "Lichtbewolkt", "Clear" => "Helder", "Rain" => "Regen",  "Sunny" => "Helder", "Overcast" => "Zwaar bewolkt");


// Initialize variables
$weather=Array(
    "Name" => "NaN",
    "LastUpdate" => "NaN",
    "Temp" => "NaN",
    "Humidity" => "NaN",
    "Barometer" => "NaN",
    "Chill" => "NaN",
    "Speed" => "NaN",
    "Gust" => "NaN",
    "DirectionStr" => "NaN",
    "UVI" => "NaN",
    "Rain" => "NaN",
    "RainRate" => "NaN",
    "ForecastStr" => "Cloudy",
    "Sunrise" => "NaN",
    "Sunset" => "NaN"
);
$devices=Array();
$j=0;


// Parse data from weather devices
$json = explode("\n",shell_exec('wget -O /dev/stdout "'.$JSON.'?type=devices&filter=weather&used=true"'));
for ($i=0;$i<sizeof($json);$i++) {
  $parm = trim(strtok($json[$i],":"));
  if ($parm=="{") {
    $j++;
    $devices[$j]=Array();
  }

  switch ($parm) {
    case '"Name"':
    case '"LastUpdate"':
    case '"Temp"':
    case '"Humidity"':
    case '"Barometer"':
    case '"Chill"':
    case '"Speed"':
    case '"Gust"':
    case '"DirectionStr"':
    case '"UVI"':
    case '"Rain"':
    case '"RainRate"':
    case '"ForecastStr"':
      $key = str_replace("\"","",$parm);
      $devices[$j][$key] = preg_replace("/,$/","",preg_replace("/^\"(.*)\",$/","$1",trim(strtok("\n"))));
      break;
  }
}


// Combine data from active devices
while (list($device, $devicedata) = each($devices)) {
  if (isset($devicedata["Gust"])) {
    unset($devicedata["Temp"]);
    unset($devices[$device]["Temp"]);
  }
  if (isset($devicedata["LastUpdate"]) && ($devicedata["LastUpdate"]>$lastseenOK)) {
    while (list($key, $value) = each($devicedata)) {
      $weather[$key]=$value;
    }
  }
}


// Retrieve Sunrise and Sunset values
$json = explode("\n",shell_exec('wget -O /dev/stdout "'.$JSON.'?type=command&param=getSunRiseSet"'));
for ($i=0;$i<sizeof($json);$i++) {
  $parm = trim(strtok($json[$i],":"));
  switch ($parm) {
    case '"Sunrise"':
    case '"Sunset"':
      $key = str_replace("\"","",$parm);
      $weather[$key] = preg_replace("/,$/","",preg_replace("/^\"(.*)\",$/","$1",trim(strtok("\n"))));
      break;
  }
}


// Translate ForecastString to weather icon and localized text
$condition = $weather["ForecastStr"];
if (($currenttime > $weather["Sunrise"]) && ($currenttime < $weather["Sunset"])) {
  $weathericon = $iconmapday["$condition"];
  $forecaststr = isset($localizeNLday["$condition"])?$localizeNLday["$condition"]:$condition;
} else {
  $weathericon = $iconmapnight["$condition"];
  $forecaststr = isset($localizeNLnight["$condition"])?$localizeNLnight["$condition"]:$condition;
}


// Translate UV Index number to localized text
switch (floor($weather["UVI"])) {
  case 0:
  case 1:
  case 2:
    $uvitext = "zeer laag";
    break;
  case 3:
  case 4:
    $uvitext = "zwak";
    break;
  case 5:
  case 6:
    $uvitext = "matig";
    break;
  case 7:
  case 8:
    $uvitext = "sterk";
    break;
  default:
    $uvitext = "zeer sterk";
    break;
}


?>

<html>
<meta name="viewport" content="width=185, initial-scale=1.6, user-scalable=yes">
<head>
  <title>Het weer in Delft</title>

  <style>
    .weatherbox { padding: 15px 0px 0px 5px; width: 175px; height: 140px; background-image: url(<?=_($imgpath)?>weatherbox.jpg); background-repeat: no-repeat;}
    .weatherdetails { margin: 15px 0px 0px 8px; font-family: Arial;font-size: 11px; color:#000000;font-weight:bold;}
    .weathericon { background-image: url(<?=_($imgpath.$weathericon)?>); background-repeat: no-repeat; margin: 0px 0px 0px 15px; width: 175px; text-align: center; color:#000000; font-family:verdana,arial;}
    .maintemp { font-size:18px;font-weight:bold; }
    .condition { font-size:10px; }
  </style>
</head>

<body>
<div class='weatherbox'>
<div class='weathericon'><span class='maintemp'><?=_(round($weather['Temp'],1))?>&deg;C</span><br>
                         <span class='condition'><?=_($forecaststr)?></span></div>
<div class='weatherdetails'>
  Gevoelstemperatuur: <?=_(round($weather['Chill'],1))?>&deg;C<br>
  Luchtvochtigheid: <?=_($weather['Humidity'])?>%<br>
  Wind: <?=_(round($weather['Speed']*3.6,1))?> km/h <?=_($weather['DirectionStr'])?><br>
  Regen: <?=_($weather['RainRate'])?> mm/h (<?=_($weather['Rain'])?> mm)<br>
  UV index: <?=_($weather['UVI']." ($uvitext)")?>
</div>
</div>
</body>
</html>



Excito B3 running Gentoo Linux, P1, Rfxtrx433 to read and control TFA, KaKu, EvoHome RFG100
Custom patched Domoticz v3.8000

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests