14_CUL_TX.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ##############################################
  2. # $Id: 14_CUL_TX.pm 17102 2018-08-08 05:34:42Z rudolfkoenig $
  3. package main;
  4. # From peterp
  5. # Lacrosse TX3-TH thermo/hygro sensor
  6. use strict;
  7. use warnings;
  8. sub
  9. CUL_TX_Initialize($)
  10. {
  11. my ($hash) = @_;
  12. $hash->{Match} = "^TX.........."; # Need TX to avoid FHTTK
  13. $hash->{DefFn} = "CUL_TX_Define";
  14. $hash->{UndefFn} = "CUL_TX_Undef";
  15. $hash->{ParseFn} = "CUL_TX_Parse";
  16. $hash->{AttrList} = "IODev do_not_notify:1,0 ignore:0,1 showtime:1,0 " .
  17. $readingFnAttributes;
  18. $hash->{AutoCreate}=
  19. { "CUL_TX.*" => { GPLOT => "temp4hum4:Temp/Hum,",
  20. FILTER => "%NAME",
  21. autocreateThreshold => "2:180" } };
  22. }
  23. #############################
  24. sub
  25. CUL_TX_Define($$)
  26. {
  27. my ($hash, $def) = @_;
  28. my @a = split("[ \t][ \t]*", $def);
  29. return "wrong syntax: define <name> CUL_TX <code> [corr] [minsecs]"
  30. if(int(@a) < 3 || int(@a) > 5);
  31. my $dp = $modules{CUL_TX}{defptr};
  32. my $old = ($dp && $dp->{$a[2]} ? $dp->{$a[2]}{NAME} : "");
  33. my $op = ($hash->{OLDDEF} ? "modify":"define");
  34. my $oc = ($hash->{OLDDEF} ? $hash->{CODE} : "");
  35. return "Cannot $op $hash->{NAME} as the code $a[2] is already used by $old"
  36. if($old && $oc ne $a[2]);
  37. delete($modules{CUL_TX}{defptr}{$oc}) if($oc);
  38. $hash->{CODE} = $a[2];
  39. $hash->{corr} = ((int(@a) > 3) ? $a[3] : 0);
  40. $hash->{minsecs} = ((int(@a) > 4) ? $a[4] : 0);
  41. $hash->{lastT} = 0;
  42. $hash->{lastH} = 0;
  43. $modules{CUL_TX}{defptr}{$a[2]} = $hash;
  44. $hash->{STATE} = "Defined";
  45. return undef;
  46. }
  47. #####################################
  48. sub
  49. CUL_TX_Undef($$)
  50. {
  51. my ($hash, $name) = @_;
  52. delete($modules{CUL_TX}{defptr}{$hash->{CODE}})
  53. if(defined($hash->{CODE}) &&
  54. defined($modules{CUL_TX}{defptr}{$hash->{CODE}}));
  55. return undef;
  56. }
  57. ###################################
  58. sub
  59. CUL_TX_Parse($$)
  60. {
  61. my ($hash, $msg) = @_;
  62. $msg = substr($msg, 1);
  63. # Msg format: TXTHHXYZXY, see http://www.f6fbb.org/domo/sensors/tx3_th.php
  64. my @a = split("", $msg);
  65. my $id2 = hex($a[4]) & 1; #meaning unknown
  66. my $id3 = (hex($a[3])<<3) + (hex($a[4])>>1);
  67. if($a[5] ne $a[8] || $a[6] ne $a[9]) {
  68. Log3 $hash, 4, "CUL_TX $id3 ($msg) data error";
  69. return "";
  70. }
  71. my $def = $modules{CUL_TX}{defptr}{$id3};
  72. if(!$def) {
  73. Log3 $hash, 2, "CUL_TX Unknown device $id3, please define it";
  74. return "UNDEFINED CUL_TX_$id3 CUL_TX $id3";
  75. }
  76. my $now = time();
  77. my $name = $def->{NAME};
  78. return "" if(IsIgnored($name));
  79. Log3 $name, 4, "CUL_TX $name $id3 ($msg)";
  80. my ($msgtype, $val);
  81. my $valraw = (hex($a[5]).$a[6].".".$a[7]);
  82. my $type = $a[2];
  83. if($type eq "0") {
  84. if($now - $def->{lastT} < $def->{minsecs} ) {
  85. return "";
  86. }
  87. $def->{lastT} = $now;
  88. $msgtype = "temperature";
  89. $val = sprintf("%2.1f", ($valraw - 50 + $def->{corr}) );
  90. Log3 $name, 4, "CUL_TX $msgtype $name $id3 T: $val UnknownFlag: $id2";
  91. } elsif ($type eq "E") {
  92. if($now - $def->{lastH} < $def->{minsecs} ) {
  93. return "";
  94. }
  95. $def->{lastH} = $now;
  96. $msgtype = "humidity";
  97. $val = $valraw;
  98. Log3 $name, 4, "CUL_TX $msgtype $name $id3 H: $val UnknownFlag: $id2";
  99. } else {
  100. Log3 $name, 2, "CUL_TX $type $name $id3 ($msg) unknown type";
  101. return "";
  102. }
  103. # I suspect that humidity 0F.F is battery warning. Can someone verify?
  104. if($val !~ m/^[0-9.-]*$/) {
  105. Log3 $name, 5, "CUL_TX $type $name bogus value $val ($msg)";
  106. return "";
  107. }
  108. my $state="";
  109. my $t = ReadingsVal($name, "temperature", undef);
  110. my $h = ReadingsVal($name, "humidity", undef);
  111. $t = $val if($msgtype eq "temperature");
  112. $h = $val if($msgtype eq "humidity");
  113. if(defined($t) && defined($h)) {
  114. $state="T: $t H: $h";
  115. } elsif(defined($t)) {
  116. $state="T: $t";
  117. } elsif(defined($h)) {
  118. $state="H: $h";
  119. }
  120. readingsBeginUpdate($def);
  121. readingsBulkUpdate($def, "state", $state);
  122. readingsBulkUpdate($def, $msgtype, $val);
  123. readingsEndUpdate($def, 1);
  124. return $name;
  125. }
  126. 1;
  127. =pod
  128. =item summary Some Lacrosse sensors (TX-3TH, etc)
  129. =item summary_DE Einige Lacrosse Sensoren (TX-3TH, usw.)
  130. =begin html
  131. <a name="CUL_TX"></a>
  132. <h3>CUL_TX</h3>
  133. <ul>
  134. The CUL_TX module interprets TX2/TX3 type of messages received by the CUL,
  135. see also http://www.f6fbb.org/domo/sensors/tx3_th.php.
  136. This protocol is used by the La Crosse TX3-TH thermo/hygro sensor and other
  137. wireless themperature sensors. Please report the manufacturer/model of other
  138. working devices. <br><br>
  139. <a name="CUL_TXdefine"></a>
  140. <b>Define</b>
  141. <ul>
  142. <code>define &lt;name&gt; CUL_TX &lt;code&gt; [corr] [minsecs]</code> <br>
  143. <br>
  144. &lt;code&gt; is the code of the autogenerated address of the TX device (0
  145. to 127)<br>
  146. corr is a correction factor, which will be added to the value received from
  147. the device.<br>
  148. minsecs are the minimum seconds between two log entries or notifications
  149. from this device. <br>E.g. if set to 300, logs of the same type will occure
  150. with a minimum rate of one per 5 minutes even if the device sends a message
  151. every minute. (Reduces the log file size and reduces the time to display
  152. the plots)
  153. </ul>
  154. <br>
  155. <a name="CUL_TXset"></a>
  156. <b>Set</b> <ul>N/A</ul><br>
  157. <a name="CUL_TXget"></a>
  158. <b>Get</b> <ul>N/A</ul><br>
  159. <a name="CUL_TXattr"></a>
  160. <b>Attributes</b>
  161. <ul>
  162. <li><a href="#ignore">ignore</a></li><br>
  163. <li><a href="#do_not_notify">do_not_notify</a></li><br>
  164. <li><a href="#showtime">showtime</a></li><br>
  165. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  166. </ul>
  167. <br>
  168. <a name="CUL_TXevents"></a>
  169. <b>Generated events:</b>
  170. <ul>
  171. <li>temperature: $temp</li>
  172. <li>humidity: $hum</li>
  173. </ul>
  174. <br>
  175. </ul>
  176. =end html
  177. =cut