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
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.
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.
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).
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?
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).
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?
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.