Below is a skeleton of my final code… essentially I need to count the total kW of AC that is running at any point in time. But that will be dependant on various factors.
I have set a variable (KWDemand) at the beginning of my script hoping that this will be accessible to everything.
Then I have an IF statement that is calculating the kW for an AC Headunit, and adding this to the variable.
Finally I am writing this to an Input Number.
I know the IF statement is working correctly; but when I try to write this out the result is always zero.
I assume this is something to do with the scoping, but I cannot figure out how to fix it. Can anyone provide any advice?
Note:
Yes, I have tried reading the docs
Yes, I have been searching through the forums and Google.
Thanks for that it is a really good example (If only that was the official example!!!)
Although I am still having some issues:
I first changed it to the “var_3” example, but this did not seem to have any affect. Also the UI editor keeps moving the variables section to the end which is not intuitively correct from my view.
So then I switched to the version in his sample automation by putting it all in a sequence which has a nice side affect of making it more maintainable.
But I am still left with it writing out the contents of 0 (Or whatever I put in the variable instantiation.
I am left wondering if on line 17 I am creating a new variable that is then local to the IF statement, rather than updating the original copy…
<<After reading what I wrote, and reading the docs again…>>
Looking at it again just after writing that, I can see what I am doing wrong;. I am defining a new variable when I set 7.1… but I cannot figure out in HA how to just write to the original variable.
That is really to need to add a helper for a variable that is only within the scope of an automation… I’ve never seen a programming language like this in my life!!!