Not all Relais visible in Domoticz
-
I started a project with 5 relays and 2 binary buttons. In Domotics 3 of the 5 relays pops up including the 2 binary buttons. Why not 5 relays?
// 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> #include <Bounce2.h> #define RELAY_1 A0 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 5 // 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 CHILD_ID_IRBIN 3 #define CHILD_ID_IRBUI 4 #define IRBIN_PIN 3 // Arduino Digital I/O pin for button/reed switch #define IRBUI_PIN 4 Bounce debouncerIRBIN = Bounce(); Bounce debouncerIRBUI = Bounce(); int oldValue_IRBIN = -1; int oldValue_IRBUI = -1; MyMessage msgIRBIN(CHILD_ID_IRBIN, V_TRIPPED); MyMessage msgIRBUI(CHILD_ID_IRBUI, V_TRIPPED); 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() { // Setup the button1 pinMode(IRBIN_PIN, INPUT); debouncerIRBIN.attach(IRBIN_PIN); debouncerIRBIN.interval(5); digitalWrite(IRBIN_PIN, HIGH); // Setup the button2 pinMode(IRBUI_PIN, INPUT); debouncerIRBUI.attach(IRBUI_PIN); debouncerIRBUI.interval(5); digitalWrite(IRBUI_PIN, HIGH); } void presentation() { // Register binary input sensor to gw (they will be created as child devices) // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. present(CHILD_ID_IRBIN, S_DOOR); present(CHILD_ID_IRBUI, S_DOOR); 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); } } void loop() { debouncerIRBIN.update(); // Get the update value int valueIRBIN = debouncerIRBIN.read(); if (valueIRBIN != oldValue_IRBIN) { // Send in the new value send(msgIRBIN.set(valueIRBIN == HIGH ? 1 : 0)); oldValue_IRBIN = valueIRBIN; } debouncerIRBUI.update(); // Get the update value int valueIRBUI = debouncerIRBUI.read(); if (valueIRBUI != oldValue_IRBUI) { // Send in the new value send(msgIRBUI.set(valueIRBUI == HIGH ? 1 : 0)); oldValue_IRBUI = valueIRBUI; } } 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+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()); } }
-
For me, sometimes Domoticz does not show a switch until it gets a value, even though the switch was presented. I add a small routine to Loop() with a flag so it only runs the first time after startup. Then i send the default value for each switch, Usually Off. It is not very elegant, but seems to work.
If (startup) {
Send initial value for each switch/relay
startup=false;
}
-
@nagelc
Thanks for the reply but I am not that experience. I must add your script to the "LOOP" but doing that I get a message "startup" was not declared in the scope. Can you show me was to declair?
-
@dick add
static bool startup = true;
before the If (startup) line.
You might also need to add delay(100) after each present and send, to avoid stressing the radio and power source too much.
-
@mfalkvidd Exactly. Thanks for clarifying.
-
@mfalkvidd I tested the solution and the LOOP code looks like this
but having an error during compiling ("IF" was not declared in this scope). how can I fix that?void loop() { debouncerIRBIN.update(); // Get the update value int valueIRBIN = debouncerIRBIN.read(); if (valueIRBIN != oldValue_IRBIN) { // Send in the new value send(msgIRBIN.set(valueIRBIN == HIGH ? 1 : 0)); oldValue_IRBIN = valueIRBIN; } debouncerIRBUI.update(); // Get the update value int valueIRBUI = debouncerIRBUI.read(); if (valueIRBUI != oldValue_IRBUI) { // Send in the new value send(msgIRBUI.set(valueIRBUI == HIGH ? 1 : 0)); oldValue_IRBUI = valueIRBUI; } static bool startup = true; If (startup) { Send initial value for each switch/relay startup=false; } }```
-
@dick if needs to be lowercase. Change "If" to "if".
-
@mfalkvidd thanks for your support and no errors anymore. Unfortunately it did not work forme. probably I split my sensors in to, one with the relais switches and 1 with the binary buttons. I think that is the only way to continue right now.
-
Another thing to try. Try changing the button IDs to 6 and 7.
You define the button child IDs as 3 and 4.
The relays are presented with child ID's 1 through 5 by the loop in Presentation(). So the buttons have the same child IDs as two of the relays. Some sensors can have the same child ID, like temperature and humidity, but I don't know about S_DOOR and S_BINARY.Also, delete this code i suggested. It needs some tweaking and I suspect the issue is really the overlap of the child IDs:
static bool startup = true;
If (startup) {
Send initial value for each switch/relay
startup=false;
}