MySensors Dimmers and Relays not in entity list

Hello!

I am using the serial MySensors Gateway.

All the input like temp sensors, buttons, and PIR shows up in the entity list.
None of the Relay output and Dimmers show up.

Is there anybody that can point me in the correct direction?

Maybe you have to send at least one value per V_TYPE, as stated here

Hey @emurr,

I have both working with no issue.

Can you share the sketch you’re using?

Hello!

Solved. I did not send a initial value.
Thanks for a very rapid resonse!

This is looking very promising!

Home Assistant - MySensors v2 - Arduino Serial Gateway
Relay Actuator: 4 Channel Opto Relay Module

Working Code

// Setting for Manual Node ID to 2
#define MY_NODE_ID 2
// Enable debug prints to serial monitor
#define MY_DEBUG 
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 4 // Total number of attached relays
// Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
#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
bool initialValueSent = false;
MyMessage msg1(1, V_LIGHT);
MyMessage msg2(2, V_LIGHT);
MyMessage msg3(3, V_LIGHT);
MyMessage msg4(4, V_LIGHT);
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);
  }
}
void setup() {
  
}
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_LIGHT);
  }
}
void loop() 
{
  if (!initialValueSent) {
    Serial.println("Sending initial value");
    send(msg1.set(loadState(1)?RELAY_OFF:RELAY_ON));
    send(msg2.set(loadState(2)?RELAY_OFF:RELAY_ON));
    send(msg3.set(loadState(3)?RELAY_OFF:RELAY_ON));
    send(msg4.set(loadState(4)?RELAY_OFF:RELAY_ON));
    initialValueSent = true;
    Serial.println("Sending initial value: Completed");
  }
}
void receive(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
     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());
   } 
}

I am a newbie in IoT and Home Automation getting started with Home Assistant and MySensors
This is my first post in this forum.
I had a tough time to get this working.
Sharing the full code here so that it may be helpful to any one experiencing similar issue.

Thanks.

3 Likes

I suggest adding feedback of state change in the receive function, ie add a call to send(...) in the receive function after changing the state of a relay and send in the new value to the gateway.

Also, in your current sketch, you can’t be sure that the message with initial value was received by the gateway. To be sure, request the value in the loop after sending it in and wait a bit, and set initialValueSent to true in receive when you get the reply from the gateway. See my example here:
https://forum.mysensors.org/topic/4009/can-t-see-my-nodes-on-ha-web-interface/20

Maybe even simpler would be to set ack to true in the call to send when sending in the initial values. Then set initialValueSent to true when the ack message from the gateway comes in, in receive function.

1 Like

Much appreciated your suggestions @martinhjelmare

I made the modifications as you suggested. Its working excellent now!!

Here is HA yaml Configuration for Arduino Serial Gateway

mysensors:
gateways:
- device: ‘/dev/ttyUSB0’
persistence_file: ‘/home/hass/.homeassistant/mysensors.json’
baud_rate: 115200
debug: true
persistence: false //Need to set to ‘true’ to persist the sensor Entities in HA
optimistic: false

Here is the Modified Arduino Sketch for 4 Channel Relay

// Override Setting for Manual Node ID to 2
#define MY_NODE_ID 2

// Enable debug prints to serial monitor
#define MY_DEBUG 

// Enable and select radio type attached
#define MY_RADIO_NRF24

// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE

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

#define RELAY_1  3          // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 4  // Total number of attached relays: 4

// Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
#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

bool initialValueSent = false;

//Init MyMessage for Each Child ID
MyMessage msg1(1, V_LIGHT);
MyMessage msg2(2, V_LIGHT);
MyMessage msg3(3, V_LIGHT);
MyMessage msg4(4, V_LIGHT);

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);
  }
}

void setup() {
  
}

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_LIGHT);
  }
}


void loop() 
{
  if (!initialValueSent) {
    Serial.println("Sending initial value");
    send(msg1.set(loadState(1)?RELAY_OFF:RELAY_ON),true);
    wait(1000);
    send(msg2.set(loadState(2)?RELAY_OFF:RELAY_ON),true);
    wait(1000);
    send(msg3.set(loadState(3)?RELAY_OFF:RELAY_ON),true);
    wait(1000);
    send(msg4.set(loadState(4)?RELAY_OFF:RELAY_ON),true);
    wait(1000);
    Serial.println("Sending initial value: Completed");
    wait(5000);
  }
}

void receive(const MyMessage &message) {
  Serial.println("=============== Receive Start =======================");
  if (message.isAck()) {
     Serial.println(">>>>> ACK <<<<<");
     Serial.println("This is an ack from gateway");
     Serial.println("<<<<<< ACK >>>>>>");
  }
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LIGHT) {
    Serial.println(">>>>> V_LIGHT <<<<<");
    if (!initialValueSent) {
      Serial.println("Receiving initial value from controller");
      initialValueSent = true;
    }
     // Update relay state to HA
     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
     switch (message.sensor) {
        case 1:
          Serial.print("Incoming change for sensor 1");
          send(msg1.set(message.getBool()?RELAY_OFF:RELAY_ON));
          break;
        case 2:
          Serial.print("Incoming change for sensor 2");
          send(msg2.set(message.getBool()?RELAY_OFF:RELAY_ON));
          break;
        case 3:
          Serial.print("Incoming change for sensor 3");
          send(msg3.set(message.getBool()?RELAY_OFF:RELAY_ON));
          break;
        case 4:
          Serial.print("Incoming change for sensor 4");
          send(msg4.set(message.getBool()?RELAY_OFF:RELAY_ON));
          break;                    
        default: 
          Serial.println("Default Case: Receiving Other Sensor Child ID");
        break;
     }
     // Store state in Arduino eeprom
     saveState(message.sensor, message.getBool());
     Serial.print("Saved State for sensor: ");
     Serial.print( message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
     Serial.println("<<<<<< V_LIGHT >>>>>>");
   } 
   Serial.println("=============== Receive END =======================");
}

Thanks

3 Likes

Finally migrated my all sensors: mysensors - nrf24l01 to ESPHome - ESP8266