MQTT Light on a different host

Hi.
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”.
Thanks.

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.

Thanks.
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 https://home-assistant.io/components/light.template/ 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,

<?php
require("phpMQTT.php");

  $host = "192.168.2.150"; 
  $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);      
    $mqtt->close();
  }else{
    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

try:
    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

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

light:
- platform: template
  lights:
    ninja_light:
      friendly_name: "Ninja Light"
      turn_on:
        service: shell_command.set_sphere_light
        data_template:
          brightness: 100
      turn_off:
        service: shell_command.set_sphere_light
        data_template:
          brightness: 0
      set_level:
        service: shell_command.set_sphere_light
        data_template:
          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