Deciphering JSON with spaces in keys

I am receiving an MQTT message with like this

/energenie/eTRV/Report/Diagnostics/296 {
        "Motor current below expectation":      false,
        "Motor current always high":    false,
        "Motor taking too long":        false,
        "discrepancy between air and pipe sensors":     false,
        "air sensor out of expected range":     false,
        "pipe sensor out of expected range":    false,
        "low power mode is enabled":    true,
        "no target temperature has been set by host":   false,
        "valve may be sticking":        false,
        "valve exercise was successful":        false,
        "valve exercise was unsuccessful":      false,
        "driver micro has suffered a watchdog reset and needs data refresh":    false,
        "driver micro has suffered a noise reset and needs data refresh":       false,
        "battery voltage has fallen below 2p2V and valve has been opened":      false,
        "request for heat messaging is enabled":        false,
        "request for heat":     false
}

The spaces in the keys are giving hass some problems decoding. I have tried

      - platform: mqtt
        name: "Office TRV Motor current below expectation"
        state_topic: "/energenie/eTRV/Report/Diagnostics/296"
        value_template: '{{value_json.Motor current below expectation}}'
        payload_on: true
        payload_off: false

But this gives an error

ERROR:homeassistant.bootstrap:The following platforms contain invalid configuration:  binary_sensor.mqtt  (please check your configuration)invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'current') for dictionary value @ data['value_template']. Got '{{value_json.Motor current below expectation}}'. Please check the docs at https://home-assistant.io/components/binary_sensor.mqtt/

I have tried various combination of quotes, and replacing the spaces with \u0032, but havenā€™t had any success.

Does anyone know how to do this?

Try to replace the spaces with underscores and see if that works, i.e.

value_template: '{{value_json.Motor_current_below_expectation}}'

Sebastian

1 Like

This only matches keys with undebars, unfortunately.

Hm, replacing the spaces (or other special characters) with underscores is the solution for the rest of the configuration, i.e a scene name like ā€œscene 1: my favouriteā€ would become scene.scene_1_my_favourite.
Strange that this does not work in your case.

Sebastian

I think it can replace underscores in internal data. but a name with underscores is valid in a JSON string.

So it couldnā€™t replace the underscores in case I did actually want to match them.

You donā€™t have to replace the underscores.
Again, unfortunately I donā€™t know the solution for your example, but - to stay with my scene name example - if you call it e.g. ā€œname: scene 1: this_is_my favouriteā€ (which is a valid name) - it would translate to scene.scene_1_this_is_my_favourite - so ā€œunderscoreā€ translates to ā€œunderscore, space or colonā€ (Iā€™m pretty sure thatā€™s not a complete list).
I bet it would be interesting to look at the parser code :wink:

Sebastian

I never managed to get this done with yaml, but with apps, the python json parser manages it easily

import json

    def new_diagnostics(self, entity, attribute, old, new, kwargs):
        obj = json.loads(new)

        if obj["Lounge TRV Motor current below expectation"]:
            self.log("Turning on {}".format(self.mc_below))
            self.set_state(self.mc_below, state = "on")
        else:
            self.log("Turning off {}".format(self.mc_below))
            self.set_state(self.mc_below, state = "off")