Mosquitto not talking to Automation

I have a Pi-Core music player on my network that is turned on and off using a smart socket (it runs in memory and the storage is read only). I can control the socket without any problems using the Home Assistant Interface, on my phone or computer.

I’m in the process of adding an additional USB keypad, that gives me 12 keys that I can use to trigger actions on my computer, that I frequently carry out. These load applications, act as a shortcuts for key combinations and one shuts down my MagicMirror device, using a single key.

I want to set up a key to toggle the state of my Pi-Core player smart socket (Meross), and was planning to use MQTT to do this.

I installed the Mosquitto broker and have set it up with an account.

I have written a simple executable (C++ using the Mosquitto library) that will broadcast a topic of player/cmd with a payload of toggle every time it is run. It runs on desktop PC (Linux).

If I go into the MQTT settings and Listen to a topic on player/cmd, I get the payload of toggle, so the MQTT broker is receiving the messages that I am sending it.

If I look in the Mosquitto Broker logs, I see my software is making a New connection, that there is a New Client connected and it shows the clients user name (the one I set up for use with the broker). It then disconnects.

In the Home Assistant Log screen, I only see the action when I run it manually within Home Assistant and it works without fail.

I have set up an automation that I think should trigger the toggling on my smart socket and it does if I run the Action within Home Assistant (so the actions section of the yaml works).

alias: Toggle Music Player State
description: ""
triggers:
  - trigger: mqtt
    topic: player/cmd
    payload: toggle
conditions: []
actions:
  - type: toggle
    device_id: 7bdc89348112e27e40afbc15ecf0707a
    entity_id: 54a42928ec120a2c854a9f25115cb369
    domain: switch
mode: single

The automation action isn’t triggered, so I’m thinking there is something missing between the Mosquitto broker and the Automation, I’m sure I’m doing something daft/have missed out a step, but I can’t see it. Any assistance would be appreciated.

I suggest you start by installing MQTT Explorer, connect it to your broker, then confirm that your software is actually sending the topic.

Thank you for the suggestion.

I installed and ran MQTT Explorer and got the following when I ran my executable.

image

So my application is communicating with the Mosquitto broker, if I’m understanding things correctly.

Try to test it inside mqtt integration. If you go to configure you can publish and listen to a topics.
Maybe your topic is not correct or you are publishing wrong message.

Look closely at the screenshot you posted. There’s a nonprintable character at the end of toggle.

That’s sufficient to prevent the automation’s MQTT Trigger from matching the desired string toggle.

You can test this hypothesis by removing this line from the MQTT Trigger.

payload: toggle

If after testing the automation triggers successfully, you need to revisit your C++ program and eliminate the trailing nonprintable character it’s inserting into the payload.

1 Like

That was a good idea, if I publish to the player/cmd topic the payload toggle, from within the Home Assistant add-on, it does turn the Raspberry Pi, on and off (the case has colored LEDs so it is easy to see :slight_smile: ).

I have to be doing something else now, but I will check out the value of the payload in the executable. In the picture in my last post, it shows a square at the end of the payload, which I wonder is a non printable character. I’ll check that out later.

I’m wondering if it is the terminating NULL, as I passed a NULL terminated array of characters to the library function perhaps it should have been a cstring?

Thanks for the suggestion, I’ll investigate.

As mentioned in my previous post, your payload contains a trailing nonprintable character (visible in the screenshot you posted). Its presence prevents the MQTT Trigger from matching the string toggle as specified in its payload option.

You need to modify your C++ program to eliminate the inclusion of the trailing NULL.

C++ example

It was the trailing NULL, but the problem wasn’t caused by using a character array.

The Mosquitto C++ MQTT library has a mosquitto_publish function, to which you pass the payload string and it’s size.

It was caused by the fact that I included the NULL character within the length of the string, which are two of the seven parameters you pass to it. The function was including 7 characters as the payload (so including the NULL), rather than 6, which is what I should have specified.

A lot of functions you use in C/C++, when you are dealing with character arrays, you would include the NULL character as the size, to allow for it to be allocated in memory. I should have checked!

Thanks all, it now works.

It was solve by combining suggestions from a number of people, that brought me to the point of resolving it.

Mike

You’re welcome!

Before you go, please consider marking my post above with the Solution tag. It was the first to identify the root cause of the stated problem (transmitted payload contains trailing nonprintable character which prevents it from matching the MQTT Trigger’s payload value). This is the guideline used by this community forum.

For more information about this guideline and others, please refer to the forum’s FAQ.