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

[jinja] Empty string result when binding is missing #10496

Closed
antroids opened this issue Apr 9, 2021 · 5 comments · Fixed by #10581
Closed

[jinja] Empty string result when binding is missing #10496

antroids opened this issue Apr 9, 2021 · 5 comments · Fixed by #10581
Labels
bug An unexpected problem or unintended behavior of an add-on

Comments

@antroids
Copy link
Contributor

antroids commented Apr 9, 2021

I'm creating an implementation of climate platform for mqtt.homeassistant binding, but found a side issue.
Variable used in state_template can be missing in the state, but Jinja returns the empty string, that considered as valid value.

Expected Behavior

Thing state should not be updated when variable from state_template is missing in the state.

Current Behavior

20:26:16.995 [WARN ] [hab.binding.mqtt.generic.ChannelState] - Command '' not supported by type 'OnOffValue': No enum constant org.openhab.core.library.types.OnOffType.`

Payload:
'{"auto_lock":"AUTO","away_mode":"OFF","away_preset_days":1,"away_preset_temperature":15,"battery_low":false,"boost_time":300,"child_lock":"UNLOCK","comfort_temperature":20,"current_heating_setpoint":20,"eco_temperature":15,"force":"normal","holidays":[{"hour":6,"minute":0,"temperature":20},{"hour":8,"minute":0,"temperature":15},{"hour":11,"minute":30,"temperature":15},{"hour":12,"minute":30,"temperature":15},{"hour":17,"minute":30,"temperature":20},{"hour":22,"minute":0,"temperature":15}],"linkquality":70,"local_temperature":26,"local_temperature_calibration":-1,"max_temperature":35,"min_temperature":5,"position":0,"preset":"schedule","system_mode":"auto","update":{"state":"idle"},"update_available":false,"week":"5+2","window_detection":"OFF","window_detection_params":{"minutes":10,"temperature":5},"workdays":[{"hour":6,"minute":0,"temperature":20},{"hour":8,"minute":0,"temperature":15},{"hour":11,"minute":30,"temperature":15},{"hour":12,"minute":30,"temperature":15},{"hour":17,"minute":30,"temperature":20},{"hour":22,"minute":0,"temperature":15}]}'

Template:
{{ value_json.valve_detection }}

Possible Solution

Try this config option: com.hubspot.jinjava.JinjavaConfig#isFailOnUnknownTokens

Steps to Reproduce (for Bugs)

  1. Add the template to OnOff MQTT channel with binding, that missing in state.
  2. Publish MQTT message.

Your Environment

  • Version used: openHAB 3.0.1
@antroids antroids added the bug An unexpected problem or unintended behavior of an add-on label Apr 9, 2021
@antroids
Copy link
Contributor Author

Also reported to zigbee2mqtt project issue with missing properties in state payload.

@Rossko57
Copy link
Contributor

Rossko57 commented Apr 11, 2021

When user-defined transformations are involved, common practice is to chain a REGEX that looks for presence of property to another transformation e.g. JSONPATH extracting property. If the REGEX fails, the JSONPATH is not run and so missing field WARNS and empty updates avoided.

@antroids
Copy link
Contributor Author

Sounds like quick fix. These transformations are defined by mqtt topic with Home Assistant configuration, they can be little complexity. Not sure that regexp can be generated to cover jinja syntax. For example:
{"action_template":"{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}","action_topic":"zigbee2mqtt/th1"}

@antroids
Copy link
Contributor Author

antroids commented Apr 13, 2021

I've checked the option com.hubspot.jinjava.JinjavaConfig#isFailOnUnknownTokens.
In Jinjava 2.5.0 it works only for root variables, and ignores map structures and java beans.
To support Jinja transformation in correct way, Jinjava must be upgraded to last version.

Jinjava 2.5.0 just ignores missing map keys, so we don't know whether state is invalid or empty:

  • binding: ["value_json": "val"], pattern: "{{ value_json.val }}", result: "val"
  • binding: ["value_json": "val"], pattern: "{{ value_json.asd }}", result: ""
  • binding: ["value_json": ["val":""]], pattern: "{{ value_json.val }}", result: ""
  • binding: ["value_json": "val"], pattern: "{{ value_json.val is defined }}", result: true
  • binding: ["value_json": "val"], pattern: "{{ value_json.asd is defined }}", result: false

Jinvava 2.5.7 works well:

  • binding: ["value_json": "val"], pattern: "{{ value_json.val }}", result: "val"
  • binding: ["value_json": "val"], pattern: "{{ value_json.asd }}", result: UnknownTokenException
  • binding: ["value_json": ["val":""]], pattern: "{{ value_json.val }}", result: ""
  • binding: ["value_json": "val"], pattern: "{{ value_json.val is defined }}", result: true
  • binding: ["value_json": "val"], pattern: "{{ value_json.asd is defined }}", result: false

Looks like dependency version cannot be updated just in addon's pom, and I need to upload it to the OpenHAB Repository first? How can I do this?

@openhab-bot
Copy link
Collaborator

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/update-version-of-osgified-dependency/120768/1

kaikreuzer pushed a commit that referenced this issue Apr 25, 2021
Signed-off-by: Anton Kharuzhy <antroids@gmail.com>
@wborn wborn linked a pull request Apr 26, 2021 that will close this issue
themillhousegroup pushed a commit to themillhousegroup/openhab2-addons that referenced this issue May 10, 2021
…penhab#10578)

Signed-off-by: Anton Kharuzhy <antroids@gmail.com>
Signed-off-by: John Marshall <john.marshall.au@gmail.com>
computergeek1507 pushed a commit to computergeek1507/openhab-addons that referenced this issue Jul 13, 2021
frederictobiasc pushed a commit to frederictobiasc/openhab-addons that referenced this issue Oct 26, 2021
thinkingstone pushed a commit to thinkingstone/openhab-addons that referenced this issue Nov 7, 2021
marcfischerboschio pushed a commit to bosch-io/openhab-addons that referenced this issue May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants