openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.87k stars 3.58k forks source link

[knx] Console commands do not use UoM #15065

Closed holgerfriedrich closed 1 year ago

holgerfriedrich commented 1 year ago

Looking at #15049 there seems to be an issue sending data with UoM from the openHAB console to a KNX installation.

We have defined in item with DPTs 7.007 (time in hours) with textual config. Here are the snippets from knx.things and default.items:

Type number: t_t_h "t std" [ ga="7.007:10/0/7" ]

Number:Time time_h "T [%.1f h]" {channel="knx:device:ip:test:t_t_h"}

Receiving data properly respects UoM and converts data to seconds, which is the representation of time on OH.

Using the OH console to send data, however:

openhab> openhab:send time_h "7 s"
Command has been sent successfully.
openhab> openhab:items list time_h
time_h (Type=NumberItem, State=7 s, Label=T, Category=null)
openhab> openhab:send time_h "2 h"
Command has been sent successfully.
openhab> openhab:items list time_h
time_h (Type=NumberItem, State=7200 s, Label=T, Category=null)
openhab> openhab:send time_h "5 kg"
Command has been sent successfully.
openhab> openhab:items list time_h
time_h (Type=NumberItem, State=7200 s, Label=T, Category=null)
openhab> openhab:send time_h 1
Command has been sent successfully.
openhab> openhab:items list time_h
time_h (Type=NumberItem, State=1 s, Label=T, Category=null)

The internal state is properly updated for h and s; the wrong unit "kg" is ignored. The last command without unit uses default for time (s).

But the knx addon sends out the data in all 4 cases (even the one with wrong unit). It used the plain number without any conversion. See the bus log:

A_Group.write 00 07
A_Group.write 00 02
A_Group.write 00 05
A_Group.write 00 01

Expected Behavior

Data converted according to UoM, data on the bus is in hours.

Current Behavior

Possible Solution

Steps to Reproduce (for Bugs)

see above

Context

UoM

Your Environment

holgerfriedrich commented 1 year ago

@J-N-K can you comment how the UoM handling should work? The addon seems to get the data as plain number from the framework - handleCommand always sees the number, not the unit. Or do I overlook something?

openhab> openhab:send time_h "4 s"
21:49:40.426 [TRACE] [knx.internal.client.AbstractKNXClient] - Received a Group Write telegram from '1.1.161' to '5/3/10' with value '[0]'
21:49:42.471 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'time_h' received command 4 s
21:49:42.472 [INFO ] [openhab.event.ItemStatePredictedEvent] - Item 'time_h' predicted to become 4 s
21:49:42.491 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'time_h' changed from NULL to 4 s
21:49:42.491 [TRACE] [x.internal.handler.DeviceThingHandler] - Handling command '4' for channel 'knx:device:ip:hsb_test:t_t_h'
21:49:42.492 [TRACE] [nding.knx.internal.channel.KNXChannel] - getCommandSpec checking keys '[ga]' for command '4' (class org.openhab.core.library.types.DecimalType)
21:49:42.494 [TRACE] [nding.knx.internal.channel.KNXChannel] - getCommandSpec key 'ga' has expectedTypeClass '[class org.openhab.core.library.types.QuantityType, class org.openhab.core.library.types.DecimalType]', matching command '4' and dpt '7.007'
21:49:42.495 [TRACE] [knx.internal.client.AbstractKNXClient] - writeToKNX groupAddress '10/0/7', commandSpec 'org.openhab.binding.knx.internal.channel.WriteSpecImpl@199986e'
21:49:42.496 [TRACE] [knx.internal.client.AbstractKNXClient] - sendToKNX mappedValue: '4' groupAddress: '10/0/7'
21:49:42.522 [DEBUG] [knx.internal.client.AbstractKNXClient] - Wrote value '4' to datapoint 'command DP 10/0/7 'knx:ip:ip', DPT 7.007, low priority' (0. attempt).
21:49:42.500 1.0.248->10/0/7 A_Group.write 00 04

When I use "4 h" as command, to framework handles the data correctly, but the addon still gets a plain 4 - same as above.

22:04:15.464 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'time_h' received command 4 h
22:04:15.465 [INFO ] [openhab.event.ItemStatePredictedEvent] - Item 'time_h' predicted to become 4 h
22:04:15.483 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'time_h' changed from 4 s to 14400 s
22:04:15.484 [TRACE] [x.internal.handler.DeviceThingHandler] - Handling command '4' for channel 'knx:device:ip:hsb_test:t_t_h'

Do I miss something?

J-N-K commented 1 year ago

Core has more strict type checking. As a result the KNX channel (which reports to be Number which is a plain number without unit) only receives a DecimalType (unit stripped).

I'll have look how this can be handled. It probably also affects HTTP and MQTT which use a similar concept.