Homey Community Forum

Calculations in flows

Hi all,

Not sure what language works best here, but will do in English.

So i have a question about a flow I tried to build.
Goals of the flow:
I have solar panels and want to see how much electricity I consumed, how much of that came from the solar panels and how much i delivered back into the grid.
I short this calculation: (total energy used (of meter) + total energy from solar panels - energy delivered back to grid)
I’m thinking to run this flow on a daily basis.
Flow i created including quite some variables

The issue:
The idea was at start of flow i set value of that moment and at the end of the flow i set the same value in other variable. Doing this gives me the option to see the diff between yesterday and today of all meters. however the flow has been running for some time and i notice that the calculation always return 0, meaning my start value is being update before the calculation.
tech question, does the homey assign a value to the variable or a reference to the tag and it keeps updating it.

Note: other tips to get the same result are welcome, however i would like to understand why my logic isn’t working.

Thanks!

You have asynchronise “bug”.

You Set Electra Meter Eind as first, but Homey runs it asynchron (all at once, not in a sequence).
So 3 cards later (the 4th condition card) you allready use the Electra Meter Eind tag, while it probebly will not have ben set yet!

You should increase every actioncard with a one-second-more-delay-then-the-previous-card.
That way it will be executed in sequence (mostly, not always tho, if your Homey is busy for example).

(Or use the H.O.O.P. (Hope) - Homey Object Oriented Programming App which i created to solve this kind off problem (you can choose to execute the Actioncards synchronised (sequentuel).

So just to be clear, the issue is NOT the last actioncard
image

The issue is:
image

Those are run at the same time, and in my experience, the Getter goes faster then the Setter.
So at the 4th card, where you setElectra Meter Totaal, the Electra Meter Eind is still the same the day before.

Eather fix it with setting all delays, use HOOP, or accept the fact the Homey executes actioncards asynchronised/paralel/all-at-once :wink:

2 Likes

thanks and that explains :slight_smile:. Will take a look what option to take. Do you know if it can be fixed with homeyScript as well?

No, you cannot really fix it with HomeyScript, because you will have the same delay issue:

You could send all variables needed for the calculations to a HomeyScript-script, let it write into variables or tags, but you would still need to use an extra ActionCard to respond it to you, which would need a delay.

Allthough, you problebly can create a very large custom script that also executes your “Notify” cards, but it will cost you some time to get the knowleage which ActionCard your need to trigger the right Notification card.

HOOP makes this easy, you can use the current flow, just need to change the trigger to a HOOP trigger and set the Action Card execution way to synchronized execution.

Easiest way is just set a +1 (or 3) delay for each action card, that way it will most likely execute correctly /sequentuel.

like i said, there is no way (i know) without HOOP to get actioncards to fire really synchronized, if your Homey is busy while the first one is suppose to be start-and-finished, and the second one is started, it could be that the second one is finished before the first action card.

thanks, will look into HOOP for sure :+1:. just curious about some background as well and getting to know the homey.

1 Like

Well, with HomeyScript you are able to use scripting, and thuse you can do almost “anything” with it. But then you have to code a lott yourself.

And if you do want to code a lott yourself, then 1) get to know HomeyScript and 2) start your own private custom app.
In HomeyScript your script is a single file. So there is no real standarisation possibility.
Its good for single actions i would say (like your wished calculations), but if you want to program your whole house/homey yourself, create an app in which you place (and run from) the javascripts.

Thats my opinion, i starting with Homeyscript a while back and combining it with a private custom app a few years ago.

There are other ways to fix this. If the calculations are not interdependent, the execution order won’t matter. So rather use the original tag(s) when you can rather than the variable you also copied it to, and avoid intermediate results when you can. It won’t be prettier, but it will work better.

If the values are dependent, you could also split the flows and have a second calculation trigger when the variable it depends upon changes. But with a lot of interdependencies, that won’t work well either. This is more for simple cases.

When you’re doing a complex calculation, Homeyscript would fit best. You would probably want to use it in combination with BetterLogic, because you can set those variables from Homeyscript directly. If you want to set normal variables you have to put your Homeyscript in an AND condition, set local tags and copy those to variables in the then part. I would not reccomend it.

On a sidenote: Power by the hour provides nice ways for daytotoals and such, maybe a combination of that and a calculation might simplify things?

1 Like

@Arie_J_Godschalk Did you manage to get it to work?