Unable to add mqtt sensor via mqtt API Niko Home Control

Hi all, I am struggling with a mqtt sensor. Completely new to Home Assistant and MQTT.

  1. I run Home Assistant as docker container
  2. I have an Niko Home Control home domotics system. It has an MQTT API available. I use a power clamp to monitor power usage. Link: API for technology lovers and Hobby API en Niko Home Control - Gebruikershandleiding NHC II - Niko Guide
  3. Goal is to use the MQTT power events in a dashboard using a MQTT sensor. Format of the JSON: #{“Params”:[{“Devices”:[{“Uuid”:“cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e”,“Properties”:[{“ElectricalPower”:“76.000”}]}]}],“Method”:“devices.status”}

However I get following error message:

Invalid config for [mqtt]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'd7') for dictionary value @ data['mqtt']['sensor'][0]['json_attributes_template']. Got '{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}'
invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'd7') for dictionary value @ data['mqtt']['sensor'][0]['value_template']. Got '{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}'. (See /config/configuration.yaml, line 12).

Any ideas or anyone has experience on this to help me troubleshoot?

Thanks in advance!

Following I currently have defined in the configuration.yaml

mqtt:
  broker: 192.168.3.10
  port: 8884
  username: hobby
  password: <deleted>
  certificate: ./certs/ca-chain.cert.pem

mqtt: 
  sensor:
    - name: "cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e"
      state_topic: "hobby/control/devices/evt"
      unit_of_measurement: "W"
      value_template: "{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}"
      json_attributes_topic: "hobby/control/devices/evt"
      json_attributes_template: "{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}"

Probably because you have two instances of the mqtt: key word in configuration.yaml where the first instance configures Home Assistant’s connection to your MQTT broker but that’s now done via the UI.

MQTT - Home Assistant (home-assistant.io)

Correct, but in the UI I can’t use the certificate parameter which is needed to connect to the MQTT broker of the Niko Home Control system. Hence adding it directly in the configuration.yaml. Or am I missing something?

Hmm, interesting problem. The UI-based configuration doesn’t support specifying a certificate so it explains you must add it to configuration.yaml under the mqtt: key.

MQTT Certificate - Home Assistant (home-assistant.io)

However, the mqtt: key is now used to configure MQTT Sensors, Binary_Sensors, Switches, etc.

I don’t have any experience with this combination so I can’t offer a definitive solution. I would suggest trying to combine all options under a single mqtt: key but I doubt that would fix it.

Try that and if it fails to work then use MQTT Discovery instead of manually configuring the MQTT Sensor in configuration.yaml. This technique involves publishing the sensor’s configuration (as a retained message) to a special discovery topic. The configuration must be in JSON format and conform to certain rules. Home Assistant receives the discovery payload and uses it to create the sensor.

FWIW, I use MQTT Discovery to create all of my MQTT-based entities (none are configured via configuration.yaml).

Thanks, will check MQTT discovery.

So I got the connection working by splitting it up via UI and YAML.

  1. Configure YAML file with following settings:
mqtt:
  client_id: home-assistant
  certificate: ca-chain.cert.pem 
  tls_insecure: true
  1. configure the in the UI the broker IP, port, username and password

Listining to the topic (of which I want to extract the ElectricalPower data) works.

Listen to a topic
 
Topic to subscribe to: hobby/control/devices/evt
  
Message 1 received on hobby/control/devices/evt at 23:12:
{
    "Params": [
        {
            "Devices": [
                {
                    "Uuid": "413eac54-a0dd-4e6a-8629-05e1fb521131",
                    "Properties": [
                        {
                            "ElectricalPower": "86.000"
                        }
                    ]
                }
            ]
        }
    ],
    "Method": "devices.status"
}
QoS: 0 - Retain: false

MQTT discovery seems not to be working, I am assuming because I use a container.

Defining the sensor in configuration.yaml still gives the same error. I am unable to get the syntax working. It seems the string is too long if I read the error message correctly?

Anyone a clue?

Sensor definiton

mqtt: 
  sensor:
    - name: "cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e"
      state_topic: "hobby/control/devices/evt"
      unit_of_measurement: "W"
      value_template: "{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}"
      json_attributes_topic: "hobby/control/devices/evt"
      json_attributes_template: "{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}"

Error message:

Invalid config for [mqtt]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘d7’) for dictionary value @ data[‘mqtt’][‘sensor’][0][‘json_attributes_template’]. Got ‘{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}’

invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘d7’) for dictionary value @ data[‘mqtt’][‘sensor’][0][‘value_template’]. Got ‘{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}’. (See /config/configuration.yaml, line 12).

MQTT Discovery works regardless of how Home Assistant is installed (as a Docker container or not).

  1. Remove the MQTT Sensor’s configuration from configuration.yaml and then execute Reload Manually Configured MQTT Entities (or restart Home Assistant).
# remove all of this
mqtt: 
  sensor:
    - name: "cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e"
      state_topic: "hobby/control/devices/evt"
      unit_of_measurement: "W"
      value_template: "{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}"
      json_attributes_topic: "hobby/control/devices/evt"
      json_attributes_template: "{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}"
  1. Create the following script:
  create_sensors:
    alias: "Create sensors via MQTT Discovery"
    sequence:
      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/electrical_power/config
          retain: true
          payload: >
            {
              "name": "Electrical Power",
              "unique_id": "cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e_electrical_power",
              "state_topic": "hobby/control/devices/evt",
              "unit_of_measurement": "W",
              "value_template": {% raw %}"{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}"{% endraw %},
              "json_attributes_topic": "hobby/control/devices/evt",
              "json_attributes_template": {% raw %}"{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}{% endraw %}"
            }
  1. Execute script.create_sensors
  2. Confirm sensor.electrical_power was created.

Thanks for explaining this. I feel a total n00b but can’t get the script working in HA: Message malformed: Unable to determine action @ data[‘sequence’][0]

Or should I just execute it in the command line? Or add it to the scripts.yaml?

Let’s try something else instead of the script.

Go to Developer Tools > Services, switch to YAML mode, erase whatever is in the editing window, then copy-paste the following YAML into the editing window.

service: mqtt.publish
data:
  topic: homeassistant/sensor/electrical_power/config
  retain: true
  payload: >
    {
      "name": "Electrical Power",
      "unique_id": "cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e_electrical_power",
      "state_topic": "hobby/control/devices/evt",
      "unit_of_measurement": "W",
      "value_template": {% raw %}"{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e.ElectricalPower }}"{% endraw %},
      "json_attributes_topic": "hobby/control/devices/evt",
      "json_attributes_template": {% raw %}"{{ value_json.cfab8d1c-d2a4-40d7-8475-00e9cffcfd5e | tojson }}{% endraw %}"
    }

Click the Call Service button.

That gives a green checkmark :wink: