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

Can Dict key be an integer? #1090

Closed
apella12 opened this issue Jul 2, 2023 · 2 comments · Fixed by #1152
Closed

Can Dict key be an integer? #1090

apella12 opened this issue Jul 2, 2023 · 2 comments · Fixed by #1152

Comments

@apella12
Copy link

apella12 commented Jul 2, 2023

Background : I'm probably over my head with my limited skills, but as a proof of concept I have been looking at using Zwave-js-ui as a third-party connection to OpenHAB (Home Automation) as an alternative ZWave provider using an MQTT broker. OpenHAB uses jinjava to "unwrap" the MQTT messages from Zwave-js-ui. Most work fine, but one type uses the zwave hexadecimal converted to an integer as a filter and springs an error. It must work in Home Assistant, but that is a javascript application.

Details:

  1. Message: {"value_template":"{{ {22: \"Window/door is open\",23: \"Window/door is closed\",5632: \"Window/door is open in regular position\",5633: \"Window/door is open in tilt position\"}[value_json.value] | default(value_json.value) }}","icon":"mdi:alarm-light","state_topic":"zwave/nodeID_10/113/0/Access_Control/Door_state","json_attributes_topic":"zwave/nodeID_10/113/0/Access_Control/Door_state","device":{"identifiers":["zwavejs2mqtt_0xe2bd1ff1_node10"],"manufacturer":"Zooz","model":"Tilt Shock XS Sensor (ZSE43)","name":"nodeID_10","sw_version":"1.20.1"},"name":"nodeID_10_notification_access_control_door_state","unique_id":"zwavejs2mqtt_0xe2bd1ff1_10-113-0-Access_Control-Door_state"}
  2. Error: Executing the JINJA-transformation failed: An error occurred while transformation. InterpretException: Error resolving expression [{22: "Window/door is open",23: "Window/door is closed",5632: "Window/door is open in regular position",5633: "Window/door is open in tilt position"}[value_json.value] | default(value_json.value)]: TemplateStateException: Dict key must be a string or identifier, was: 22
  3. Section of OpenHAB code that calls jinjava:
   /**
     * Transforms the input <code>value</code> by Jinja template.
     *
     * @param template Jinja template
     * @param value String may contain JSON
     * @throws TransformationException
     */
    @Override
    public @Nullable String transform(String template, String value) throws TransformationException {
        String transformationResult;
        Map<String, @Nullable Object> bindings = new HashMap<>();

        logger.debug("about to transform '{}' by the function '{}'", value, template);

        bindings.put("value", value);

        try {
            JsonNode tree = new ObjectMapper().readTree(value);
            bindings.put("value_json", toObject(tree));
        } catch (IOException e) {
            // ok, then value_json is null...
        }

        try {
            transformationResult = jinjava.render(template, bindings);
        } catch (FatalTemplateErrorsException e) {
            throw new TransformationException("An error occurred while transformation. " + e.getMessage(), e);
        }

        logger.debug("transformation resulted in '{}'", transformationResult);

        return transformationResult;
    }

  1. I did capture value, tree and bindings during the transformation.
    value: {"time":1688240438204,"value":22,"nodeName":"","nodeLocation":""}
    tree: '{"time":1688240438204,"value":22,"nodeName":"","nodeLocation":""}
    bindings: {value_json={nodeName=, time=1688240438204, value=22, nodeLocation=}, value={"time":1688240438204,"value":22,"nodeName":"","nodeLocation":""}}

So the question as above; Can an integer be allowed as a key or identifier?

@asutalo
Copy link

asutalo commented Jul 24, 2023

something like this #934 ?

@apella12
Copy link
Author

@asutalo - That does look like a fix. Thanks for pointing it out. I'll keep an eye on it.

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

Successfully merging a pull request may close this issue.

2 participants