Testcase MQTT not working

I’m trying to simulate the MQTT service for a switch. The MQTT client connects to HA, but the
switch is not switched or no message is returned that the switch has been switched.

The log does not see any messages, even if it is set to homeassistant.components.mqtt: debug.
What can I do ?

Testcase:

### MQTT Broker #########################
mqtt:
  broker: !secret mqtt_broker
  port: !secret mqtt_port
  client_id: ha-system-1
  keepalive: 60
  username: !secret mqtt_username
  password: !secret mqtt_password
  protocol: 3.1
  
  ### SWITCH ############################
 - platform: mqtt
   name: "Sonoff power"
   state_topic: "stat/switch/POWER"
   command_topic: "cmnd/stat/switch/POWER"
   qos: 1
   payload_on: "ON"
   payload_off: "OFF"
   retain: true

Javascript

  ## SIMULATE SWITCH SCRIPT ###########
  'use strict';
  // call: node mqttswitchtest.js
  const mqtt   = require('mqtt');
  const moment = require('moment');
  const broker = 'mqtt://hassbian.local';
  const options = {
      port: 1883,
      username: '******', 
      password: '******'
  };
   
  // MQTT Client
  const client = mqtt.connect(broker,options); 
  const device = 'stat/switch';
  let state = 'OFF';
  let timer;
  
  client.on('connect', function () {
  	console.log(`${moment().format("YYYY-MM-DD HH:mm:ss")} Client connected to ${broker}`);
  	client.subscribe(`stat/${device}/+`);
  	client.subscribe(`tele/${device}/+`);
  	client.publish(`cmnd/${device}/status`);
  	timer = setInterval(loop, 2000);
  });

  client.on('message', function (topic, message) {
  	if (topic === `stat/${device}/POWER`) {
  		state = message.toString();
  	}
  	console.log(`${moment().format("YYYY-MM-DD HH:mm:ss")} RX ${topic} ${message}`);
  });

  function loop() {
  	if (!client.connected) {
  		return timer && clearInterval(timer);
  	}
  	let newState = state === 'OFF' ? 'ON' : 'OFF'; 
  	client.publish(`cmnd/${device}/POWER`, newState);
  	console.log(`${moment().format("YYYY-MM-DD HH:mm:ss")} TX cmnd/${device}/POWER ${newState}`);
  }

First check your wildcards for the topics.

Thanks for your info. But also the nativ call do nothing:
mosquitto_pub -h 127.0.0.1 -p 1883 -u hasysuser -P p4pwseci -i ha-siebler-1 -t "stat/switch/POWER" -m "ON"

Sorry, not the wildcards, the variables in your topics.

client.publish(`cmnd/${device}/status`);

And the quotes and the string concatenation.

Yes this was one problem, but it was the setting “client_id”, this must not be set. Unbelievable,
because with HA this is set at the settings MQTT.

Settings mqtt and switch

mqtt:
  broker: !secret mqtt_broker
  port: !secret mqtt_port
  username: !secret mqtt_username
  password: !secret mqtt_password
  client_id: !secret mqtt_client_id
  
  - platform: mqtt
    name: "Wohnzimmer Media"
    state_topic: "home/sonoffpow01"
    command_topic: "home/sonoffpow01/stat"
    qos: 0
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

Testcase (simulation for sonoff switch)

'use strict';
var mqtt     = require('mqtt'),
    moment   = require('moment'),
   _secrets  = require('./secrets.js');

var options = {
    port:     _secrets._mqtt.port,
    username: _secrets._mqtt.username, 
    password: _secrets._mqtt.password
};

var device    = 'home/sonoffpow01',
    cmd_topic = 'home/sonoffpow01/stat',
    state     = 'OFF',
    timer     = null,
    prevStat  = 'OFF',
    client    = mqtt.connect(_secrets._mqtt.host, options);

client.on('connect', function () {
    console.log(moment().format("YYYY-MM-DD HH:mm:ss") + ' Client connected to : ' + _secrets._mqtt.host);
    client.subscribe( cmd_topic );
    timer = setInterval(loop, 1000);
});

client.on('message', function (topic, message) {
    console.log(topic);
    if (topic === (cmd_topic) ) {
        console.log(moment().format("YYYY-MM-DD HH:mm:ss") + " RX " + topic + ': ' + message);
		state = message.toString();
	}
});

function loop() {
	if (!client.connected) {
		return timer && clearInterval(timer);
	}
    if(prevStat != state){
       console.log(moment().format("YYYY-MM-DD HH:mm:ss") + " publish new state " + state);
	   client.publish(device, state);
       prevStat = state;
    }
}

Thanks and have a nice day.