Use MQTT Service

Hi!
First of all: i realy like home assistant, i tried nodered and OpenHAB before, and this is the tool to go for me, thanks for this!

So my question is realy basic:
I want to forward a MQTT message from channel 1 to channel 2 (home/input to home/output)

Now i dont get how to define a “service”:

It says “The MQTT component will register the service publish which allows publishing messages to MQTT topics”. So i installed the mqtt broker (which is working, checked from PC). But when i open the “Service” Tab i can not find a service named “publish”. Same thing when adding an automation, there is no “publish” service available to choose from under “action”. Did i install the wrong MQTT Thing? Which one do i need to install, and how?

Best wishes

You probably need to add the mqtt component to your configuration.yaml before the service shows up.

I added this:
image

but there is still no “publish” Service to choose from (sorry can only put one picture due to some restriction).

Or am i mixing definitions here? Do i need to define a service somewhere else to use the publishing service?

Okay now there is a mqtt.publish Service! Dont know why it suddenly appeared. But my next question is: Now i can select it, but i can not give it a topic and a payload, just a payload. How do i tell the service to write to a specific topic?

In all the configuration you need to keep the indentation as in the docs. You are missing two spaces in front of “broker”:

mqtt:
  broker: 127.0.0.1

Yes now it appears.

So the initial question is still there. I found this on reddit as an example how to do what i want to do (forward a message):

alias: 'test'
trigger:
 platform: mqtt
 topic: home/buzzer1
 payload: PRESSED
action:
 service:mqtt.publish
 data:
  topic: home/test
  payload: "test"

This seems to do what i want to do. But where do i put it?
When i put it in configuration.yaml it shows errors. Same with automation.yaml

What is the right file to choose? Does this even do what i try to do?

1 Like

To put it into the automation.yaml is the right way to do it. Just remember that indentation is really important when writing YAML.

Try it with the code below:

- action:
  - data:
      topic: home/test
      payload: "test"
    service: mqtt.publish
  alias: Test
  condition: []
  id: '1111111111111'
  trigger:
  - payload: PRESSED
    platform: mqtt
    topic: home/buzzer1

You could also use the “Automation” function below “Configuration” in hass.io’s sidebar and use that web interface to create your automation. If you do it that way you have to provide the service data like so:

{
  "payload": "test",
  "topic": "home/test"
}

If you want to keep Node-RED you may want to have a look at NotoriousBDG Add-Ons

1 Like

Yes thanks for the answer! This worked! Even with the automation tab.

Can i ask another question?
This example:

Shows

action:
> service: light.turn_on
> entity_id: light.couch_lamp

So the reason to use that MQTT forwarding is that i try to control mopidy, which listens on a specific channel.
The Syntax with service and entity_id looks nicer than writing that plain MQTT. Which tab / config file do i use to define a “play_music_remotely” service? The Service Tab just lets me call a service.

To be able to call mopidy directly using a service someone must implement a corresponding component. Without a component you are not able to write something like media_player.media_play for media_player.mopidy_living_room (I’m assuming mopidy is a music player because Google Search told me so).

As long as there is no component you have to stick to the mqtt.publish service and communicate using mqtt.

What exactly are you trying to achieve here? How do you wanna control it using MQTT. From within hass.io’s web interface? MQTT Switch might be a solution then - at least to start and stop the music…

Mopidy is controllable by a MQTT channel, thats the part i have already finished now. Yes the aim is to have a trigger (like that buzzer in my example) or a RFID controlled ESP, but those are other projects. With mopidy i can play specific spotify playlists, triggered by mqtt. Thats why i am interested in managing mqtt so much. Hassio is just there to forward the message from the trigger to the actor. But using it on a webinterface is good for a later usage, but no priority now.

So i would have to create a component to use that nice looking syntax?

If you are able to create some kind of component it may be a possibility but I’m not an expert regarding that topic.

What I don’t get: I would assume you need one automation rule that forwards message from several different topics to one other topic. Why bother writing a component when one automation rule should do the job?

Or even simpler: Why do you need forwarding at all? Isn’t it possible to change the publish topic on your triggers to directly write into the topic mopidy has subscribed?

Yes you are right, it would be enough for the given task.
The plan is to extend the “action” part in the future, to not just control mopidy by MQTT, but also control the lights triggered by the same MQTT message. I brought the component up to learn how to do it, because i dont realy get how everything works together (triggers, components, automations, services, and the different files).

There are switches, binary_sensors and others. Could you tell me which kind of device-type i should use for the buzzer? It does not have an “ON - OFF” state. It is just “pressed”.

Okay, you don’t need a component to achieve what you are trying to do.

The buzzer you’ve bought will also use MQTT to communicate? Are you programming the code of the buzzer yourself? You could use MQTT Switch then and program it so that every press toggle between sending the on or off command.

To get a deeper understanding of what is possible with hass.io and how it works you should have a look at the documentation.

Yes the buzzer was rewired to have wifi with an esp8266 and is sending to MQTT.
So the buzzer should send “ON” and “OFF”, so the switch can use that information?
There is no “fire and forget” trigger once switch?

One more question, as i now got deeper into components. I am not sure where to lookup the “Service Data” like this one:
image

I see examples over the internet, but no actual description of that service.
I found this:

but it does not discuss “service data”. Any hints on how to get those information without asking in the forums every time?

When you are implementing the code for the esp8266 that is wired to that buzzer you should implement a method that toggles between on and off depending on what was sent last.

Have a look at the developer tools on the bottom of the sidebar. There is a little remote icon called “services” that provides a lot of information. Right next to it there is also an icon titled “states” that may be useful too if you want to know more about what is available in your own hass instance.

The service data for that specific case in your screenshot could be …

{
  "entity_id": "light.bedroom"
}

… if you have a light called bedroom. You can see all available entity ids under “states”.

Yes thank you a lot for your information and patience, you helped me a lot! I think i am good with automation, states and devices now :slight_smile:
Everything working, Home Assistant is awesome!

2 Likes

hey dwist could you share the automation that does this:
“a method that toggles between on and off depending on what was sent last.”

please share the yaml if you want.!