[Solved] Define function issue in latest-25304
-
Edit: Solved in 25310
I'm getting errors for all my defined functions in latest-25304 bare metal install and a simple test from the docs throws an error as well.
Reverting to 25208 gets them working again.
Anything changed fordefine <functionName>( <args...> ) <expression>in 25304, is it a bug or am I doing something wrong?[latest-25304]2025-11-04T11:07:45.290Z <Engine:NOTICE> Engine#1 expression testHere has changed [latest-25304]2025-11-04T11:07:45.291Z <Engine:ERR> Engine#1: error evaluating expression testHere: [TypeError] Cannot set properties of undefined (setting 'square') [-] [latest-25304]2025-11-04T11:07:45.291Z <Engine:ERR> Engine#1: expression: define square(a) a*a [latest-25304]2025-11-04T11:07:45.291Z <Engine:CRIT> TypeError: Cannot set properties of undefined (setting 'square') [-] TypeError: Cannot set properties of undefined (setting 'square') at _run (/home/homebridge/reactor/common/lexp.js:1804:41) at /home/homebridge/reactor/common/lexp.js:1425:29 at Array.forEach (<anonymous>) at _run (/home/homebridge/reactor/common/lexp.js:1424:28) at Object.run (/home/homebridge/reactor/common/lexp.js:1854:24) at Engine._eval_expr (/home/homebridge/reactor/server/lib/Engine.js:1166:55) at Engine.notify (/home/homebridge/reactor/server/lib/Engine.js:840:169) at /home/homebridge/reactor/server/lib/MessageBus.js:102:292 at Array.forEach (<anonymous>) at MessageBus._sendToBus (/home/homebridge/reactor/server/lib/MessageBus.js:99:52) -
Can you show me
testHere? -
Sorry... not caffeinated enough yet. Is that expression a dependency of any other?
-
OK. Technically, that's not supported (defining a function in a global variable for use in another), and the fixes for other issues now exclude that explicitly (although you should not have received a runtime exception because of it). But, I will have a workaround for you in a (near, today) future build.
-
Thank you for the
global_expression_environmentconfiguration in 25308, reminds me of Lua startup
, but sadly my functions require getEntity().
In build 25208 I have one global expression named "functions" defining all my custom functions and calling them both from other global expressions and rule based expressions, even though you did not intend it to be used like this, it has been working great and all my rules related to electricity use is dependent on these functions.
How would the approach be to define system wide functions with the use ofgetEntity()requirement from now on? -
Help me see your bigger picture. Can you show me some example, and explain why you approached the problem that way?
-
Sure, I will try to explain what's logic in my brain to another brain

For the electricity use example (water heater, car charger, laundry etc) I want to find the cheapest hours to buy electricity and I found myself reusing the same expression code but with different time span and time of day so logically this should be functions.
getPrices(type)function returns the prices (from Hass entity) and creates an array that matches the argument type (day, night or tomorrow) defined in the function e.g day=06-23, night=23-06 and tomorrow=06-23 the next day.getSlots(span, type)calls getPrices() and finds all the available "chunks" or "slots" by slicing it, where the length of the slice is taken from argument span, starting from start of the getPrices() array and iterate over it, returns all "slots" where the last slot doesn't end after the end of the array.cheapestHours(span, type)calls getSlots(), sums all the slots in the getSlots() array and returns the slot that has the lowest sum with start and stop time.
For "night" all functions return the previous night if no prices for upcoming night is available to prevent dashboards and rules to react on an empty or incorrect values.Example of
cheapestHours(3, "night")returns:
{"start":{"time":1762384500000,"value":"00:15"},"stop":{"time":1762395300000,"value":"03:15"}}So for example my water heater rule calculates if it should run day time or night time depending on prices, using
heatTime=cheapestHours()with different span, depending if it has run the previous night or day, and type to compare prices, combined with solar production forecast the next day.
The rule Sets when current time is > heatTime.start.time and Resets when current time > heatTime.stop.time.
heatTime.start.value is the human readable value and is pushed to dashboards and used in notifications.
cheapestHours()can also return a summarized text that is used in my wakeup notifications.Sorry for long post and if none of this makes any sense to you or others

-
I never cease to be amazed at some of the clever angles you all come up with.
Give me a day or two...
-
OK. Build 25310 is up.









