Math Comparison Fail

I am using the current version of KM, 9.2. I am downloading my bills and statements from websites. Since they get posted throughout the month I want to compare the day of the month they get posted with today's day. However, as you'll see from the below screen shots, every combination of testing the date I want to run getting a particular bill or statement ("Run_Date") against today's date ("Todays_Day") comes up false. That is every combination of <, <=, =, >=, >, !=. Obviously, some of those would have to be true if their counterpart is false. So something is clearly wrong.

I also did a test on Matches and Does Not Match just to be sure the variables were being evaluated. That does work in that my test date of "2" did show false when asked if matched today's date (which happens to be "7), but did show True for the Did Not Match test. Likewise, when I set Run_Date to 7 the Match was True and the Does Not Match was false.

That makes me think that KM is treating them like strings (but even so the string "2" is less than the string "7"). But I saved the variables as calculations which should have saved them as numbers. Anyone know how to fix this or what the problem is? BTW, I was having this same problem before I upgraded, which was 8.2.4, and one of the main reasons I did so was hoping this was fixed.

< False

<= False

= False

= False

False

!= False

2 Matches 7 False

2 Does Not Match 7 True

7 Matches 7 True

7 Does Not Match 7 False

It seems to work if you change the If action as follows. Not sure why it doesn't work as you have it, but I suspect your string/number thought is correct

Tester Macro (v9.2)

Tester.kmmacros (2.0 KB)

I can give you a possible explanation. Take these 2 examples of the same 2 actions in a macro:

This one plainly is showing nonsense - why when 1=1 is it currently false?

KM 0 2021-05-07_16-11-22

But this one is correct.

KM 1 2021-05-07_16-13-39

The explanation for this disparity is that the second example was taken after I had run the macro.

It has been brought up a number of times previously and the advice has been to not rely on the currently true/false indication.

I don't know if the examples you've shown are from macros you have actually tried running or not, but if not this might be the explanation.

Let us know if that's the case. Cheers.

2 Likes

It is always best to actually run the macro to test it.

Having said that, the evaluation shown in the KM Editor is actually correct:

1 Like

Yep. What is wrong is that you have NOT entered a valid KM Variable in the numeric calculation field of the IF/THEN Action:

image

When using a KM Variable in a numeric calculation field, just enter the Variable Name.
Do NOT enter it as a Variable Token with the % symbols.

As stated above don't rely fully on the KM Editor evaluation. Always run the Macro to test it.
Also, in the future, always upload your macro so that it is easy for other to inspect and test.

Here's is my TEST macro that works fine:

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Math TEST

-~~~ VER: 1.0    2021-05-07 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Math TEST.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Stepping back, the picture is that:

  1. KM variables are stored as strings,
  2. when you want them interpreted as numbers, so that you can perform numeric tests and other numeric operations on them, you need to do that in one of the special calculation fields.
  • You know that your cursor is in a calculation field when a small letter C appears at top right of that field.
  • If you are seeing a T at top right then you are in a field which interprets your value as text, and not as a number (unless you make explicit use of the %Calculate% token).

The details are here:

[manual:Calculations [Keyboard Maestro Wiki]](manual:Calculations [Keyboard Maestro Wiki])

Good catch! I should’ve tried it with a local variable before putting my foot in it :wink:

So here it is with a local variable:

KM 0 2021-05-08_13-22-01

and, of course, it's showing (currently false).

And it remains this way even after running the macro - so the advice to not rely on the currently true/false indication is still very much valid.

1 Like

Thanks to all!!

BTW, I had run them before relying on the results. I should have added that to my post.

That said, I found the simplest solution was to change %Variable%Todays_Day% to just Todays_Day per JMichaelTX suggested.

I'm not sure where in the documentation that is.

Thanks again,

Stuart

1 Like

The post by @ComplexPoint provided the reference.

Thanks! Got it.