[APP][Pro] Better Logic Library - For Users

found it!

This calculates the difference between the two times in minutes
Note that the logic cards needs to be set to text, not variable!

3 Likes

Wow, hats off!

I think it should read
ā€¦text, not numeric

edit I figured it out, I used Math.round(Date.now() / 1000)

How do i get the Unix or Epoch time to use as a tag? Previously Iā€™ve used $timenow$ with a mathJS card but Iā€™m trying to use the BLL card to a GET Request as it needs the current time in 10 digits.

I found this working script, but I canā€™t get it to work as BLL function

function epoch (date) { 
return Date.parse(date) 
} 
const dateToday = new Date() 
const timestamp = epoch(dateToday) console.log(timestamp)
1 Like

Hopefully is the picture clearly to understand.
Later I saw also a MMM option but that was giving the same error.

The formatting differs
First timestamp has this format:
yyyy-MM-ddTHH:mm:ss+01:00
The ā€œKNMIā€ timestamp has this format:
yyyy-MM-dd HH:mm:ss

I think you first should convert your ā€œKNMIā€ timestamp to that format, then I donā€™t see why it should error;
You could pre-define it in BLL format settings:

or use a flowcard

1 Like

That idea of different timestamps came also the my mind therefore I let you know.
There is also a split for having only HH:mm:ss and I am afraid to get there problems.

Now I know where I have to look for I will trying those other things.

Edit

Found
date(ā€œISOZā€) gives 2024-04-16T12:56:05.547+02:00

2 Likes

That date(ā€œISOZā€) gives the right format for the day, for now.
BTW ISO gives without the +hh:mm

The KNMI gives this format by JSON. Time: ā€œ16-04-2024 18:58:03ā€
There is also this Timestamp: 1713286683

For the time the only option I see now is Cut & Past in date-text.
Moving 2024 and 16 and added the T.

For the Timestamp you need a script for the conversion.

Lot of thinking to do. :wink:


Edit 1

Previously I received a tip to get the time from a ā€˜date with timeā€™.
That was a .split(ā€™ ')[1] command.

Now I have found to put a ā€œTā€+ in front of it and get part of the correct time format.
ā€œTā€+ā€œ16-04-2024 17:08:04ā€.split(ā€™ ')[1]

Where can I see more of these ideas to continue puzzling?
Copy the day and place it in front of that T.
Do the same with the month and year.

Edit 2

By putting split(ā€™ ') in the search I came to .slice( , ).

The input = 16-04-2024 21:38:03
The output = 2024-04-16T21:38:03

Now the variables with only the time I will let go otherwise I have 2 x 3 needed.

Edit 3

My goal in post 144 is sadly not there.
The parseInt to ā€˜nowā€™ worked for both date.
Unfortunately the difference between the two is not yet clear.
So now or then itā€™s working but mostly not.
The error is then the same as in post 144.

For now :sleeping:

Edit 4

Finally I found why I was not getting a 0 out of that formule when I replaced ā€˜nowā€™ with in the exact same way with the other value.
Then I try to put ( ) around every parseInt(time('M', new Date('2023-07-03T16:15:12+01:00')

That make it then this:
(parseInt(time(ā€˜Mā€™, new Date(ā€™ Data update KNMI ISOā€™)) - (parseInt(time(ā€˜Mā€™, new Date(ā€˜Data update Weerlive ISOā€™))))))

you can do the conversion, by the same method I used, which is a lot simpler (unless I am missing something. by:

image

Setting how you want the date format to look like. You can change the order and add text wherever you want (see I added the T here ass well)

But if you get the date as ā€œ26-04-2024 16:48:04ā€ from a Json File?

I have some problems doing calculations with negative numbers, e.g. in the following test-flow I want to subtract the value of ā€œAbweichung Raumtemperaturā€ from 1, whereby ā€œAbweichung Raumtemperaturā€ is a negative number. It works fine when using the standard logic card to calculate a variable, but it fails with the shown error when trying to use the BLL card to calculate the values as a tag.

image

It seems itā€™s trying to evaluate 1ā€“0.5 (watch the two minus signs). I could put ā€œAbweichung Raumtemperaturā€ into round brackets, but I canā€™t do that for all variables/capabilities in all circumstances.

What Iā€™m a doing wrong? I canā€™t imagine, that BLL canā€™t handle negative variables/capabilities.

When I do 1- -0.5 then I get 1.5.

Think you have to put a space after 1-.

Thatā€™s another work-around, thanks.

Indeed, when you place 2 minus characters next to each other, you are telling JS to subtract 1 from the variable in front of it.
But since the value 1 is not a variable, you recieve the error.

So the solution is indeed to place a space between the first minus character and the following (minus) value.

Iā€™m sorry, but thatā€™s not a solution. Iā€™m not placing two minus characters next to each other, Iā€™m subtracting one value from another and both of those values may be negative. The logic card handles this correctly.

Even if the work-around works, I would mark it as a design bug in the formula parser. It looks as if there is a simple macro substituting the variable by its value. But a variable is an entity and should not be combined with something else. That is especially dangerous, if a formula works fine for months. until the first negative value occurs, and either stops or has unexpected side effects, when after a-b suddenly a is lowered by the value of b.
So I would propose that the Better Logic Library sets blanks or brackets when dealing with variables instead of moving the responsibility to the user.
Just my 2 cts to the topic. Currently I do not dare to use this App, although I installed it.
I do not really understand the description in the readme text, how to use it effectively. I wanted to run some test to understand, what is written in readme.txt, and to find out what exactly is a BLL expression, etc.
And now it seems to be dangerous. so probably I better do not touch it at the moment.

2 Likes

Thank you for this elaborate explanation that identifies the real problem. I fully agree with you that this behaviour is unexpected, intransparent and therefore pretty dangerous. But a fix should be fairly easy as you already explained; IMO just adding spaces around variables should do the trick.

In my opinion BLL is pretty much a must-have if you are doing more complex flows. Iā€™m only using a very small part of its capabilities, mostly calculating some tags, but compared to using the default logic cards, BLL is way more powerful and convenient. Not only can you do more complex calculations, you have more math functions at your disposal and - the absolute killer argument - you can get your results in tags and not only store it in some logic variable. This makes flows a lot clearer and compact, avoids race conditions and you need homey script a lot less.

Another feature that I rely on is the ability to get the timestamp of the last update of a BLL variable. I use that all the time to update various energy numbers, where I need to know when the power changed the last time in order to calculate the correct energy. Yes, I could do that also with logic variables or timers or with a separate PbtH device, but itā€™s far easier done with BLL.

2 Likes

Unfortunately, this is not possible, because a tag/token is parsed by Homey as plain text. BLL cannot know that its a token, tag or variable. This is a limitation of how Homey works, and Unfortunately there is not much i can do about that.

Thank you @CaptainVoni,
probably I am too old and/or just not used to more modern programming languages than C or Visual Basic, not used to JavaScript, etc.