30_pilight_temp.pm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ##############################################
  2. # $Id: 30_pilight_temp.pm 10506 2016-01-14 20:40:45Z risiko79 $
  3. #
  4. # Usage
  5. #
  6. # define <name> pilight_temp <protocol> <id>
  7. #
  8. # Changelog
  9. #
  10. # V 0.10 2015-03-29 - initial beta version
  11. # V 0.11 2015-03-29 - FIX: $readingFnAttributes
  12. # V 0.12 2015-05-16 - NEW: reading battery
  13. # V 0.12 2015-05-16 - NEW: attribut corrTemp, a factor to modify temperatur
  14. # V 0.13 2015-05-17 - NEW: attribut corrHumidity, a factor to modify humidity
  15. # V 0.14 2015-05-30 - FIX: StateFn
  16. # V 0.15 2015-08-30 - NEW: support pressure, windavg, winddir, windgust
  17. # V 0.16 2015-09-06 - FIX: pressure, windavg, winddir, windgust from weather stations without temperature
  18. # V 0.17 2015-11-29 - NEW: offsetTemp and offsetHumidity to correct temperature and humidity
  19. # V 0.18 2015-12-17 - NEW: Attribut IODev to switch IO-Device
  20. ##############################################
  21. package main;
  22. use strict;
  23. use warnings;
  24. use Time::HiRes qw(gettimeofday);
  25. use JSON;
  26. use Switch; #libswitch-perl
  27. sub pilight_temp_Parse($$);
  28. sub pilight_temp_Define($$);
  29. sub pilight_temp_Initialize($)
  30. {
  31. my ($hash) = @_;
  32. $hash->{DefFn} = "pilight_temp_Define";
  33. $hash->{Match} = "^PITEMP";
  34. $hash->{ParseFn} = "pilight_temp_Parse";
  35. $hash->{StateFn} = "pilight_temp_State";
  36. $hash->{AttrList} = "corrTemp corrHumidity offsetTemp offsetHumidity IODev ".$readingFnAttributes;
  37. }
  38. #####################################
  39. sub pilight_temp_Define($$)
  40. {
  41. my ($hash, $def) = @_;
  42. my @a = split("[ \t][ \t]*", $def);
  43. if(@a < 4) {
  44. my $msg = "wrong syntax: define <name> pilight_temp <protocol> <id>";
  45. Log3 undef, 2, $msg;
  46. return $msg;
  47. }
  48. my $me = $a[0];
  49. my $protocol = $a[2];
  50. my $id = $a[3];
  51. $hash->{STATE} = "defined";
  52. $hash->{PROTOCOL} = lc($protocol);
  53. $hash->{ID} = $id;
  54. #$attr{$me}{verbose} = 5;
  55. $modules{pilight_temp}{defptr}{lc($protocol)}{$me} = $hash;
  56. AssignIoPort($hash);
  57. return undef;
  58. }
  59. #####################################
  60. sub pilight_temp_State($$$$)
  61. {
  62. my ($hash, $time, $name, $val) = @_;
  63. my $me = $hash->{NAME};
  64. #$hash->{STATE} wird nur ersetzt, wenn $hash->{STATE} == ??? fhem.pl Z: 2469
  65. #machen wir es also selbst
  66. $hash->{STATE} = $val if ($name eq "state");
  67. return undef;
  68. }
  69. ###########################################
  70. sub pilight_temp_Parse($$)
  71. {
  72. my ($mhash, $rmsg, $rawdata) = @_;
  73. my $backend = $mhash->{NAME};
  74. Log3 $backend, 4, "pilight_temp_Parse ($backend): RCV -> $rmsg";
  75. my ($dev,$protocol,$id,@args) = split(",",$rmsg);
  76. return () if($dev ne "PITEMP");
  77. my $chash;
  78. foreach my $n (keys %{ $modules{pilight_temp}{defptr}{lc($protocol)} }) {
  79. my $lh = $modules{pilight_temp}{defptr}{$protocol}{$n};
  80. next if ( !defined($lh->{ID}) );
  81. if ($lh->{ID} eq $id) {
  82. $chash = $lh;
  83. last;
  84. }
  85. }
  86. return () if (!defined($chash->{NAME}));
  87. my $corrTemp = AttrVal($chash->{NAME}, "corrTemp",1);
  88. my $corrHumidity = AttrVal($chash->{NAME}, "corrHumidity",1);
  89. my $tempOffset = AttrVal($chash->{NAME}, "offsetTemp",0);
  90. my $humidityOffset = AttrVal($chash->{NAME}, "offsetHumidity",0);
  91. readingsBeginUpdate($chash);
  92. foreach my $arg (@args){
  93. #temperature, humidity, battery
  94. #pressure, windavg, winddir, windgust
  95. my($feature,$value) = split(":",$arg);
  96. switch($feature) {
  97. case m/temperature/ {
  98. $value = $value * $corrTemp + $tempOffset;
  99. readingsBulkUpdate($chash,"state",$value);
  100. }
  101. case m/humidity/ { $value = $value * $corrHumidity + $humidityOffset;}
  102. }
  103. readingsBulkUpdate($chash,$feature,$value);
  104. }
  105. readingsEndUpdate($chash, 1);
  106. return $chash->{NAME};
  107. }
  108. 1;
  109. =pod
  110. =begin html
  111. <a name="pilight_temp"></a>
  112. <h3>pilight_temp</h3>
  113. <ul>
  114. pilight_temp represents a temperature and humidity sensor receiving data from pilight<br>
  115. You have to define the base device pilight_ctrl first.<br>
  116. Further information to pilight: <a href="http://www.pilight.org/">http://www.pilight.org/</a><br>
  117. Supported Sensors: <a href="http://wiki.pilight.org/doku.php/protocols#switches">http://wiki.pilight.org/doku.php/protocols#weather_stations</a><br>
  118. <br>
  119. <a name="pilight_temp_define"></a>
  120. <b>Define</b>
  121. <ul>
  122. <code>define &lt;name&gt; pilight_temp protocol id</code>
  123. <br><br>
  124. Example:
  125. <ul>
  126. <code>define myctrl pilight_temp alecto_wsd17 100</code><br>
  127. </ul>
  128. </ul>
  129. <br>
  130. <a name="pilight_temp_readings"></a>
  131. <p><b>Readings</b></p>
  132. <ul>
  133. <li>
  134. state<br>
  135. present the current temperature
  136. </li>
  137. <li>
  138. temperature<br>
  139. present the current temperature
  140. </li>
  141. <li>
  142. humidity<br>
  143. present the current humidity (if sensor support it)
  144. </li>
  145. <li>
  146. battery<br>
  147. present the battery state of the senor (if sensor support it)
  148. </li>
  149. <li>
  150. pressure<br>
  151. present the pressure state of the senor (if sensor support it)
  152. </li>
  153. <li>
  154. windavg<br>
  155. present the average wind speed state of the senor (if sensor support it)
  156. </li>
  157. <li>
  158. winddir<br>
  159. present the wind direction state of the senor (if sensor support it)
  160. </li>
  161. <li>
  162. windgust<br>
  163. present the wind gust state of the senor (if sensor support it)
  164. </li>
  165. </ul>
  166. <br>
  167. <a name="pilight_temp_attr"></a>
  168. <b>Attributes</b>
  169. <ul>
  170. <li><a name="corrTemp">corrTemp</a><br>
  171. A factor (e.q. 0.1) to correct the temperture value. Default: 1
  172. temperature = corrTemp * piligt_temp + offsetTemp
  173. </li>
  174. <li><a name="offsetTemp">offsetTemp</a><br>
  175. An offset for temperature value. Default: 0
  176. temperature = corrTemp * piligt_temp + offsetTemp
  177. </li>
  178. <li><a name="corrHumidity">corrHumidity</a><br>
  179. A factor (e.q. 0.1) to correct the humidity value. Default: 1
  180. humidity = corrHumidity * piligt_humidity + offsetHumidity
  181. </li>
  182. <li><a name="offsetHumidity">offsetHumidity</a><br>
  183. An offset for humidity value. Default: 0
  184. humidity = corrHumidity * piligt_humidity + offsetHumidity
  185. </li>
  186. </ul>
  187. </ul>
  188. =end html
  189. =cut