I’ve just tested it, indeed you can have methods inside UI rules.
Here’s another version of your script:
# convert a value from_range to to_range
def convert_scale(value, from:, to:)
(value.to_f - from.begin) / (from.end - from.begin) * (to.end - to.begin) + to.begin
end
logger.info("Outside light level: #{OutsideLuxNum}")
dimmer = convert_scale(OutsideLuxNum, from: 900..10_000, to: 100..10).clamp(10, 100)
if CurrentDimmer.ensure.command(dimmer) # Item.ensure.command returns nil when it already had the same value
gDimmerLux.ensure << dimmer
logger.info("Dimmer set to: #{dimmer}")
else
logger.info('Dimmer unchanged')
end
It’s probably up to @broconne - but if you have a specific rule for a specific functionality, it might be a good idea to post a new topic.