Need some minor help with MQTT strings



  • Hi,

    I have a MQTTClient from the dev branch communicating with my Mosquitto Broker, and relaying information from Temp/Humidity sensors nicely. Now I have been trying to add in my relays, and i'm not sure what string to use for them to send them data. I'm using Home-Assistant, and my MQTT strings look like this for temp/humid/motion and work fine:

    #Sensors
    sensor 1:
      platform: mqtt
      state_topic: "mygateway1-out/13/0/1/0/1"
      name: "Upstairs Humidity"
      unit_of_measurement: "%"
    
    sensor 3:
      platform: mqtt
      state_topic: "mygateway1-out/13/1/1/0/0"
      name: "Upstairs Temperature"
      unit_of_measurement: "°C"
    
    sensor 4:
      platform: mqtt
      state_topic: "mygateway1-out/13/2/1/0/16"
      name: "Upstairs Room PIR"
      payload_on: "1"
      payload_off: "0"
    
    sensor 5:
      platform: mqtt
      state_topic: "mygateway1-out/1/1/1/0/16"
      name: "Test PIR LR"
      payload_on: "ON"
      payload_off: "OFF"
    
    

    But my relay one does not. This is what it looks like:

    #Switches (Lights)
    switch 1:
      platform: mqtt
      state_topic: "mygateway1-out/2/255/3/0/3"
      command_topic: "mygateway1-in/2/255/3/0/3"
      name: "test1"
      payload_on: "1"
      payload_off: "0"
    

    I notice that both my PIR sensors end in /16, so i'm guessing there is a standard for light switches as well. Anyone have any idea where I would be going wrong? This is the sketch I am using. There are two relays connected one on Pin 3 the other on Pin 5, so it's set up for 3 relays right now.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to control physical relays. 
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */ 
    
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 3 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define NODE_ID 2 //Disable to have GW set automatically
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, NODE_ID, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("EasyLED", "1.0");
    
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    

    Thanks,



  • @drock1985 said:

    command_topic: "mygateway1-in/2/255/3/0/3"

    This does not look korrekt.
    The serial API says:

    node-id;child-sensor-id;message-type;ack;sub-type;payload\n
    

    so you are trying to send something to node 2, sensor 255. That does not work. Your skeetch is numbering the child from 1 to NUM_RELAYS

    so i think something like

    command_topic: "mygateway1-in/2/1/3/0/3"
    

    should to... But that's my theoretical knowledge 😄 haven't build a relay yet


  • Plugin Developer

    The third digit from the left is the command/message type and should not be 3 but 1, to set a value. 3 means internal command/message. See the mysensors API.

    Edit:

    Since you are using a state topic in home assistant, you also need to send a message to the controller of the state change, after receiving the command to switch the relay.

    For the sensors, I suggest using binary sensors in home assistant for the PIRs that only report on/off.



  • Well, getting closer @dakky and @martinhjelmare

    The Serial API page is a good resource, but i'm still having issues with the gateway-in part I think. HA starts the light as off, so when I try to switch it on I receive the message in mosquitto mygateway-out:

    mygateway1-out/2/0/1/1/3 1
    

    And this is the output I receive via serial from the sensor:

    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=ok:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=fail:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=fail:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=ok:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=ok:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=ok:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=ok:1
    read: 0-1-2 s=0,c=1,t=3,pt=0,l=1,sg=0:1
    send: 2-2-1-0 s=0,c=1,t=3,pt=0,l=1,sg=0,st=ok:1
    

    So i'm getting closer, i'm sure it's in the communication somewhere but not sure where. This is what my configuration.yaml file looks like right now....

    #Switches (Lights)
    switch 1:
      platform: mqtt
      state_topic: "mygateway1-out/2/0/1/2/3"
      command_topic: "mygateway1-in/2/0/1/2/3"
      name: "test1"
      payload_on: "1"
      payload_off: "0"
    
    

  • Plugin Developer

    Read @dakky 's post again. Your MQTT topic structure is still not correct. Topics in HA config does not match what you write that mosquitto outputs.

    Post your new sketch, might be something in there as well.


  • Plugin Developer

    @drock1985 I guess your switch string should be as follows,

    #Switches (Lights)
    switch 1:
      platform: mqtt
      state_topic: "mygateway1-out/2/1/1/0/2"
      command_topic: "mygateway1-in/2/1/1/0/2"
      name: "test1"
      payload_on: "1"
      payload_off: "0"
    

 

Suggested Topics

259
Online

8.6k
Users

9.4k
Topics

99.1k
Posts