It is currently October 18th, 2024, 3:51 am

Hide meters when no value available from WebParser

Get help with creating, editing & fixing problems with skins
User avatar
Phuturist
Posts: 3
Joined: December 1st, 2015, 7:47 pm

Hide meters when no value available from WebParser

Post by Phuturist »

I'm retrieving a JSON that looks like this.

Code: Select all

{
    "entity_id": "input_text.reminders",
    "state": "reminder text 1, reminder text 2, reminder text 3, reminder text 4",
    "attributes": {
        "editable": true,
        "min": 0,
        "max": 9999999,
        "pattern": null,
        "mode": "text",
        "icon": "mdi:checkbox-marked-circle-auto-outline",
        "friendly_name": "Reminders"
    }
}
I want to show the first 5 values from the comma separated state object. So I created the config as shown below. Probably not very elegant but it shows the values. The problem is that sometimes there are less than 5 values in the state object and I need to hide the meters related to the missing values. I somehow need to check if the value is present and if not hide the meters. How do I do this?

Code: Select all

[MeasureParent]
Measure=WebParser
URL=https://some.url
UpdateRate=300
RegExp="\"state\":\"([^\"]+)\""

[MeasureItem1]
Measure=WebParser
URL=[MeasureParent]
StringIndex=1
RegExpSubstitute=1
Substitute="([^,]+).*":"\1"
Substitute=#Substitute#

[MeasureItem2]
Measure=WebParser
URL=[MeasureParent]
StringIndex=1
RegExpSubstitute=1
Substitute="[^,]+,\s*([^,]+).*":"\1"
Substitute=#Substitute#

[MeasureItem3]
Measure=WebParser
URL=[MeasureParent]
StringIndex=1
RegExpSubstitute=1
Substitute="[^,]+,\s*[^,]+,\s*([^,]+).*":"\1"
Substitute=#Substitute#

[MeasureItem4]
Measure=WebParser
URL=[MeasureParent]
StringIndex=1
RegExpSubstitute=1
Substitute="[^,]+,\s*[^,]+,\s*[^,]+,\s*([^,]+).*":"\1"
Substitute=#Substitute#

[MeasureItem5]
Measure=WebParser
URL=[MeasureParent]
StringIndex=1
RegExpSubstitute=1
Substitute="[^,]+,\s*[^,]+,\s*[^,]+,\s*[^,]+,\s*([^,]+).*":"\1"
Substitute=#Substitute#

;[METERS]==========================
[Item1Box]
Meter=Shape
Shape=Rectangle 0,3,13,13,2 | Fill Color 255,255,255,0 | StrokeWidth 2 | Stroke Color 255,255,255

[Item1]
Meter=STRING
MeasureName=MeasureItem1
X=22
Y=0
MeterStyle=standaardFont

[Item1Separator]
Meter=IMAGE
MeterStyle=Separator

[Item2Box]
Meter=Shape
Shape=Rectangle 0,40,13,13,2 | Fill Color 255,255,255,0 | StrokeWidth 2 | Stroke Color 255,255,255

[Item2]
Meter=STRING
MeasureName=MeasureItem2
X=22
Y=37
MeterStyle=standaardFont

[Item2Separator]
Meter=IMAGE
MeterStyle=Separator

[Item3Box]
Meter=Shape
Shape=Rectangle 0,80,13,13,2 | Fill Color 255,255,255,0 | StrokeWidth 2 | Stroke Color 255,255,255

[Item3]
Meter=STRING
MeasureName=MeasureItem3
X=22
Y=77
MeterStyle=standaardFont

[Item3Separator]
Meter=IMAGE
MeterStyle=Separator

[Item4Box]
Meter=Shape
Shape=Rectangle 0,120,13,13,2 | Fill Color 255,255,255,0 | StrokeWidth 2 | Stroke Color 255,255,255

[Item4]
Meter=STRING
MeasureName=MeasureItem4
MeasureName2=MeasureItem1
X=22
Y=117
MeterStyle=standaardFont
ClipString=1

[Item4Separator]
Meter=IMAGE
MeterStyle=Separator

[Item5Box]
Meter=Shape
Shape=Rectangle 0,160,13,13,2 | Fill Color 255,255,255,0 | StrokeWidth 2 | Stroke Color 255,255,255

[Item5]
Meter=STRING
MeasureName=MeasureItem5
MeasureName2=MeasureItem1
X=22
Y=157
MeterStyle=standaardFont
ClipString=1

[Item5Separator]
Meter=IMAGE
MeterStyle=Separator
User avatar
balala
Rainmeter Sage
Posts: 16669
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Hide meters when no value available from WebParser

Post by balala »

Phuturist wrote: July 15th, 2024, 5:47 pm How do I do this?
To do this, you need IfMatch options. This options when used checks if the string returned by the measure in which they are used, match the posted expression. If it is, IfMatchAction is executed, otherwise the IfNotMacth option.
So, try adding such set of options to each of the [MeasureItem1] - [MeasureItem5] measures:

Code: Select all

[MeasureItem1]
...
IfMatch=^$
IfMatchAction=[!HideMeter "Item1Box"][!HideMeter "Item1"][!HideMeter "Item1Separator"][!Redraw]
IfNotMatchAction=[!ShowMeter "Item1Box"][!ShowMeter "Item1"][!ShowMeter "Item1Separator"][!Redraw]
I posted here only the options related to the [MeasureItem1] measure. Obviously you have to add similar options to the other four measures as well. Hope you can add them. Please come back if you get problems on adding them.
User avatar
Phuturist
Posts: 3
Joined: December 1st, 2015, 7:47 pm

Re: Hide meters when no value available from WebParser

Post by Phuturist »

balala wrote: July 15th, 2024, 9:05 pm To do this, you need IfMatch options. This options when used checks if the string returned by the measure in which they are used, match the posted expression. If it is, IfMatchAction is executed, otherwise the IfNotMacth option.
So, try adding such set of options to each of the [MeasureItem1] - [MeasureItem5] measures:

Code: Select all

[MeasureItem1]
...
IfMatch=^$
IfMatchAction=[!HideMeter "Item1Box"][!HideMeter "Item1"][!HideMeter "Item1Separator"][!Redraw]
IfNotMatchAction=[!ShowMeter "Item1Box"][!ShowMeter "Item1"][!ShowMeter "Item1Separator"][!Redraw]
I posted here only the options related to the [MeasureItem1] measure. Obviously you have to add similar options to the other four measures as well. Hope you can add them. Please come back if you get problems on adding them.
Thanx for the quick response. Unfortunately this isnt working as it will show the complete string of all items for the index that have no corresponding value. Therefore the IfMatch is always false.

For instance with a string of "reminder text 1, reminder text 2, reminder text 3" and the current webparser and measures the value of MeasureItem4 and MeasureItem5 will both return "reminder text 1, reminder text 2, reminder text 3" and thus show on the desktop.

I'm struggling to find an IfMatch regexp that will validate true/false based on the presence of a value.

[EDIT]
I hacked around this by checking if the regexp substitute string contains a comma. That works but obviously breaks when one of the values actually contains a comma. But as that probably wont happen I'm good with it. Thanks again for pointing me in the right direction.
User avatar
balala
Rainmeter Sage
Posts: 16669
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Hide meters when no value available from WebParser

Post by balala »

Phuturist wrote: July 16th, 2024, 7:27 pm Thanks again for pointing me in the right direction.
You're welcome. I'm glad if you figured out how to get it working. Unfortunately I can't check the code, I assume you know why, so what I wrote was a general thing.
User avatar
Yincognito
Rainmeter Sage
Posts: 8485
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Hide meters when no value available from WebParser

Post by Yincognito »

Phuturist wrote: July 16th, 2024, 7:27 pm For instance with a string of "reminder text 1, reminder text 2, reminder text 3" and the current webparser and measures the value of MeasureItem4 and MeasureItem5 will both return "reminder text 1, reminder text 2, reminder text 3" and thus show on the desktop.
I think you might want to take a look here, as that might help:
https://docs.rainmeter.net/tips/webparser-lookahead-assertions-in-regexp/
Then, you'll be able to use the IfMatches recommended by balala much more comfortably.

By the way, quotes don't need to be escaped by preceding them with a \ in the PCRE flavor of regex.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth