cant add two relays in the Home assistant
-
I need to add two basic relays to the home assistant with Mysensors .but unfortunately I can add only one relay please help me
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_NRF5_ESB //#define MY_RADIO_RFM69 //#define MY_RADIO_RFM95 // Enable repeater functionality for this node #define MY_REPEATER_FEATURE #include <MySensors.h> #define RELAY_PIN_1 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // 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 void before() { for (int sensor=1, pin=RELAY_PIN_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_PIN_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_BINARY); } } void loop() { } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.getType()==V_STATUS) { // Change relay state digitalWrite(message.getSensor()-1+RELAY_PIN_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom saveState(message.getSensor(), message.getBool()); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.getSensor()); Serial.print(", New status: "); Serial.println(message.getBool()); } }
-
Hello
It could be a case of presentation being done too fast. Add a wait(50) like this// Present all relays to controller for (int sensor=CHILD_ID_RELAY, pin=RELAY_1; sensor<CHILD_ID_RELAY+NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_LIGHT); wait( 50 ); // <--- byTheo
-
I believe you have to send a state also, to have it show up in home assistant.
3 out of 3
Suggested Topics
-
Day 1 - Status report
Announcements • 23 Mar 2014, 22:45 • hek 24 Mar 2014, 20:12 -
Raspberry Pi 5: invalid GPIO 9
Troubleshooting • 27 Aug 2024, 13:20 • igo 27 Aug 2024, 13:20 -
Compiling Sensor code using BME280 and ESP8266
Troubleshooting • 26 Feb 2025, 00:32 • dpcons 26 Feb 2025, 06:22 -
JSN-SR04T-V3.0 Coax cable extended
Troubleshooting • 10 days ago • bocalexandru 9 days ago -
Ghost Child
Troubleshooting • 15 days ago • FcNanoLed 12 days ago -
JSN SR04T - Temperature Influencing Readings
Troubleshooting • 6 Sept 2019, 07:51 • Timbergetter 2 days ago