How to create mqtt switch

I want to have in my Home Assistant button called Nobody home for different automation purposes (at least to turn off all the lights and some power lines). Every click should toggle switch stated. But it doesn’t work as expected.

in configuration.yaml I created following

switch: !include_dir_list ../entities/switches

in …/entities/switches/nobodyHome.yaml I have this code

platform: mqtt
name: "Nobody Home mode"
icon: mdi:home-minus-outline
state_topic: "smarthome/nobodyHome/status"
command_topic: "smarthome/nobodyHome/switch"
qos: 0
payload_on: "ON"
payload_off: "OFF"
optimistic: false
retain: true

then on the panel I show button like that

type: button
  action: toggle
entity: switch.nobody_home_mode

Then I open windows MQTT explorer to check what happens when I click the button.
I expect that if current status is ON then after click it should be OFF, then next click ON and so on.
But in reality after connection MQTT explorer shows me that initial state of smarthome/nobodyHome/switch is ON. And every next click just send ON again. So it is never OFF.
What is the problem?

If you only want a switch entity to use in automations and the UI, you could use an input_boolean instead. Input Boolean - Home Assistant

I am newbie in HA and at the moment I have no idea about automations integrated in HA. Under automations I meant that after every click mqtt message must be sent and then my arduino controller should do something.
With this input_boolean I don’t see the way how to send mqtt message.
command_topic and payload options are not allowed.

Then disregard my answer, I figured you wanted a switch as a “helper” for automations and the UI only.

Let’s give your question another try now that I understand what you want (which I should have understood after more careful reading :slight_smile: )

The state_topic is the topic that your Arduino device should publish to when it receives an mqtt message from HA, to confirm a state change.
If your Arduino does not publish a state to the state_topic, you can either use the command_topic as the state_topic, or use optimistic mode.

So unless your Arduino publish a state, the following will retain the correct state of your switch:

state_topic: "smarthome/nobodyHome/switch"
command_topic: "smarthome/nobodyHome/switch"

So you mean that correct way to do that is like that:

state_topic: "smarthome/nobodyHome"
command_topic: "smarthome/nobodyHome/switch"

When I click on mqtt switch in HA message sent to topic smarthome/nobodyHome/switch and Arduino should generate answer to topic smarthome/nobodyHome.


Correct. Your Arduino should send a message to the state_topic.

Thanks. I fixed my Arduino code to republish message in callback. Can you help me also to understand how to handle availability_topic option?

I havn’t implemented availability in my arduino code yet, but you can read more about it here:
and here:

Basically, your arduino tells the MQTT broker the maximum time it will stay silent until the MQTT broker flags you as unavailable. If for instance the connection from your arduino to the MQTT broker is broken without the Arduino first gracefully disconnects, HA will know that the entity is unavailable.