Regex für manchmal nicht vorhandenen Teil

Begonnen von MarcoE, 13 April 2023, 16:57:09

Vorheriges Thema - Nächstes Thema

MarcoE

Hallo,
ich habe ein Problem mit Regexp. Ich bekomme per httpmod zb folgendes zurück:
<FONT style="BACKGROUND-COLOR: FF0000">busy</FONT><div>blabla</div>busy soll in ein Reading (Status):
<FONT.*?>(.*?)<\/FONT>
blabla in ein anderes (Status2):
<div>(.*?)<\/div>
Soweit prima. Nur kann es manchmal sein, das folgendes per httpmod zurück kommt:
<FONT COLOR="00FF00">free</FONT> (ja tatsächlich ohne den div Teil)

Nun kommt in Status tatsächlich free aber in Status2 bleibt der alte Wert erhalten blabla.
Den kann ich allerdings nicht brauchen- der String sollte leer sein.

Wie kann man das lösen?

Danke und viele Grüße
Marco

frank

für status2 kannst du zb folgendes nutzen

Zitatreplacing reading values when they have not been updated / the device did not respond

    If a device does not respond then the values stored in readings will keep the same and only their timestamp shows that they are outdated. If you want to modify reading values that have not been updated for a number of seconds, you can use the attributes
        (reading|get)[0-9]*(-[0-9]+)?MaxAge
        (reading|get)[0-9]*(-[0-9]+)?MaxAgeReplacementMode
        (reading|get)[0-9]*(-[0-9]+)?MaxAgeReplacement
    Every time the module tries to read from a device, it will also check if readings have not been updated for longer than the MaxAge attributes allow. If readings are outdated, the MaxAgeReplacementMode defines how the affected reading values should be replaced. MaxAgeReplacementMode can be text, reading, internal, expression or delete.
    MaxAge specifies the number of seconds that a reading should remain untouched before it is replaced.
    MaxAgeReplacement contains either a static text that is used as replacement value or a Perl expression that is evaluated to give the replacement value. This can be used for example to replace a temperature that has not bee updated for more than 5 minutes with the string "outdated - was 12":
        attr PM readingMaxAge 300
        attr PM readingMaxAgeReplacement "outdated - was " . $val
        attr PM readingMaxAgeReplacementMode expression The variable $val contains the value of the reading before it became outdated.
    If the mode is delete then the reading will be deleted if it has not been updated for the defined time.
    If you want to replace or delete a reading immediatley if a device doid not respond, simply set the maximum time to a number smaller than the update interval. Since the max age is checked after a HTTP request was either successful or it failed, the reading will always contain the read value or the replacement after a failed update.
FHEM: 6.0(SVN) => Pi3(buster)
IO: CUL433|CUL868|HMLAN|HMUSB2|HMUART
CUL_HM: CC-TC|CC-VD|SEC-SD|SEC-SC|SEC-RHS|Sw1PBU-FM|Sw1-FM|Dim1TPBU-FM|Dim1T-FM|ES-PMSw1-Pl
IT: ITZ500|ITT1500|ITR1500|GRR3500
WebUI [HMdeviceTools.js (hm.js)]: https://forum.fhem.de/index.php/topic,106959.0.html

TomLee

oder das alte Reading löschen, wenn nix matcht ?

Zitatget|reading[0-9]*DeleteIfUnmatched
If set to 1 this attribute causes certain readings to be deleted when the parsing of the website does not match the specified reading. Internally HTTPMOD remembers which kind of operation created a reading (update, Get01, Get02 and so on). Specified readings will only be deleted if the same operation does not parse this reading again. This is especially useful for parsing that creates several matches / readings and this number of matches can vary from request to request. For example if reading01Regex creates 4 readings in one update cycle and in the next cycle it only matches two times then the readings containing the remaining values from the last round will be deleted.
Please note that this mechanism will not work in all cases after a restart. Especially when a get definition does not contain its own parsing definition but ExtractAllJSON or relies on HTTPMOD to use all defined reading.* attributes to parse the responsee to a get command, old readings might not be deleted after a restart of fhem.