It is currently April 25th, 2024, 9:50 am

Remove decimals

Get help with creating, editing & fixing problems with skins
one1tick
Posts: 7
Joined: March 19th, 2022, 6:48 pm

Re: Remove decimals

Post by one1tick »

balala wrote: March 20th, 2022, 4:57 pm See the added MeasureName and AutoScale options as well as the replaced Text option of the [mqttOutput] meter.
With the AutoScale=2 option, the meter will show the consumption into W if it is below 1000 W = 1 kW, or in kW, if it is above.

Additional note: Update=10000 in the [Rainmeter] section is definitely not a good approach. It means you get the skin updated once on every 10 seconds. To can use the skin to see the real consumption, I'd tend to recommend to use the default Update=1000.
Thanks, I had update = 10000 because mqtt value is updated every 10sec. AutoScale would be a great option but it does not work in my case... I have slightly changed my code to change the labale based on a negative net consumption (sell to the grid) and positive consumption (buy from the grid). Thanks

Code: Select all

[Rainmeter]
Update=1000

[Style1]
FontColor=255,255,255
FontFace=Segoe UI
FontSize=15
AntiAlias=1

[mqttServer]
Measure=Plugin
Plugin=MqttClient
;Connection Details
Server=192.168.1.126
Port=1883
Username
Password=
;Update the meter when the client connects:
OnConnect=[!UpdateMeter mqttOutput][!Redraw]
;Update the meter as soon as a message arrives:
OnMessage=[!UpdateMeter mqttOutput][!Redraw]

;Subscribe to kostal/Grid_P
[mqttTopic1]
Measure=Plugin
Plugin=MqttClient
ParentName=mqttServer
Topic=kostal/Grid_P
;Label based on Net Consumption (Buy if > 0 / Sell if < 0)
IfCondition=[mqttTopic1]<0
IfTrueAction=[!SetOption MeterLabel Text "Sell:"]
IfFalseAction=[!SetOption MeterLabel Text "Buy:"]
;Format with thousands separators and no decimal  => 2345.4 to 2'345
;https://forum.rainmeter.net/viewtopic.php?t=17379
;RegExpSubstitute=1
;Substitute="\d{1,3}(?=(\d{3})+\.)":"\0'","(\d*)\.(\d*)":"\1"
;DynamicVariables=1

[MeterOutput]
Meter=String
MeasureName=mqttTopic1
X=70
Y=5
W=350
H=30
MeterStyle=Style1
AutoScale=2
NumOfDecimals=0
Text=%1W
DynamicVariables=1

[MeterLabel]
Meter=String
X=5
Y=5
W=350
H=30
MeterStyle=Style1
User avatar
Yincognito
Rainmeter Sage
Posts: 7157
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Remove decimals

Post by Yincognito »

one1tick wrote: March 20th, 2022, 5:07 pm Thanks @Yincognito appreciate your comment. However the regex I have found is not a "random" one found on the forum and I don't think it makes no sense in my case. Maybe there is a more efficient one and I would be happy if you have a better way to convert the raw data coming from mqtt.
I want to show the the consumption of my home and the power I buy from the grid (or the power I sell from my pv production) without decimals and with thousand separators.

Examples:
10234.6 => 10'234W
-1234.7 => -1'234W

Substitute="\d{1,3}(?=(\d{3})+\.)":"\0'","(\d*)\.(\d*)":"\1" is doing what I want but again I would be happy if there is a better way to achieve my need.

Thanks again.
Oh, I see, so you want to use the ' as the thousands separator? You didn't mention this anywhere in your posts, you only said about removing decimals so I assumed it was a mistake - the help is only as good as explaining the purpose by the OP is. A better choice would be Substitute="\d{1,3}(?=(\d{3})+(?:\.|$))":"\0'","(\d*)\.(\d*)":"\1" so you can cover the case when the number is already without decimals, say -167876234.

Or, go with AutoScale=2. Apparently AutoScale=2 coupled with NumOfDecimals=0 has two bugs: it doesn't work with negative numbers and it yields subunitar values (e.g. 0.577) as 0. So, this will do:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[String]
Measure=String
String=-167876234.7
;RegExpSubstitute=1
;Substitute="\d{1,3}(?=(\d{3})+(?:\.|$))":"\0'","(\d*)\.(\d*)":"\1"
DynamicVariables=1

[Number]
Measure=Calc
Formula=(Round(Abs([String])))
DynamicVariables=1

[Sign]
Measure=Calc
Formula=([Number]<1?0:Sgn([String]))
RegExpSubstitute=1
Substitute="\d":""
DynamicVariables=1

---Meters---

[Result]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
MeasureName=Number
AutoScale=2
NumOfDecimals=0
Text="Result = [Sign]%1"
DynamicVariables=1
Feel free to adapt it to your needs, if you're ok with it. I kept the RegExp Substitute there for reference only. The conditional in [Sign] is to prevent displaying stuff like -0 for negative but subunitar values.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Remove decimals

Post by balala »

Yincognito wrote: March 20th, 2022, 6:31 pm and it yields subunitar values (e.g. 0.577) as 0.
When a variable (number) is varying between 0 and a few thousands, 0,577 is practically 0. This is not a problem and is normal, I think. If the number is varying between 0 and a few units (up to 10 or maybe even 100), it would make sense, but if can go up to more thousands, well, I doubt it is a problem.
User avatar
Yincognito
Rainmeter Sage
Posts: 7157
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Remove decimals

Post by Yincognito »

balala wrote: March 20th, 2022, 6:55 pm When a variable (number) is varying between 0 and a few thousands, 0,577 is practically 0. This is not a problem and is normal, I think. If the number is varying between 0 and a few units (up to 10 or maybe even 100), it would make sense, but if can go up to more thousands, well, I doubt it is a problem.
It is a bug, no matter how you look at it, sorry. :confused: What you said would be correct if it happened for 1.577 or 2.577 and so on as well, but it does not, even though they are just as small compared to thousands like 0 is. So, if you have something like:

Code: Select all

[Variables]
Value=0.577

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[Number]
Measure=Calc
Formula=#Value#
DynamicVariables=1

---Meters---

[Result]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
MeasureName=Number
AutoScale=2
NumOfDecimals=0
Text="Result = %1"
MouseScrollUpAction=[!SetVariable Value (#Value#+1)][!Update]
MouseScrollDownAction=[!SetVariable Value (#Value#-1)][!Update]
DynamicVariables=1
and you scroll through neighboring values, you'll see that the result jumps from 0 to 2, like 1 doesn't exist at all (nevermind the -0 which I already talked about, and which is somewhat normal in some programming languages - I'm not saying this because Rainmeter is a programming language, but because it was made in one). Bear in mind that this is not [Number:/1000,0], for which you would be right to expect such a behavior, since that yields 0 for everything from 0 to 600 (which is normal). AutoScale=2 does not, it yields all the sub 1000 values ... except 1, as mentioned above.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Remove decimals

Post by balala »

Yincognito wrote: March 20th, 2022, 7:29 pm It is a bug, no matter how you look at it, sorry.
Then you should post it into the Bugs & Feature Suggestions section.
one1tick
Posts: 7
Joined: March 19th, 2022, 6:48 pm

Re: Remove decimals

Post by one1tick »

Yincognito wrote: March 20th, 2022, 6:31 pm Oh, I see, so you want to use the ' as the thousands separator? You didn't mention this anywhere in your posts, you only said about removing decimals so I assumed it was a mistake - the help is only as good as explaining the purpose by the OP is. A better choice would be Substitute="\d{1,3}(?=(\d{3})+(?:\.|$))":"\0'","(\d*)\.(\d*)":"\1" so you can cover the case when the number is already without decimals, say -167876234.

Or, go with AutoScale=2. Apparently AutoScale=2 coupled with NumOfDecimals=0 has two bugs: it doesn't work with negative numbers and it yields subunitar values (e.g. 0.577) as 0. So, this will do:
Feel free to adapt it to your needs, if you're ok with it. I kept the RegExp Substitute there for reference only. The conditional in [Sign] is to prevent displaying stuff like -0 for negative but subunitar values.
Thanks I will have a look tomorrow.
User avatar
Yincognito
Rainmeter Sage
Posts: 7157
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Remove decimals

Post by Yincognito »

balala wrote: March 20th, 2022, 7:35 pmThen you should post it into the Bugs & Feature Suggestions section.
Done. Delayed because of more testing, to be sure I'm not seeing things out of the blue.
one1tick wrote: March 20th, 2022, 8:13 pmThanks I will have a look tomorrow.
No problem - maybe by then we'll have an answer regarding why AutoScale works the way it does.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Remove decimals

Post by balala »

Yincognito wrote: March 20th, 2022, 8:41 pm Done. Delayed because of more testing, to be sure I'm not seeing things out of the blue.
Alright. Let's see what devs will say.
User avatar
Yincognito
Rainmeter Sage
Posts: 7157
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Remove decimals

Post by Yincognito »

balala wrote: March 20th, 2022, 8:48 pm Alright. Let's see what devs will say.
Yup. Hopefully the rounding part is not related to some unmodifiabel C++ / C# behavior, see my edit in the Bugs post.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
Yincognito
Rainmeter Sage
Posts: 7157
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Remove decimals

Post by Yincognito »

balala wrote: March 20th, 2022, 8:48 pm Alright. Let's see what devs will say.
Looks like a strange bug, after all... :???:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth