Switch mysensors does not work

I do not know how to explain my problem, since home assistant does not work the swich, I detect the states, since google assistant through home assistant works very well, but if I click on home assistant there is no way to turn on a light.

mysensors:
  gateways:
    - device: !secret ip_local
      persistence_file: '/home/pi/.homeassistant/mysensors3.json'
      tcp_port: 5003
  optimistic: false
  persistence: true
  version: '2.0'

I’m desperate, in domoticz it works perfect

I have an RGB Light strip setup with MySensors works fine. What does your error log say?

no, it’s a S_LIGHT / S_BINARY
the problem that I see is that it works well less from the dashboard for Homeassistant, otherwise it works perfect.
from the active dashboard the swich and it goes off but the light is still on and I turn it off from the physical switch or from google assistant integrated in homeassistant.

sorry for my English

Will need to see your sketch to help more. Likely missing the receive message handler.

Right now I do not have it. but it’s very weird
I already commented that by voice in home assistant works well.
And in Domoticz it works very well.
In a few hours I upload the sketch.

Are you using the lovelace UI?

no, I try it and now I say

With Lovelace UI it does not work.

my sketch:

#define BUTTON_PIN_1                3                      // Arduino Digital I/O pin number for button 
#define RELAY_PIN_1                 A1                      // Arduino Digital I/O pin number for relay 
#define BUTTON_PIN_2                6                      // Arduino Digital I/O pin number for button 
#define RELAY_PIN_2                 A3                      // Arduino Digital I/O pin number for relay 

/**** MySensor configuration ****/
#define MY_BAUD_RATE                9600                   // Serial output baud rate
#define MY_TRANSPORT_WAIT_READY_MS  1 //3000 //1               // Establezca cuánto tiempo esperar para que el transporte esté listo en milisegundos
#define MY_RADIO_NRF24                                     // Enable and select radio type attached
//#define MY_RF24_PA_LEVEL RF24_PA_LOW
#define MY_REPEATER_FEATURE                                // Habilitar la funcionalidad de repetidor para este nodo

#define MS_BOARD_NAME               "Control Pruevas v1"         // Nombre del Sketch
#define MS_SOFTWARE_VERSION         "1.1"                  // Version del Sketch
#define MS_RELE_CHILD_ID_1          1                      // Id del sensor child para el 1º relay
#define MS_RELE_CHILD_ID_2          2                      // Id del sensor child para el 2º relay

// Activar o desactivar sensores
#define MY_DEBUG                                           // Habilitar la depuracion en el serial monitor
#define SERIAL_DEBUG                                       // Habilita sensores en el serial monitor
#define USE_TEMP_SENSOR                                    // Habilitar el sensor de temperatura interna
#define USE_BUTTON_SENSOR                                  // Lo are para la version 3 del Sketch -- #ifdef USE_BUTTON_SENSOR #endif
// ------------------------

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

#ifdef USE_TEMP_SENSOR
  #define pDS18B20                  A2                      // Arduino Digital I/O pin number para el sensor de temperatura interno
  #define MS_TEMP_CHILD_ID_1        0                       // Id del sensor child para la temperatura
  #include <OneWire.h>
  #include <DallasTemperature.h>
#endif

// Estados de los Reles
#define RELAY_ON                    1
#define RELAY_OFF                   0

// Fuente de cambio de estado (utilizada al imprimir información de depuración)
#define CHANGE_STATE_SOURCE_RADIO   0
#define CHANGE_STATE_SOURCE_SWITCH  1

Bounce debouncer1 = Bounce();
int oldValue1;
bool state1;
Bounce debouncer2 = Bounce();
int oldValue2;
bool state2;

MyMessage msgR1(MS_RELE_CHILD_ID_1,   V_LIGHT);
MyMessage msgR2(MS_RELE_CHILD_ID_2,   V_LIGHT);

#ifdef USE_TEMP_SENSOR
  MyMessage msgT1(MS_TEMP_CHILD_ID_1, V_TEMP);
  OneWire oneWire(pDS18B20);
  DallasTemperature ds18b20(&oneWire);
#endif

void presentation() {
  // Enviar la informacion del sketch al gateway
  sendSketchInfo(MS_BOARD_NAME,     MS_SOFTWARE_VERSION);
  present(MS_RELE_CHILD_ID_1,       S_LIGHT,             "Rele1");
  present(MS_RELE_CHILD_ID_2,       S_LIGHT,             "Rele2");
  #ifdef USE_TEMP_SENSOR
    present(MS_TEMP_CHILD_ID_1,     S_TEMP,              "Temperatura interna");
  #endif
}

void setup()
{
  #ifdef USE_TEMP_SENSOR
    ds18b20.begin();
    ds18b20.setWaitForConversion(false);
  #endif

  #ifdef USE_BUTTON_SENSOR 
    // Configuracion del Boton y Activar el internal pull-up
    pinMode(BUTTON_PIN_1, INPUT_PULLUP);            //_PULLUP
    pinMode(BUTTON_PIN_2, INPUT_PULLUP);            //_PULLUP

    // Después de configurar el botón, se configura el debouncer del boton
    debouncer1.attach(BUTTON_PIN_1);
    debouncer1.interval(5);
    
    debouncer2.attach(BUTTON_PIN_2);
    debouncer2.interval(5);
    
    debouncer1.update();
    debouncer2.update();
    oldValue1 = debouncer1.read();
    oldValue2 = debouncer2.read();
  #endif
  
  setRelayState(RELAY_PIN_1, RELAY_OFF);            // Make sure relays are off when starting up
  pinMode(RELAY_PIN_1, OUTPUT);                     // Then set relay pins in output mode

  setRelayState(RELAY_PIN_2, RELAY_OFF);            // Make sure relays are off when starting up
  pinMode(RELAY_PIN_2, OUTPUT);                     // Then set relay pins in output mode
  
  state1 = loadState(MS_RELE_CHILD_ID_1);           // Set relay to last known state (using eeprom storage)
  setRelayState(RELAY_PIN_1, state1);               // Make sure relays

  state2 = loadState(MS_RELE_CHILD_ID_2);           // Set relay to last known state (using eeprom storage)
  setRelayState(RELAY_PIN_2, state2);               // Make sure relays
}

void loop()
{
  #ifdef USE_TEMP_SENSOR
    static float prevTemp = 0;
  #endif

  #ifdef USE_BUTTON_SENSOR
    debouncer1.update(); 
    int value1 = debouncer1.read();

    if (value1 != oldValue1 && value1 == HIGH) {                                   // Quitar o poner al final && value==0 para poner interruptor o pulsador === if (value != oldValue && value==0), para touch es (value != oldValue && value == HIGH )
      state1 =  !state1;                                                           // Toggle the state
      send(msgR1.set(state1), false);                                                // enviar nuevo estado al controlador, no se solicitó ack
      setRelayState(RELAY_PIN_1, state1);                                          // switch the relay to the new state
      saveState(MS_RELE_CHILD_ID_1, state1);                                       // Store state in eeprom
      // Write some debug info
      printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, MS_RELE_CHILD_ID_1, value1);
    }
    oldValue1 = value1;

    debouncer2.update();
    int value2 = debouncer2.read();
    if (value2 != oldValue2 && value2 == HIGH) {                                   // Quitar o poner al final && value==0 para poner interruptor o pulsador === if (value != oldValue && value==0), para touch es (value != oldValue && value == HIGH )
      state2 =  !state2;                                                           // Toggle the state
      send(msgR2.set(state2), false);                                                // enviar nuevo estado al controlador, no se solicitó ack
      setRelayState(RELAY_PIN_2, state2);                                          // switch the relay to the new state
      saveState(MS_RELE_CHILD_ID_2, state2);                                       // Store state in eeprom
      // Write some debug info
      printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, MS_RELE_CHILD_ID_2, value2);
    }
    oldValue2 = value2;
    
  #endif

  // Fetch temperatures from Dallas sensors
  #ifdef USE_TEMP_SENSOR
    ds18b20.requestTemperatures();
    // Fetch and round temperature to one decimal
    float temperature = static_cast<float>(static_cast<int>(ds18b20.getTempCByIndex(0)));  
    if (temperature != -127.00 && temperature != 85.00 && prevTemp != temperature) {
      send(msgT1.setSensor(0).set(ds18b20.getTempCByIndex(0),1));                // Envio la nueva temperatura
      #ifdef SERIAL_DEBUG
        Serial.print("Temperatura interna: ");
        Serial.println(ds18b20.getTempCByIndex(0),1);
      #endif
      prevTemp = temperature;
    }
  #endif
}

void receive(const MyMessage &message) { 
  if (message.type == V_STATUS) {
    switch (message.sensor) {
      case MS_RELE_CHILD_ID_1:    
        state1 = message.getBool();                 
        setRelayState(RELAY_PIN_1, state1);                                             // Cambia el estado del rele
        saveState(MS_RELE_CHILD_ID_1, state1);                                          // Almacena el estado en la eeprom
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, MS_RELE_CHILD_ID_1, state1);  // Escribe la informacion en el debug
      break; 
      case MS_RELE_CHILD_ID_2:    
        state2 = message.getBool();                 
        setRelayState(RELAY_PIN_2, state2);                                             // Cambia el estado del rele
        saveState(MS_RELE_CHILD_ID_2, state2);                                          // Almacena el estado en la eeprom
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, MS_RELE_CHILD_ID_2, state2);  // Escribe la informacion en el debug
      break; 
    }
  }
}

// Set status of a relay pin
void setRelayState(byte relayPin, bool value) {
  digitalWrite(relayPin, value ? RELAY_ON : RELAY_OFF);
}

// Print debug info, centralized in one place to minimize memory usage and have only one #ifdef MY_DEBUG for all state change messages
void printStateChangedDebug(int source, int sensorID, bool value) {
  #ifdef SERIAL_DEBUG
    Serial.print(F("Sensor cambiado por:  "));
    Serial.print(source == CHANGE_STATE_SOURCE_RADIO ? F("Cambio por radio") : F("Cambio físico"));
    Serial.print(F(", Sensor nº: "));
    Serial.println(sensorID);
  #endif

Shouldn’t this:

  present(MS_RELE_CHILD_ID_1,       S_LIGHT,             "Rele1");
  present(MS_RELE_CHILD_ID_2,       S_LIGHT,             "Rele2");

be this:

  present(MS_RELE_CHILD_ID_1,       S_BINARY,             "Rele1");
  present(MS_RELE_CHILD_ID_2,       S_BINARY,             "Rele2");

I don’t see a S_LIGHT in the Serial API.

As I understand it the MySensors library is strict on matching types.

no, because it is a relay, and it works in other systems.
it also works well internally in home assistant with

tts:
  - platform: google
    language: 'es'
    cache: true
    cache_dir: /tmp/tts
    time_memory: 300
    base_url:

S_LIGHT doesn’t exist in the API per the documents:

For a relay (on/off) you’d use S_BINARY this is likely why the UI isn’t picking it up, @martinhjelmare does a lot of the MySensors bit and maybe able to shed more light on this.

Yes, I’m going to try it, but it’s funny that the home-run tts and from a switch detects it but does not send it

nothing, it does not work, it detects it well, but since the dashboard does not work

What do you get from the node’s debug when you attempt to toggle it via HomeAssistant?

In the debug when he acted on the switch nothing appears

That’s odd, what kind of gateway are you using?

mysensors:
  gateways:
    - device: !secret ip_local
      persistence_file: '/home/pi/.homeassistant/mysensors3.json'
      tcp_port: 5003
  optimistic: false
  persistence: true
  version: '2.0'

I do not know if you mean this.
If it is very rare, and updated to version 2.3.1 (mysensors) and remains the same

Ok you’re using ethernet gateway just like I am, so if your node isn’t receiving the “turn on” command from HomeAssistant something isn’t communicating properly.

You may need to enable mysensors logging in HA to get more information.

add the following to configuration.yaml (or add it under your existing logger config):

logger:
  logs:
    homeassistant.components.mysensors: debug

This will dump more info into the log file when MySensors is used, then toggle via HA and we can see what’s going on.

Yes, as I mentioned when I activate or deactivate it from the dashboard in the debug it does not show anything.
In case it works when I activate or deactivate it depending on the state it automatically returns to its state and shows nothing in the debug.
I’ll try if you do it with automations and then I’ll tell you

Also just noticed:

MyMessage msgR1(MS_RELE_CHILD_ID_1,   V_LIGHT);
MyMessage msgR2(MS_RELE_CHILD_ID_2,   V_LIGHT);

should be:

MyMessage msgR1(MS_RELE_CHILD_ID_1,   V_STATUS);
MyMessage msgR2(MS_RELE_CHILD_ID_2,   V_STATUS);