MQTT Light on a different host

I have a light on a different host system that I can control with MQTT via a command like:
mosquitto_pub -m '{"brightness": 0}' -t '$hardware/status/reset' -h ninjasphere.local

That is, I need to use -h : mqtt host to connect to. Defaults to localhost.

Is there a way to specify a different host to send MQTT commands to? I didn’t see it in any config options.
It’s only for this one… I want the normal MQTT topics on the HA host for everything else?

HA can only talk to one MQTT broker.

All the clients on your network should be talking to the same broker - is there some reason why you are using two?

My light is on another system, which has its own MQTT broker. I don’t know how to change that.
I guess I could use a command line switch, but I’d rather it be a “light”.

Can you use that broker for everything?

No, I don’t want to. It was a somewhat unreliable system :slight_smile:
I used to run HA on it, but switched to a RPi3.

Could I write something that listens on my HA system and forwards messages to the other system… easily, that is… is that a thing people do?

Somebody created a bridge between their local broker and cloudMQTT, which might be useful. But having two brokers on a local network is an unusual situation.

Failing this idea… is there any way to tell HA that a switch should be treated like a light?
I could make this a command_line switch and I also have a power socket that I use for a lamp.
I’d like to be able to ask Siri to “turn off the lights” and have it turn the socket (and command_line switch) off too.
EDIT: looks like might be the solution…

The template light looks the best bet to me.

This is a php script that publish something on a mqtt server. You might create an on / off php scripts that could be a command line switch,


  $host = ""; 
  $port = 1883;
  $clientid = "xx";
  $username = "xx"; 
  $password = "xx"; 
  $mqtt = new phpMQTT($host, $port, $clientid); 

  if ($mqtt->connect(true,NULL,$username,$password)) {
    $mqtt->publish("rpi3/hcvr_motion/body",$body_decoded, 0);      
    echo "Fail or time out<br />";

1 Like

you can use Node RED to link multiple MQTT brokers or use it to link your light to HA

Thanks for everyone’s suggestions. Here’s what I’ve done (and it works):

Python script:

#!/usr/bin/env python
"""Set Spheramid underlight to brightness in command line parameter."""

import os
import sys

    value = int(sys.argv[1])
    if value < 0:
        value = 0
    elif value > 100:
        value = 100
except (ValueError, IndexError):
    value = 0

os.system("mosquitto_pub -m '{{\"brightness\": {}}}' -t '$hardware/status/reset' -h ninjasphere.local".format(value))

HA config

  set_sphere_light: 'python /home/homeassistant/.homeassistant/scripts/ {{ brightness }}'

- platform: template
      friendly_name: "Ninja Light"
        service: shell_command.set_sphere_light
          brightness: 100
        service: shell_command.set_sphere_light
          brightness: 0
        service: shell_command.set_sphere_light
          brightness: "{{ brightness }}"

The only thing I don’t have is an accurate read of its brightness, but I don’t think it provides that, so it’s no problem.
So now I can turn the light on and off, and I can adjust the slider to set its brightness. All good.

1 Like