MySensors 2 MQTT Multi Sensor - Close, but missing a few

Hello:

Hoping perhaps @martinhjelmare can help… I have put together an arduino multi sensor with PIR, DHT, Reed switch, and a Relay. Seems to be functioning ok based on serial output, and I can trigger the relay on / off from an MQTT broker. Some of the sensors are being presented to HASS, but some are not, ( for example the reed switch, or relay, and PIR ). The DHT seems fine… The IDE sketch I used is bellow, along with the serial output:

// Garage Door Relay ( Open/Close) + Motion (PIR), + DHT Temp/HUM, + Reed Switch
#define MY_DEBUG 
#define MY_RADIO_NRF24
#define MY_NODE_ID 50
#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>
#include <DHT.h>  

#define CHILD_ID_REL1 1
#define CHILD_ID_PIR 3
#define CHILD_ID_HUM 10
#define CHILD_ID_TEMP 11
#define REL1_PIN 4
#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 TOGGLE_INTERVAL 350  //Tells how many milliseconds the relay will be held closed
#define CLOSED 0
#define OPEN 1
#define PIR_PIN 3
#define HUMIDITY_SENSOR_DIGITAL_PIN 7
#define BUTTON_PIN_1  2       // Arduino Digital I/O pin for button/reed switch
#define CHILD_ID_SW 3

Bounce debouncer = Bounce(); 
int oldValue=-1;

MyMessage msgREL1(CHILD_ID_REL1, V_STATUS);  // 2
MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);
MyMessage msgHum(CHILD_ID_HUM, V_HUM); // 1
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); // 0
MyMessage msgSW(CHILD_ID_SW,V_TRIPPED);

DHT dht;

bool StateREL=0, StateREL1=0;
const unsigned long tUpdate=30000; // update interval
unsigned long t0;
float Temp, Hum;
byte StatePIR=0;
byte oldStatePIR=0;
unsigned long tSetup=30000; // warming time

void presentation()  {
  
  sendSketchInfo("Garage Multi Sensor", "1.0");
  present(CHILD_ID_REL1, S_BINARY);
  present(CHILD_ID_PIR, S_MOTION);
  present(CHILD_ID_HUM, S_HUM);
  present(CHILD_ID_TEMP, S_TEMP);
  present(CHILD_ID_SW, S_DOOR);  
}

void setup() {
  // Setup the button
  pinMode(BUTTON_PIN_1,INPUT);
  // Activate internal pull-up
  digitalWrite(BUTTON_PIN_1,HIGH);
  // After setting up the button, setup debouncer
  debouncer.attach(BUTTON_PIN_1);
  debouncer.interval(5);
  pinMode(REL1_PIN, OUTPUT);
  digitalWrite(REL1_PIN, StateREL1); 
  pinMode(PIR_PIN, INPUT_PULLUP);
  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
  
  t0=millis();
}

void loop() {
    debouncer.update();
  // Get the update value
  int value = debouncer.read();

  if (value != oldValue) {
     // Send in the new value
     send(msgSW.set(value==HIGH ? 1 : 0));
     oldValue = value;
  }
  StatePIR=digitalRead(PIR_PIN);
  if (StatePIR != oldStatePIR) {
    oldStatePIR=StatePIR;
    send(msgPir.set(StatePIR ? "ON" : "OFF"));
  }
    
  if ((millis()-t0) > tUpdate) ServerUpdate();

}

void ServerUpdate() {
  
  send(msgPir.set(StatePIR ? "ON" : "OFF"));
  
  Hum = dht.getHumidity();
  Temp = dht.getTemperature();
  send(msgTemp.set(Temp, 1));
  send(msgHum.set(Hum, 1));

  t0=millis();
}

void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_STATUS) {
     // Change relay state
     digitalWrite(message.sensor-1+REL1_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
     digitalWrite( REL1_PIN, RELAY_ON );
    //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
    delay( TOGGLE_INTERVAL );
    digitalWrite( REL1_PIN, RELAY_OFF );
    //Added this to tell the controller that we shut off the relay
     // Store state in eeprom
     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());
   } 
}

Serial Output:

Starting repeater (RNNRA-, 2.0.0)
TSM:INIT
TSM:RADIO:OK
TSP:ASSIGNID:OK (ID=50)
TSM:FPAR
TSP:MSG:SEND 50-50-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
TSP:MSG:READ 0-0-50 s=255,c=3,t=8,pt=1,l=1,sg=0:0
TSP:MSG:FPAR RES (ID=0, dist=0)
TSP:MSG:PAR OK (ID=0, dist=1)
TSM:FPAR:OK
TSM:ID
TSM:CHKID:OK (ID=50)
TSM:UPL
TSP:PING:SEND (dest=0)
TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
TSP:MSG:READ 0-0-50 s=255,c=3,t=25,pt=1,l=1,sg=0:1
TSP:MSG:PONG RECV (hops=1)
TSP:CHKUPL:OK
TSM:UPL:OK
TSM:READY
TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=ok:0100
TSP:MSG:SEND 50-50-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,ft=0,st=ok:2.0.0
TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=ok:0
TSP:MSG:READ 0-0-50 s=255,c=3,t=6,pt=0,l=1,sg=0:M
TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=11,pt=0,l=19,sg=0,ft=0,st=ok:Garage Multi Sensor
TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=ok:1.0
TSP:MSG:SEND 50-50-0-0 s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=ok:
TSP:MSG:SEND 50-50-0-0 s=3,c=0,t=1,pt=0,l=0,sg=0,ft=0,st=ok:
TSP:MSG:SEND 50-50-0-0 s=10,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=ok:
TSP:MSG:SEND 50-50-0-0 s=11,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=ok:
TSP:MSG:SEND 50-50-0-0 s=3,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=ok:
Request registration...
TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=ok:2
TSP:MSG:READ 0-0-50 s=255,c=3,t=27,pt=1,l=1,sg=0:1
Node registration=1
Init complete, id=50, parent=0, distance=1, registration=1
TSP:MSG:SEND 50-50-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,ft=0,st=ok:0
TSP:MSG:SEND 50-50-0-0 s=3,c=1,t=16,pt=0,l=2,sg=0,ft=0,st=ok:ON
TSP:MSG:SEND 50-50-0-0 s=3,c=1,t=16,pt=0,l=3,sg=0,ft=0,st=ok:OFF
TSP:MSG:READ 0-0-50 s=1,c=1,t=2,pt=0,l=1,sg=0:1
Incoming change for sensor:1, New status: 1
TSP:MSG:SEND 50-50-0-0 s=3,c=1,t=16,pt=0,l=2,sg=0,ft=0,st=ok:ON
TSP:MSG:READ 0-0-50 s=1,c=1,t=2,pt=0,l=1,sg=0:1
Incoming change for sensor:1, New status: 1
TSP:MSG:SEND 50-50-0-0 s=3,c=1,t=16,pt=0,l=3,sg=0,ft=0,st=ok:OFF

The following shows up for the relay:

And the DHT Data is here:

But I cannot find the PIR or Reed switch anywhere in the HASS configuration. Any help would be greatly appreciated! Thank you.

Just a few observations:

  • You’re assigning the same child ID for CHILD_ID_PIR and CHILD_ID_SW. That might confuse HASS.
  • The documentation recommends to send an initial value to the gateway in the loop function. In your sketch you may implicitly be doing that, but I am not sure what the effect looks like if HASS does not receive an initial value.
1 Like

@exxamalte is correct. Fix your conflicting child ids. All devices/sensors need to send at least one value during the loop phase of operation, ie after presentation, to be added in home assistant. Also always feedback state changes from devices to home assistant. Ie send a message in the receive function when the relay state changes.

martin, you say:

always feedback state changes from devices to home assistant

but why?
hass has already turned on the switch at the time you send a switch change.
if the arduino sends back the feedback hass doesnt do anything with it.
if there was a setting in hass what said “confirmed” it would be a nice extra.
but now all it does is send data in the air and keep my gateway busy (which is busy enough already)

or am i missing something?

Home assistant can’t be sure of the device state without feedback.

switches have only 2 states: on of off.
there is no unsure setting.

hass doesnt do anything with the feedback at all.
hass doesnt check if the feedback gets there or not.
so even if you make a feedback function in your arduino, hass and you will never know if the feedback arrived.
i had feedback for my relayswitches. but that doesnt work. sometimes the command doesnt arrive at the arduino (can be by many reasons ) and sometimes the feedback doesnt come back (can also be for many reasons)
it resulted in that the switch in hass sometimes wasnt in the same state as on the arduino.

the only way to be sure is if you do something with the feedback.
i make extra binary sensors for the feedback, just to see the feedback and to see if the feedback arrivés.
an automation then regularly checks if the switch and the sensor have the same state.
just to make sure hass has the same setting as my arduino.

Since the update message might not reach the destination, home assistant can’t be sure that its requested change have reached the device and changed the state at the device until the device responds with a message with updated state. So home assistant will not change the state of the device, in the state machine of home assistant, until feedback from the device is received. This is the default setting for mysensors actuators in home assistant. You can turn this off using the optimistic setting in the config.

setting optimistic: true turns that off?

because i had it Always on true for the ir remote sketch.
from the first moment i started with hass.

that explains a lot. :wink:

i guess i have to set it to false then, saves me a lot of trouble :wink:

thanks.