# Lambda and timer question

Simplified presentation/question

``````var x = Item1.state
var y = Item2.state
var fSeconds = x/y
z = x + y //in my script this line consists of ~60 lines of code
Item3.sendCommand(z)
Item1.postUpdate(x + 1)

wait fSeconds
Item3.sendCommand(z) //which requires to recalculate z because x has changed
Item1.postUpdate(x + 1)
``````

I know that I can do it the ugly way by copy/pasting the whole calculation into createTimer() lambda function.
Does anybody have a better idea? I am not that experienced with lambda function maybe that might help?

All the variables that exist before you create the timer and the timerâ€™s lambda will exist inside the timerâ€™s lambda. So you just need the last two lines of code in the lambda. It will inherit everything else.

Frankly, almost every time someone tries to give a â€śsimplified presentationâ€ť of the code they are writing about, it almost always makes our job harder, not easier. Is it the calculation of z that you are trying not to reproduce? Are you expecting x to be incremented twice in this rule? What is this code really trying to do?

Youâ€™ve removed everything that can answer those questions. And since this smells like an XY Problem, the best I can offer is the vague advice at the start of this reply.

In general, more information is better. We here on the forum are really good at filtering out the stuff that is irrelevant. Posters asking the questions are rarely very good at doing this filtering (they probably wouldnâ€™t need to ask the question in the first place if they were).

Thanks Rich,
ok understood - I just tried to keep it easier for you.
The rule is here
what I try to achieve is to make use of that part of my rule that generates the payload to be sent to a python script.
It needs to be calculated twice to send stop command after fSeconds if the command is a value between 1 and 99

And thatâ€™s why itâ€™s important to post the whole code because that was not at all clear in your original post. If you need to redo the `z = x + y` calculation after the `wait fSeconds` then you can create a lambda that gets called twice to do the calculations.

1 Like

I saw that post already but was not sure if that realy is the solution for my problem.
I will give it a try now.
Many thanks!