I am setting up a control for fan coil units (FCU’s). My fundamental problem is setting up a GUI that displays and controls in either C or F (display works but control is not working).
Running Openhab 5.0.1
The control is via modbus. I can successfully read and write registers no problem.
All modbus registers are in Celsius - I have setup openhab to successfully read the setpoints and actual values in celsius.
All Openhab temperature variables are stored in C and displayed in C or F depending on user preference (using state descriptions).
For control of the setpoints, I would like to use the slider widget. The display of the slider widget works because of the state description. However, when I change the slider, it always tries to update the underlying item to the display value -
Example #1 (correct): User preference to display in C
Display of current 24C setpoint shows correctly as 24C,
User changes slider to new setpoint of 25C
New setpoint of 25C is sent to the item
new setpoint of 25C is sent to modbus
Works perfectly.
Example #2 (incorrect): User preference to display in F
Display of current 24C setpoint shows correctly as 75.2F,
User changes slider to new setpoint of 77F
new setpoint of 77 is sent to the item, erroneously interpreted as 77C
New setpoint of 77C is sent to modbus.
FCU goes haywire
Edit - an odd observation in both example #1 and #2: the log file correctly has the setpoint changing from 24C to 25C regardless of the display units (which is the correct behavior). Somehow, the thing doing the modbus ‘write’ is using an intermediate value rather than the underlying item value.
Options that I know of:
I could use a temporary variable but I have many temperatures and that is quite a bit of work if not necessary.
(preferred) I have also tried to modify the value before updating the item (“actionCommand: =(items[props.unitSwitch].state === ‘ON’ ? Math.round((Number(vars.value) - 32) * 5 / 9) : vars.value)”).
I’m not sure this use case is supportable with the slider as it’s implemented today. After the command is sent to the Item after moving the slider, the Item always gets updated to °C since that’s the unit set for the Item. Then the slider widget gets the update event for the Item an it’s in °C. But the UI doesn’t know how to convert it to °F (that’s all done on the server) and the widgets update themselves from the event, so the event is processed without having gone through the State Description transformation.
But in your options your option 2 seems to imply that you will always want °C or °F and not switch between the two or have both in the system at the same time. You can achieve that with minimal effort.
First a primer on how units work.
The Item’s state will always be in a set unit.
The unit’s Item is first determined by the Item’s unit metadata. If that’s not defined the default unit taken from the regional settings is used. The default for SI units is °C and the default for Imperial is °F.
When a state update without units is posted to an Item with units, the Item’s unit is assumed. For example, if yo post 25 to an Item with unit °F, the Item will update to 25 °F.
When a state update with units different from the Item’s units is posted to an Item with units, the value is automatically converted to the Item’s units. Posting 25 °C to an Item with unit °F will result in the Item updating to 77 °F.
When a state update with units is posted to an Item without units, the units are simply dropped.
Given this is how unit’s work, one option will be to make sure that what ever is posting updates to these Number:Temperature Items is using units in that update. For modbus, you can do this with the gainOffset profile. Other bindings should do this automatically or provide an option to add a unit to the received value before posting it to the Item.
In a pinch you can do something like JS: | input + ' °C' to append units to the state update and commands.
This way, if the regional settings are correct for the user (i.e. they are using Imperial) and/or the unit metadata is set on these Items, modbus will publish °C but it will always be converted to °F before the update gets to the Item. There’s no need for a conversion at the state description. The slider widget will work because it’s always working in °F. The gainOffset profile will convert back to °C if needed. And a problem you may not have encountered yet, all the charts with be in °F too instead of in °C.