Mysensors Serial Gateway

Hello.
I build mysensors gateway on arduino Uno connected via USB to HA.

I want use gateway with 4 relays and 4 buttons.

I make sketch with added option to save state of relays in eprom for safety when RPI with HA dead.

Everything (almost) working but HA logs show a big list of errors when i try change switch in HA dashboard.

When i change with button everything looks fine (Relay change state, HA show change).

Maybe someone can help me with this errors?

Here a Mysensors sketch:

// *** 4 Relays serial Gateway sketch

// Enable debug prints to serial monitor
#define MY_DEBUG 
// Enable serial gateway 
#define MY_GATEWAY_SERIAL

// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway 
#define MY_INCLUSION_BUTTON_FEATURE

// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP

// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60 
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN  3 

#include <SPI.h>
#include <MySensors.h>  
#include <Bounce2.h>

// Enable repeater functionality for this node
//#define MY_REPEATER_FEATURE


#define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_2  5
#define RELAY_3  6
#define RELAY_4  7
#define NUMBER_OF_RELAYS 4 // Total number of attached relays
#define RELAY_ON 0  // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay

#define BUTTON_PIN A1
#define BUTTON2_PIN A2
#define BUTTON3_PIN A3
#define BUTTON4_PIN A4

void before() { 
  for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);   
    // Set relay to last known state (using eeprom storage) 
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
  }
}
Bounce debouncer = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();

void setup() { 
  // Setup locally attached sensors
  delay(50000);
   // Setup the button.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON2_PIN, INPUT_PULLUP);
  pinMode(BUTTON3_PIN, INPUT_PULLUP);
  pinMode(BUTTON4_PIN, INPUT_PULLUP);

  // After setting up the button, setup debouncer.
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5);
  debouncer2.attach(BUTTON2_PIN);
  debouncer2.interval(5);
  debouncer3.attach(BUTTON3_PIN);
  debouncer3.interval(5);
  debouncer4.attach(BUTTON4_PIN);
  debouncer4.interval(5);

  //presentation();
}
void presentation()  
{   
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Relay", "1.0");

  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)
    present(sensor, S_BINARY);
  }
}

MyMessage msg(1, V_STATUS);
MyMessage msg2(2, V_STATUS);
MyMessage msg3(3, V_STATUS);
MyMessage msg4(4, V_STATUS);

void loop() { 
  // Send locally attached sensor data here 
  if (debouncer.update()) {
    // Get the update value.
    int value = debouncer.read();
    // Send in the new value.
    if(value == HIGH){
         saveState(1, !loadState(1));
         digitalWrite(RELAY_1, loadState(1)?RELAY_ON:RELAY_OFF);
         send(msg.set(loadState(1)));
         }
  }
  if (debouncer2.update()) {
      int value2 = debouncer2.read();
    if(value2 == HIGH){
         saveState(2, !loadState(2));
         digitalWrite(RELAY_2, loadState(2)?RELAY_ON:RELAY_OFF);
         send(msg2.set(loadState(2)));
         }
  }
  if (debouncer3.update()) {
      int value3 = debouncer3.read();
    if(value3 == HIGH){
         saveState(3, !loadState(3));
         digitalWrite(RELAY_3, loadState(3)?RELAY_ON:RELAY_OFF);
         send(msg3.set(loadState(3)));
         }
  }
   if (debouncer4.update()) {
      int value4 = debouncer4.read();
    if(value4 == HIGH){
          saveState(4, !loadState(4));
         digitalWrite(RELAY_4, loadState(4)?RELAY_ON:RELAY_OFF);
         send(msg4.set(loadState(4)));
         }       
   }
}


 void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_STATUS && message.sender == 0) {
     // Change relay state
     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
     // 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());
   } 
}

It would help to see the log with the errors.

My guess is the errors are due to the debug prints to serial that are done in the receive function in your sketch. Since this is a serial gateway, everything printed on the serial connection will be expected by home assistant to be messages following the mysensors serial protocol. The debug prints obviously don’t follow the mysensors serial protocol.

Either remove the debug prints or ignore the errors/warnings.

It’s also recommended to feedback any state changes in the receive function to home assistant, ie use the send method to send the new state. That way home assistant will know that the message was received by the device.

Here is my error log when i switch 4 relay:
https://pastebin.com/32b5W1Y0

Second.
I try change type from Switch to LIGHT
So i need add S_DIMMER, V_PERCENTAGE and V_STATUS.
But i have problem with V_PERCENTAGE. I don’t have dimmer lights so dim need to be 100.
Where i need declare it?

Yeah, the warnings in the log are due to the debug prints in the receive function as I expected.

Send an initial value for V_PERCENTAGE, at least once, at some point in the loop.