Mosquitto not working as expected

I have these 3 entries as binary_sensor about mosquitto:

  - platform: mqtt
    name: "Maurizio"
    state_topic: '/homeassistant/location/mau'
    payload_on: 'present'
    payload_off: 'not_present'
    sensor_class: occupancy    

  - platform: mqtt
    name: "Fha"
    state_topic: '/homeassistant/location/fha'
    payload_on: 'present'
    payload_off: 'not-present'
    sensor_class: occupancy

  - platform: command_line
    name: Mosquitto
    command: sudo systemctl status mosquitto.service | awk 'FNR==3 { print $3 }'
    device_class: connectivity
    payload_on: "(running)"
    payload_off: "(dead)"

but none of them is working.
All of them are created as binary_sensor, but the first two are always off and the third (after creating a switch like this):

- platform: template
  scan_interval: 5
  switches:
    mosquitto:
      friendly_name: 'Mosquitto Service'
      value_template: "{{ is_state('binary_sensor.mosquitto','on') }}"
      turn_on:
        - service: shell_command.start_mosquitto
      turn_off:
        - service: shell_command.stop_mosquitto

doesn’t start or stop the mosquito.service giving me this error:

“Error running command: sudo systemctl start mosquitto.service, return code: 1”

Where are the errors?
Please help me as a really newbie with mqtt components.

EDIT: my entry regarding mqtt component is the following:

mqtt:
  broker: !secret mqtt_broker
  port: 1883
  client_id: home-assistant-1
  keepalive: 60
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery: true
  discovery_prefix: homeassistant

you can check the logs in /var/log either syslog, mosquitto.log.

Being that you can’t start the service it’s most likely a config issue in /etc/mosquitto/mosquitto.conf . Check that for anything (syslog may point to a line or systemctl status mosquitto.service will tell you what to fix).

That’s where I’d start.

The most likely reason for this is that the user you are running HA as does not have sudo privileges. This is a security precaution, for good reasons, and I suggest, as a beginner you do not try to circumvent it.

In any event, restarting mosquitto is not necessary, it should be run at startup and left alone

I use this sensor to check the state of the mosquitto service.

sensor:
  - platform: command_line
    name: "mosquitto status"
    command: systemctl show mosquitto.service | awk -F= '/^SubState=/ {print $2}'
    scan_interval: 60

We cannot help with your binary sensors until you show what payload you are receiving in the MQTT messages.

Thanks for the reply. As newbie i still do not understand how mqtt works so i am still not able to know where to read messages from mosquitto. Will you help me in understanding this so i will be able to track and fix errors by myself.

This is a good introduction

If you follow that you will see you need a client to connect to the broker to display messages. There are a number of graphical clients around, if you like those, but I find the command line client mosquitto_sub to be just as effective. Using mosquitto_pub and mosquitto_sub is described here

Edit:

I thought I had a link to this somewhere

if you

tail -f /var/log/mosquitto/mosquitto.log

in a shell you can see what’s happening with mosquitto. mosquitto_sub allows you to see the messages by executing

mosquitto_sub -t "#" -u username -P password -h localhost -v'

in a shell.
Don’t worry about publishing right now because obviously it’s not working as as. Again. Check your /etc/mosquitto/mosquitto.conf to figure out your error. The error is probably and most likely visible by checking the /var/log/syslog if you just look.

Here it is:

pi@hassbian:~$ sudo tail -f /var/log/mosquitto/mosquitto.log
1526335661: mosquitto version 1.4.15 (build date Sat, 07 Apr 2018 11:13:41 +0100) starting
1526335661: Config loaded from /etc/mosquitto/mosquitto.conf.
1526335661: Opening ipv4 listen socket on port 1883.
1526335661: Error: Address already in use

What does it means that error?

it means that there is something already using 1883, as in the service is already running or another service is using 1883.

do you see mosquitto running when you run ps -A | grep mosquitto from the shell? Ideally mosquitto should launch at boot and should never die unless it’s stopped.

Here it is:

pi@hassbian:~$ ps -A | grep mosquitto
8330 ? 00:00:44 mosquitto

So yeah, mosquito is running. You should be able to publish and subscribe to topics now.

Great, first step is done… and now? :grinning:
I must begin to try something inside HASS… any advice where to begin?

The easiest thing I would recommend is a basic sensor that displays a message. Then you can use an app or HA’s MQTT client to publish messages. Once you know everything is working you can move on to other things. You’ve got to wrap your head around how topics work before it really makes sense. For instance topic-a is not the same as 'topic-a'. At least now you know its working, you just need to get some clients on and sending messages. If you use an android phone get the app ‘MQTT Dash’’’ as it can send/recv Matt messages and can make troubleshooting your HA sensors a little easier

Following some examples here i tried to make a device.tracker with mqtt to enable/disable lights when home/not_home and put this in configuration, but it seems not working as the device trackers are not created or cannot find them.

- platform: mqtt_json
  devices:
    mau_s8: /location/mau
    fha_n3: /location/fha

have you defined your device trackers? Are your devices publishing MQTT messages?

I’m not using the platform you are so bear with me.

A quick and dirty about MQTT…

MQTT boils down to devices and brokers. Devices are simply, devices. Imagine you have 2 bicycles, a red one and a blue one, we’ll call those devices red-bike and blue-bike. After that we have brokers. Brokers are what get messages and publish messages. They are responsible for everything. Assume your garage is your broker. It’s where you ‘place’ your client. It’s also responsible for knowing information about your client, ie whether or not your bike is in the garage determines whether or not you’re home. Now imagine when you leave you tell the garage, “I’m leaving” but you can only do this by publishing a message to a topic. Imagine Topics are the roads and routes that you take your bike on when you go out for exercise, or ride to work, or a simple neighborhood stroll. So imagine a topic is ‘red-bike/road’. So you’ve told your broker that you are taking the ‘red-bike’ on the ‘road’ but you need to deliver a message. Messages are usually placed in the syntax of topic [SPACE] message, ie red-bike/road AWAY. So, if you’re still following me and I hope you are, you’re trying to set up a platform component that reads a topic, ie red-bike/road and sets its value to the message away and that’s what is seen on HA. In order for this all to happen your device, red-bike, needs to be able to publish a message on the topic to HA in order for this all to work. It sounds confusing I’m sure but MQTT is really basic and really simple once you understand what a TOPIC and a MESSAGE is because that’s all it really is. Someone send(PUBLISHES) a message in the TOPIC to the broker and the broker tells everyone what the message is, but only those who listent (SUBSCRIBE) to that message.

I hope this all makes sense.

Yes, it’s very clear and now i must put in writings what you explained in words… :grin:
I’ll try…

Could you help me about this problem?!?

I don’t manage to start mosquitto broker.
Here is what i have in mosquitto.log:

tail -f /var/log/mosquitto/mosquitto.log
1558461049: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1558462850: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1558464651: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1558466452: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1558467560: New connection from 192.168.1.96 on port 1883.
1558467560: Socket error on client , disconnecting.
1558468253: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1558470054: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1558470829: Error in poll: Interrupted system call.
1558470829: mosquitto version 1.4.10 terminating

Thanks in advance for any help!
Regards,

Alessandro