Help communicating with HA via windows cmd line using mosquitto_pub

WHAT I’M TRYING TO DO:
I’m trying to send commands via windows cmd line using the windows mosquitto publish client.

WHAT I’VE DONE SO FAR:

  1. Installed eclipse mosquitto MQTT broker on windows PC
  2. Added MQTT sensor according to the Home Assistant MQTT documentation. Title of the sensor is ahk_mosquitto_cmd".
  3. Sent cmd line command to Home Assistant MQTT broker.
  4. Using MQTT Explorer, confirmed the cmd line command made it to the broker (see MQTT broker screenshot below).
  5. The sensor created int he config file (ahk_mosquitto_cmd) does not change on or off with the payload.

MY SPECIFIC QUESTIONS:

  1. Is the structure of my command here (see below code) okay?
  2. Do I need to do anything specific with my MQTT sensor to make it recognize the payload?

THIS IS THE CMD LINE CODE:

mosquitto_pub -h <my ip address> -t 'light.office_ceiling_lights' -m 'off' -d -u mqtt-user -P <my password>

SCREENSHOT OF CMD LINE:

SCREENSHOT OF MQTT BROKER:

Color me confused. Your cmd line code says to turn the light off and the screenshot says to turn it on, which the Explorer indicates.

So, what is the question?

By the way, obsfucating a local (private) IP address is like covering your eyes and thinking you’re invisible. (almost) EVERYBODY uses an IP of 192.168.1.X. Your private IP address is behind your router and it is meaningless to everyone else. Including hackers.

@stevemann Thank you for responding.

Good eye on the on / off discrepancy. I’ve been attempting a lot of variations / iterations. The reason for the on / off discrepancy is because I took the screenshot before I made a change in the cmd line. I should have taken an updated screenshot. I apologize for the confusion.

In the most current iteration, I’ve created an MQTT sensor and am trying to toggle it on/off.

Configuration.yaml line:

mqtt:
  sensor:
    - name: "Command line Testing"
      state_topic: "ahk_mosquitto_cmd"

CMD line:

mosquitto_pub -h 192.168.1.83 -t 'ahk_mosquitto_cmd' -m 'on' -d -u mqtt-user -P <my password>

CMD line screenshot:

MQTT broker screenshot:

mqtt:
  sensor:
    - name: "Command line Testing"
      state_topic: "ahk_mosquitto_cmd"

What I do see is an incomplete sensor configuration.
Here is how I turn on a light relay:

mqtt:
  switch:
    - name: "Master Bedroom Light"  
      command_topic: "cmnd/mbrLights/POWER"
      state_topic: "stat/mbrLights/POWER"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      retain: true

The command_topic is what topic you use to send a payload to the device.
state_topic is what the device sends back.
payload_on is the payload you send to the device to turn it on.
You can guess what payload_off does.

Your command line sends “on” to the topic “ahk_mosquitto_cmd”.

@stevemann Thank you again for your help / suggestions.

I’m getting an invalid config error when I try configuring my sensor with these parameters. Home assistant doesn’t appear to like the “command_topic” keywords used (see error screenshot). If I delete that line, then it moves to the first payload line and says it doesn’t like it. Then, if I delete that, it complains about the next payload line. Then, if I delete that, it complains about the retain line.

Here is my sensor configuration in config.yaml:

mqtt:
  sensor:
    - name: "Command Line Testing"  
      command_topic: "cmnd/cmdline/TOGGLE"
      state_topic: "stat/cmdline/TOGGLE"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      retain: true

Here is the error screenshot:

I got it working. Used examples from the Home Assistant MQTT documentation.

This was the final working configuration:

Configuration.yaml sensor setup:

mqtt:
  sensor:
    - name: "Command Line Testing"  
      state_topic: "stat/cmdline/TOGGLE"

CMD line:

mosquitto_pub -h 192.168.1.83 -p 1883 -u mqtt-user -P <my password> -t stat/cmdline/TOGGLE -m on

Screenshot of Home Assistant Entity (working):

My mistake, there is no command_topic in an MQTT switch. Only a state_topic as you discovered,

1 Like