[SOLVED] How to switch a MQTT switch from master to remote client?

Hello

How to switch to MQTT switch from master to remote client?
If i switch from hass_camera the switch on hass_master work, but not from hass_master to hass_camera.
HA on hass_camera do not receive the incoming mqtt message, why?

This is my config, im usinging mosquitto like mqtt broker.

Master HA:

mqtt:
  broker: !secret mqtt_host
  port: 1883
  client_id: hass_master
  keepalive: 60
  username: !secret mqtt_user
  password: !secret mqtt_pass
  discovery: true
  discovery_prefix: homeassistant
  birth_message:
    topic: 'hass_master/status'
    payload: 'online'
  will_message:
    topic: 'hass_master/status'
    payload: 'offline'

switch:
  - platform: mqtt
    name: "Luce Camera"
    state_topic: "homeassistant/switch/luce_camera/state"
    command_topic: "homeassistant/switch/luce_camera/state"
    payload_on: "on"
    payload_off: "off"
    optimistic: false
    qos: 0
    retain: true

Slave HA:

mqtt:
  broker: !secret mqtt_host
  port: 1883
  client_id: hass_camera
  keepalive: 60
  username: !secret mqtt_user
  password: !secret mqtt_pass
  birth_message:
    topic: 'hass_camera/status'
    payload: 'online'
  will_message:
    topic: 'hass_camera/status'
    payload: 'offline'

mqtt_statestream:
  base_topic: homeassistant
  publish_attributes: true
  publish_timestamps: true

switch:
  - platform: rpi_gpio
    ports:
      21: Luce camera
    invert_logic: false

I don’t see anything in your slave config that will receive an MQTT message. The statestream will send all state changes out, but not receive anything.

@gpbenton, yes, what component i need to receive the mqtt messages?

From what I can see, all you want to do is to toggle gpio pins on a remote Pi. Using HA on the remote is a bit of overkill. I suggest you try something like this project, which is easier to configure

@gpbenton,

what I’m going to do is have an RPi in every room in the house, with an installation of HA installed, because every RPi will have to handle all the I/O in the room, like switchs, lights, sockets, windows opened, ecc …
But I want everything to be managed by an HA master.

will I document on flyte/pi-mqtt-gpio or are there other solutions?

Sorry, I don’t understand your question.

I’m sorry for my english.

I read what this add-on provides.
Are there any other solutions besides this?

I am sure there are other solutions - just search github.

I have this one bookmarked as it seemed well written and documented.

If you want to use HA, you could have an automation with an MQTT Trigger with an action that turns on the switch. I think the state of the switch should be reflected back to the host by the statestream. But as I said, this is rather a complex way of doing it.

A couple of other points

  • in your original post, the birth and will messages for the remote and slave must have different topics - otherwise any client subscribing will not know which HA instance has gone down.
  • the state and command topic should be different for the switch on the master.

Ok, i’ll try this solution.
I don’t want use external add-on, for now…

Yes, right, updated my config

Can you explain this point please?

I’ll try this generic automation:

- alias: 'test'
  trigger:
    platform: mqtt
    topic: homeassistant/switch/luce_camera
  action:
    service: switch.toggle
    entity_id:
    - switch.Luce_camera

Analyzing /var/log/daemon.log of the remote HA, and using the debug level for mqtt, I do not see any incoming message of mqtt

logger:
  default: error
  logs:
    homeassistant.components.mqtt: debug

Would this be the problem that the HA slave does not receive any mqtt message?

On Mosquitto log

Client mosqsub|3352-rasp-serve received PUBLISH (d0, q0, r0, m0, 'homeassistant/switch/luce_camera/state', ... (3 bytes))
off
Client mosqsub|3352-rasp-serve received PUBLISH (d0, q0, r0, m0, 'homeassistant/switch/luce_camera/state', ... (2 bytes))
on

There is only rasp-serve received and no one rasp-camer received, why rasp_camera do not watch mosquitto?

A good basic description of MQTT is here.

A client sends commands via an MQTT message, and other clients receive the message by subscribing to them. In HA terms, the command_topic describes the message that HA sends as a command, and the state_topic defines the message that HA subscribes to in order to receive the state of the device.

You can have the state_topic the same as the command_topic, but then you are just subscribing to the message that HA has just sent, so you are not really getting the state of the device, just that the broker received the message and sent it back.

Following this you should see

- alias: 'test'
  trigger:
    platform: mqtt
    topic: homeassistant/switch/luce_camera

will subscribe to the topic homeassistant/switch/luce_camera, which is not the same as the command being sent out by the switch in the master

    command_topic: "homeassistant/switch/luce_camera/state"

so the slave HA will not see the message.

Thank you @gpbenton
All work perfectly!

Recapping for all:

HA MASTER:

mqtt:
  broker: !secret mqtt_host
  port: 1883
  client_id: ha_master
  keepalive: 60
  username: !secret mqtt_user
  password: !secret mqtt_pass
  #discovery: true
  #discovery_prefix: homeassistant
  birth_message:
    topic: 'ha_master/status'
    payload: 'online'
  will_message:
    topic: 'ha_master/status'
    payload: 'offline'

switch:
- platform: mqtt
    name: "Luce Camera 1"
    state_topic: "ha_camera/switch/luce_camera_1/state"
    command_topic: "ha_camera/switch/luce_camera_1/set"
    payload_on: "on"
    payload_off: "off"
    optimistic: false
    qos: 0
    retain: true

HA SLAVE

mqtt:
  broker: !secret mqtt_host
  port: 1883
  client_id: ha_camera
  keepalive: 60
  username: !secret mqtt_user
  password: !secret mqtt_pass
  birth_message:
    topic: 'ha_camera/status'
    payload: 'online'
  will_message:
    topic: 'ha_camera/status'
    payload: 'offline'

mqtt_statestream:
  base_topic: ha_camera
  publish_attributes: true
  publish_timestamps: true

automation: 
  - alias: 'mqtt luce camera 1'
    trigger:
      platform: mqtt
      topic: ha_camera/switch/luce_camera_1/set
    action:
       service: switch.toggle
       entity_id:
       - switch.Luce_camera_1
switch:
  - platform: rpi_gpio
    ports:
      21: Luce camera
    invert_logic: false
2 Likes