Skip to content

Can Dict key be an integer? #1090

Description

@apella12

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions