Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[transform.map] Setting a Switch item value to NULL, shall look for -= in a transform/.map file, not for NULL=. #13777

Open
dilyanpalauzov opened this issue Nov 23, 2022 · 12 comments

Comments

@dilyanpalauzov
Copy link
Contributor

dilyanpalauzov commented Nov 23, 2022

I have a switch in an .items file:

Switch r2 "2 Реле [MAP(switch.map):%s]" <kitchen> {channel="mqtt:topic:r2:r2"}

The file transforms/switch.map contains:

ON=Включено
OFF=Изключено
-=-

If the -=- is missing, there is an exception:

2022-11-23 17:29:07.415 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - transformation throws exception [transformation=org.openhab.transform.map.internal.MapTransformationService@7890fe, value=-]                                                                           
org.openhab.core.transform.TransformationException: Target value not found in map for '-'                                               
        at org.openhab.transform.map.internal.MapTransformationService.transform(MapTransformationService.java:93) ~[?:?] 
        at org.openhab.core.ui.internal.items.ItemUIRegistryImpl.transform(ItemUIRegistryImpl.java:551) ~[?:?]
        at org.openhab.core.ui.internal.items.ItemUIRegistryImpl.getLabel(ItemUIRegistryImpl.java:459) ~[?:?]      
        at org.openhab.core.io.rest.sitemap.internal.PageChangeListener.constructSitemapEventForWidget(PageChangeListener.java:236) ~[?:?]                                                                                                                                      
        at org.openhab.core.io.rest.sitemap.internal.PageChangeListener.constructSitemapEvents(PageChangeListener.java:225) ~[?:?]
        at org.openhab.core.io.rest.sitemap.internal.PageChangeListener.constructAndSendEvents(PageChangeListener.java:172) ~[?:?]      
        at org.openhab.core.io.rest.sitemap.internal.PageChangeListener.stateChanged(PageChangeListener.java:186) ~[?:?]                
        at org.openhab.core.items.GenericItem.lambda$1(GenericItem.java:259) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]                             
        at java.lang.Thread.run(Thread.java:829) [?:?]                                                                                  

In a .sitemap file I have

Switch item=r2 

My understanding is that a Switch can have only three states: ON, OFF and UNDEFINED⇔NULL. When the value was never set, Main UI shows at http://…:8080/settings/items/ NULL for the UNDEFINED value.

Once the value for the switch is set and the device is disconnected, in openhab-JS and I call one of these two options:

let {NULL} = require("@runtime")

items.getItem("name_it").postUpdate('NULL')
items.getItem("name_it").postUpdate(NULL)

the value NULL is searched in transforms/switch.map. But switch.map has no NULL=.

The point is, if the .items file contains no [MAP(switch.map):%s], but sitemap has Switch item=r2 label="2 Реле [MAP(switch.map):%s]", then transform/switch.map does not need to contain NULL= line.

The problem report is that after sending items.getItem("name_it").postUpdate("NULL") to an item with label "2 Реле [MAP(switch.map):%s]", the lookup value in switch.map should be -=, but it is NULL=.

Currently it is unclear for which state -= stands for. It is neither for NULL, nor for UNDEFINED.

I use openHAB 3.3.

@J-N-K J-N-K transferred this issue from openhab/openhab-core Nov 25, 2022
@J-N-K
Copy link
Member

J-N-K commented Nov 25, 2022

This is an issue of the MAP transformation, moved to openhab-addons.

@lolodomo
Copy link
Contributor

lolodomo commented Nov 29, 2022

Is the file switch.map your file?
Because I agree that the entry in the map file should be NULL.
UNDEF is also a possible item state.

@dilyanpalauzov
Copy link
Contributor Author

dilyanpalauzov commented Nov 29, 2022

Is the file switch.map your file?

Yes, I have it under /etc/openhab/transforms.

@lolodomo
Copy link
Contributor

So why did you set this mapping rather than with NULL?

@dilyanpalauzov
Copy link
Contributor Author

The system throws the aforementioned exception if -=- is missing.

@lolodomo lolodomo changed the title Setting a Switch item value to NULL, shall look for -= in a transform/.map file, not for NULL=. [transform.map] Setting a Switch item value to NULL, shall look for -= in a transform/.map file, not for NULL=. Dec 12, 2022
@lolodomo
Copy link
Contributor

lolodomo commented May 4, 2024

"-" is what sitemap REST API map to when a state is NULL or UNDEF.
It should be the same whether the MAP is in item or sitemap. So no need normally to have NULL and UNDEF entries in the map file.

I am going to confirm that by testing again.

@lolodomo
Copy link
Contributor

lolodomo commented May 4, 2024

My items:

Switch TestSwitch1 "Switch1 [MAP(en.map):%s]"
Switch TestSwitch2

The part of my sitemap:

    Frame label="MAP & NULL state" {
        Default item=TestSwitch1
        Default item=TestSwitch2 label="Switch2 [MAP(en.map):%s]"
    }

en.map contains a map for "-" but none for NULL or UNDEF:

CLOSED=closed
OPEN=open
ON=< On >
OFF=< Off >
__ON__=<<< On >>>
__OFF__=<<< Off >>>
undefined=unknown
-=NULL or UNDEF

When I open only Basic UI, I have no warning logged and the result is as expected:
image

Now, as soon as I open Main UI, I got this warning:

10:28:31.031 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state 'NULL' on item 'TestSwitch1' with pattern 'MAP(en.map):%s': Target value not found in map for 'NULL'

The warning is repeated when opening the list of items or when opening the page of this item.

Main UI is calling a REST API that calls the transformation with the state "NULL".

As you can notice, the way to call transformation is done differently by Main UI and sitemap UI, sitemap UI is first converting NULL/UNDEF states into "-" before calling the transformation, Main UI does not.

As a workaround until we find a proper fix, to avoid warnings whatever the used UI, the solution for MAP transformation is to add entries for "NULL" and "UNDEF" in addition to "-". For my tests, I used:

-=NULL or UNDEF
NULL=This is NULL
UNDEF=This is UNDEF

@J-N-K : please move this issue in core.

@lolodomo
Copy link
Contributor

lolodomo commented May 4, 2024

The "problem" is in the method considerTransformation in class EnrichedItemDTOMapper (I am already updating it in my work in progress PR openhab/openhab-core#4203 )
But something has to be decided what to do when the state is NULL/UNDEF, should we call the transformation or not ?
The behaviour is not fully consistent when the item state is of type UndefType:

  1. Method considerTransformation in class EnrichedItemDTOMapper is calling the transformation with "NULL" or "UNDEF"
  2. Method getDisplayState in class SseItemStatesEventBuilder is not calling the transformation
  3. Method getLabel in class ItemUIRegistryImpl is calling the transformation with "-"

@lolodomo
Copy link
Contributor

lolodomo commented May 4, 2024

I believe:

  1. The transformation should be called, it allows a user to replace "NULL" or "UNDEF" by any label he prefers using a MAP transformation. So getDisplayState in class SseItemStatesEventBuilder has to be changed in my opinion.
  2. Do we replace "NULL" and "UNDEF" by "-" before calling the transformation or not ? Not doing the conversion to "-" let a chance to the user to define a different behaviour for NULL and UNDEF in the transformation. I would be in favour of this solution even if it means users will have to change their transformations if they use sitemap UIs.

WDYT ?

@lolodomo
Copy link
Contributor

lolodomo commented May 4, 2024

@openhab/core-maintainers for feedback on my analysis and my proposal.

@lolodomo
Copy link
Contributor

lolodomo commented May 4, 2024

lolodomo added a commit to lolodomo/openhab-core that referenced this issue May 6, 2024
Use item state formatter to format input of transformation, meaning using state.format(format) instead of String.format(format, state.toString())
This was already the case in sitemap API but not in other APIs used by Main UI.

Make sure to call transformation even for NULL and UNDEF states.
It was not the case in one API used by Main UI.

When calling transformation and state is NULL or UNDEF, do not apply format to the input value and do not replace by "-".
That leans the transformation will be called with "NULL" or "UNDEF".
Sitemap API was calling the transformation using a pattern containing "-".

Fix openhab#4101
Also related to discussion in openhab/openhab-addons#13777

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
lolodomo added a commit to lolodomo/openhab-core that referenced this issue May 6, 2024
Use item state formatter to format input of transformation, meaning using state.format(format) instead of String.format(format, state.toString())
This was already the case in sitemap API but not in other APIs used by Main UI.

Make sure to call transformation even for NULL and UNDEF states.
It was not the case in one API used by Main UI.

When calling transformation and state is NULL or UNDEF, do not apply format to the input value and do not replace by "-".
That means the transformation will be called with "NULL" or "UNDEF".
Sitemap API was calling the transformation using a pattern containing "-".

Fix openhab#4101
Also related to discussion in openhab/openhab-addons#13777

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
lolodomo added a commit to lolodomo/openhab-core that referenced this issue May 6, 2024
Use item state formatter to format input of transformation, meaning using state.format(format) instead of String.format(format, state.toString())
This was already the case in sitemap API but not in other APIs used by Main UI.

Make sure to call transformation even for NULL and UNDEF states.
It was not the case in one API used by Main UI.

When calling transformation and state is NULL or UNDEF, do not apply format to the input value and do not replace by "-".
That means the transformation will be called with "NULL" or "UNDEF".
Sitemap API was calling the transformation using a pattern containing "-".

Fix openhab#4101
Also related to discussion in openhab/openhab-addons#13777

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
@lolodomo
Copy link
Contributor

lolodomo commented May 6, 2024

Proposed changes are now implemented in PR openhab/openhab-core#4203

lolodomo added a commit to lolodomo/openhab-core that referenced this issue May 7, 2024
Use item state formatter to format input of transformation, meaning using state.format(format) instead of String.format(format, state.toString())
This was already the case in sitemap API but not in other APIs used by Main UI.

Make sure to call transformation even for NULL and UNDEF states.
It was not the case in one API used by Main UI.

When calling transformation and state is NULL or UNDEF, do not apply format to the input value and do not replace by "-".
That means the transformation will be called with "NULL" or "UNDEF".
Sitemap API was calling the transformation using a pattern containing "-".

Fix openhab#4101
Also related to discussion in openhab/openhab-addons#13777

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants