- Platform information:
- openHAB version: 4.1.0
I’m trying to set up a rule that would average the room temperature drawn from a few items and write it back to another device that speaks modbus. Here’s the item for the modbus item that I’m writing to:
Number:Temperature RAT_Manual_Override "RAT Manual Override" (Recuperator) ["Control"] {
unit = "°C",
channel = "modbus:data:recuperator:manual_override:rat_value:number"[ profile="modbus:gainOffset", gain="0.1°C" ]
}
NB: for now note that the °C
unit in gain
is required. Otherwise when the modbus binding reads out the register it fails to update the Item state with warnings along the line of
19:21:00.601 [WARN ] [openhab.core.library.items.NumberItem] - Failed to update item 'RAT_Manual_Override' because '21.7' could not be converted to the item unit '°C'
Then I wrote the following rule to achieve my end goal:
rule "Maintain Room Temperature value"
when
Item Temperature_BossRoom changed or
Item Temperature_GuestRoom changed or
Item Temperature_KidsRoom changed or
Item Temperature_LivingRoom changed
then
val temps = newArrayList(
Temperature_BossRoom,
Temperature_GuestRoom,
Temperature_KidsRoom,
Temperature_LivingRoom
);
var QuantityType<KELVIN> sum;
for (i: temps) {
val QuantityType<KELVIN> v = (i.state as QuantityType<KELVIN>).toUnit("K");
sum += v;
}
val avg = sum / temps.size();
RAT_Manual_Override.sendCommand(avg.toUnit(RAT_Manual_Override.getUnit))
RAT_Manual_Override.sendCommand(REFRESH)
end
Quick summary: It grabs temperature from 4 sensors in kelvin, computes the average and then calls sendCommand
with the average converted back to Celsius.
Except it does not work, this time with the following error:
19:21:21.900 [WARN ] [rnal.profiles.ModbusGainOffsetProfile] - Cannot apply gain ('0.1 °C') and pre-gain-offset ('0') to state ('21.796875') (formula '21.796875'/'0.1 °C' - '0') because types do not match (towardsItem=false): Cannot process command '21.796875', unit should compatible with gain
I also tried changing the code to sendCommand in one of these other ways among others:
RAT_Manual_Override.sendCommand(avg.toUnit("°C")) // same
RAT_Manual_Override.sendCommand(avg.toUnit("°C^2")) // idea: unit="°C" only strips one of the celcius, leaving the second one for gainOffset ==> no updates at all, I imagine the conversion probably fails and returns a null?
RAT_Manual_Override.sendCommand(21.7) // no work
From my experiments it appears like the unit = "°C"
in the item definition is stripping the unit off before it gets passed along to the modbus:gainOffset
mechanism. However gainOffset also expects the unit to be present still.
So far the only thing that has worked at all is to remove all units altogether, but that’s not a solution I am keen on. Is there a way to get this to work while keeping the units?