Setting a variable based on multi-device state

(@moderators -- I'm guessing that this will need to be done via Rule Machine, but feel to move it to a different category)

I'd like to have an variable that holds a value representing the ambient light in my house, with inputs from multiple sensors in various rooms.

However, the the Lux value for each room should be excluded from the average (to be stored in the variable) if there are lights on in the room.

Since the sensors are from different manufacturers, some scaling will need to be done (ie., multiple sensor "A" by 2, diving sensor "B" by 1.5, etc).

Any suggestions for how to implement the following pseudo-code in Hubitat:

deviceCount=0
deviceTable=["Living Room","Dining Room", "Den"]
lampsHash{"Living Room"}=["LR overhead", "LR table lamp"]
lampsHash{"Dining Room"}=["DR overhead","DR wall sconces"]
lampsHash{"Den"}=["Den overhead"]
adjustmentHash{"Living Room"}=1.0
adjustmentHash{"Dining Room"}=2
adjustmentHash{"Den"}=.25

foreach device in deviceTable
{
	lampsOn=0
	foreach lamp in lampsHash[device]
	{
		if ( GetDeviceLevel(lamp) > 0 )
		{
			lampsOn++
		}
	}
	if ( lampsOn == 0 )
	{
		Lux=Lux+getDeviceLux(device) * adjustmentHash(device)
		deviceCount++
	}
}

AverageLux=Lux / deviceCount

You could probably do it in Rule Machine, it will be pretty complicated, and will possibly cause the rule to run multiple times concurrently.

I dont think there is any way to do it as you have written in RM, maybe in Webcore?

You would need to setup a trigger for every device, when lux changes
You could then pull the value from every device into a variable.
Then I think you would need to go through each one, one at a time and setup the multiplier and save back to same variable.
The device count would probably just need to be hard coded (or a manually set variable)

That doesn't even take the lamps into account.

Best thing would be to have a custom app that does it and stores the value in a virtual light sensor.

I agree with @jtp10181 that a custom app is probably the way to go. With that said, outside of the adjustment request, you could use HubitatPublic/example-apps/averageIlluminance.groovy at master · hubitat/HubitatPublic · GitHub from the Public Repo to get some of this. Setup as many instances as you need to cover all possible options, and then, have one massive rule for the variable with If-Then and a bunch of Else-Ifs to cover all instances you created. I would be very concern with the rule running concurrently.

In writing all of this out, a custom app definitely seems like a better approach, but the above might get you close.

Hmmm.

Re. using rule machine... given the complexity and number of devices (both greater than the pseudo code), I was never considering triggering the rule via Lux or lamp level changes; I'd have it run every minute (ie., as a cron job). Eliminates the possibility of concurrent execution and that's low enough latency to populate the averageLux variable for other rules to query.

Yeah good point, having it run on a schedule would be a far better approach and might make it more feasible to do with RM. Still going to be a very long and complicated rule.