Open Stubbs opened 11 months ago
A little more on this, from the mqtt logs themselves:
mqtt | 1697055632: New connection from 192.168.1.62:50051 on port 1883.
mqtt | 1697055632: New client connected from 192.168.1.62:50051 as enviro_plus (p2, c1, k0, u'ha').
mqtt | 1697055632: Client enviro_plus disconnected due to malformed packet.
That's without the padding on the payload, with the padding it's a different error:
mqtt | 1697055412: New connection from 192.168.1.62:50046 on port 1883.
mqtt | 1697055413: New client connected from 192.168.1.62:50046 as enviro_plus (p2, c1, k0, u'ha').
mqtt | 1697055413: Client enviro_plus disconnected due to protocol error.
The issue is caused by "unit_of_measurement" where some of the values are UTF-8, even when I encode the strings with .encode() it still causes the protocol error.
The padding is a red herring.
Hi Stubbs
I assume that you have to many '{{' around "{{{{ value_json.temperature }}}}"
.
Before passing the strings to mqtt.publish()
, you could verify it using this code:
json_string =f'{{ "name": "Gas resistance"...'
print(json.loads(json_string))
mqtt.publish(..., json_string)
Looking at the print output, you will quickly see, which variables have not been expanded.
Write a python dictionary and let json.dumps()
do the job for you.
dict_json = { "name": "Gas resistance", "state_topic": "..."}
mqtt.publish(..., json.dumps(dict_json))
For info, I just stumbled across this same problem. I had working code until I tried to publish a temperature and included the ° (degree) symbol. I haven't the patience to figure out how to solve this, so I've just dropped that symbol!
With json.dumps()
, there's still issue with some charectors such as °
.
payload = { "name": "Temprature", "value_template": "{{ value_json.temp }}", "unit_of_measurement": "°C" }
mqtt.publish(TOPIC, json.dumps(payload))
Just crashes and thows ECONNRESET
. If I remove degree charector, It goes through.
I'm trying to send config info to Home Assistant from a Pico W with the following code:
There are a couple of issues, firstly I have to pad the paylod with 3 extra characters or the json that's sent in missing it's terminating brackets.
The other issue is that it only seems to send the first message, I have MQTT Explorer watching what's published and I only ever see the first of those published messages, no matter what order I put them in.