Autor Thema: HTTPMOD - Regex to extract W value from kW/W value  (Gelesen 2653 mal)

Offline Ed66

  • New Member
  • *
  • Beiträge: 8
HTTPMOD - Regex to extract W value from kW/W value
« am: 06 Juni 2017, 14:18:33 »
Hi together,

I'm using HTTPMOD to successfully extract several values from my Enphase Envoy solarcontroller. I am struggling with the creating a "smart" regex to derive the actual production in a uniform way: it displays in W when the value <= 999 W and switches to kW when above: x.xx kW

The webpage source:
<td>Currently generating</td>    <td>   671 W</td></tr>
or alternatively:
<td>Currently generating</td>    <td>   1.72 kW</td></tr>

I would like to use a single regex/reading that always returns the Watts, I tried something with looking ahead and replacing but my knowledge does not suffice. I try to avoid using multiple regexes/readings to identify the W and kW separately.
This is what I basically started with:

<td>Currently generating</td>[^0-9]+([0-9\.]+.*W)

I hope you can push me a little in the right direction.

KR,
Edwin

Offline DeeSPe

  • Developer
  • Hero Member
  • ****
  • Beiträge: 4628
  • Wer anderen eine Bratwurst brät...
    • Buy me a coffee
Antw:HTTPMOD - Regex to extract W value from kW/W value
« Antwort #1 am: 06 Juni 2017, 14:53:46 »
Try if this may work for you:
generating<\/td>\s+<td>\s+(\d+)\sW
http://www.regextester.com/ could help you finding the proper regex.

Cheers
Dan
MAINTAINER: 22_HOMEMODE, 98_Hyperion, 98_FileLogConvert, 98_serviced

Als kleine Unterstützung für meine Programmierungen könnt ihr mir gerne einen Kaffee spendieren: https://buymeacoff.ee/DeeSPe

Offline Ed66

  • New Member
  • *
  • Beiträge: 8
Antw:HTTPMOD - Regex to extract W value from kW/W value
« Antwort #2 am: 06 Juni 2017, 17:55:27 »
Hi Dan,

my challenge is to conditionally (only when webpage reports in kW) translate the kilowatts into watts through a regex. In the example the 1.72 should translate into 1720, but only in case a kW is matched after the value/string 1.72

Rgds, Edwin   



Offline Univega06

  • New Member
  • *
  • Beiträge: 6
Antw:HTTPMOD - Regex to extract W value from kW/W value
« Antwort #3 am: 02 Januar 2023, 08:42:53 »
Hi Edwin,
I try to extract the Values from the Enphase Envoy. Is it possible to get your full definition? have you solved the problem with the kW/W?

Thanks
Kai

Offline erwin

  • Developer
  • Sr. Member
  • ****
  • Beiträge: 848
Antw:HTTPMOD - Regex to extract W value from kW/W value
« Antwort #4 am: 02 Januar 2023, 09:40:44 »
Hi Kai,
try this:
my $str = ' 671.5 W</td>';
#$str = ' 1234.5 kW</td>';

$str =~ s/([\d\.]+)\s([k])?W.*/$2?$1*1000 . ' W':$1 . ' W'/e;
regards erwin
FHEM aktuell auf RaspberryPI Mdl 1-4
Maintainer: 00_KNXIO.pm 10_KNX.pm
User: CUNO2 (868 SLOWRF) - HMS100xx, FS20, FHT, 1-Wire  - 2401(iButton), 18x20, 2406, 2413 (AVR), 2450,..,MQTT2, KNX, SONOFF, mySENSORS,....
Hardware:  Busware ROT, Weinzirl IP731, 1-Wire GW,...

 

decade-submarginal