Relay node does not follow UI switch state.



  • Hey guys!

    I'm would like to use Relay node with RS485 serial gateway in HomeAssistant.
    I managed to present node and its childs to gateway and controller using MySensors addon, but unfortunately I'm not able to controll relay from HA UI level as I want.

    Usually node outpud did not react to UI switch change, it sometimes react if I double click the icon, after couple changes node output state is not synchronized to UI switch state. Please check my video

    I tested my setup using MYSController and it worked well, no lags or other issues observed, output states and GW log were always synchronized when using MYSController.

    Do you have any ideas what is wrong ?

    I'm using Home Assistant 2021.8.8, MySensors 2.2.3,

    Gateway code:

    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    
    // Enable RS485 transport layer
    #define MY_RS485
    
    // Define this to enables DE-pin management on defined pin
    #define MY_RS485_DE_PIN 2
    
    // Set RS485 baud rate to use
    #define MY_RS485_BAUD_RATE 9600
    
    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    #include <MySensors.h>
    
    // Initialize general message
    
    void setup()
    {
    	// Setup locally attached sensors
    }
    
    void presentation()
    {
    }
    
    void loop()
    {
    }
    

    Node code:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    #define MY_NODE_ID 2
    #define CHILD_ID 1
    // Enable RS485 transport layer
    #define MY_RS485
    
    // Define this to enables DE-pin management on defined pin
    #define MY_RS485_DE_PIN 2
    
    // Set RS485 baud rate to use
    #define MY_RS485_BAUD_RATE 9600
    // Enable and select radio type attached
    //#define MY_RADIO_RF24
    //#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 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
    #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 state = false;
    
    
    
    void before()
    {
        for (int sensor=1, pin=RELAY_PIN; 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, 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; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Register all sensors to gw (they will be created as child devices)
            present(sensor, S_BINARY);
            MyMessage msg(sensor, V_STATUS);
            send(msg.set(state?RELAY_ON:RELAY_OFF));
        }
    }
    
    
    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, 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());
        }
    }
    

    HomeAssistant log:

    2021-08-22 22:18:45 DEBUG (MainThread) [mysensors.transport] Receiving 2;255;3;0;21;0
    2021-08-22 22:18:45 DEBUG (MainThread) [mysensors.transport] Receiving 2;255;3;0;21;0
    2021-08-22 22:18:50 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:18:50 DEBUG (SyncWorker_7) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:18:53 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:18:53 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;0
    2021-08-22 22:18:53 DEBUG (MainThread) [mysensors.transport] Receiving 2;1;1;1;2;0
    2021-08-22 22:18:53 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 1
    2021-08-22 22:18:53 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 1: value_type 2, value = 0
    2021-08-22 22:18:57 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:18:57 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:18:57 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:18:57 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;0
    2021-08-22 22:18:57 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:18:57 DEBUG (MainThread) [mysensors.transport] Receiving 2;1;1;1;2;1
    2021-08-22 22:18:57 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 1
    2021-08-22 22:18:57 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 1: value_type 2, value = 1
    2021-08-22 22:19:01 DEBUG (SyncWorker_3) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:19:53 INFO (SyncWorker_4) [WazeRouteCalculator.WazeRouteCalculator] From: 49.970564,19.947972 - to: 50.047601,19.957191
    2021-08-22 22:19:54 INFO (SyncWorker_4) [WazeRouteCalculator.WazeRouteCalculator] Time 14.78 - 24.25 minutes, distance 10.65 - 12.07 km.
    2021-08-22 22:20:02 DEBUG (MainThread) [mysensors.transport] Receiving 0;255;3;0;14;Gateway startup complete.
    2021-08-22 22:20:02 INFO (MainThread) [mysensors.handler] n:0 c:255 t:3 s:14 p:Gateway startup complete.
    2021-08-22 22:20:02 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
    2021-08-22 22:20:02 DEBUG (MainThread) [mysensors.transport] Sending 255;255;3;0;20;
    2021-08-22 22:20:02 DEBUG (MainThread) [mysensors.transport] Receiving 0;255;0;0;18;2.3.2
    2021-08-22 22:20:02 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
    2021-08-22 22:20:05 DEBUG (SyncWorker_5) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:20:25 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;0
    2021-08-22 22:20:26 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;0
    2021-08-22 22:20:27 DEBUG (MainThread) [mysensors.transport] Receiving 2;2;1;1;2;0
    2021-08-22 22:20:27 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 2
    2021-08-22 22:20:27 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 2: value_type 2, value = 0
    2021-08-22 22:20:27 DEBUG (SyncWorker_8) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:20:28 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;0
    2021-08-22 22:20:28 DEBUG (MainThread) [mysensors.transport] Receiving 0;255;3;0;14;Gateway startup complete.
    2021-08-22 22:20:28 INFO (MainThread) [mysensors.handler] n:0 c:255 t:3 s:14 p:Gateway startup complete.
    2021-08-22 22:20:28 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
    2021-08-22 22:20:28 DEBUG (MainThread) [mysensors.transport] Sending 255;255;3;0;20;
    2021-08-22 22:20:28 DEBUG (MainThread) [mysensors.transport] Receiving 0;255;0;0;18;2.3.2
    2021-08-22 22:20:28 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
    2021-08-22 22:20:29 DEBUG (MainThread) [mysensors.transport] Receiving 2;255;3;0;21;0
    2021-08-22 22:20:29 DEBUG (MainThread) [mysensors.transport] Receiving 2;255;3;0;21;0
    2021-08-22 22:20:30 DEBUG (MainThread) [mysensors.transport] Sending 2;4;1;1;2;0
    2021-08-22 22:20:33 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;0
    2021-08-22 22:20:33 DEBUG (MainThread) [mysensors.transport] Receiving 2;3;1;1;2;0
    2021-08-22 22:20:33 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 3
    2021-08-22 22:20:34 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 3: value_type 2, value = 0
    2021-08-22 22:20:36 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;0
    2021-08-22 22:20:37 DEBUG (MainThread) [mysensors.transport] Sending 2;4;1;1;2;0
    2021-08-22 22:20:37 DEBUG (SyncWorker_6) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:20:41 DEBUG (MainThread) [mysensors.transport] Sending 2;4;1;1;2;0
    2021-08-22 22:20:41 DEBUG (MainThread) [mysensors.transport] Sending 2;4;1;1;2;1
    2021-08-22 22:20:42 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;0
    2021-08-22 22:20:42 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;1
    2021-08-22 22:20:42 DEBUG (MainThread) [mysensors.transport] Receiving 2;1;1;1;2;1
    2021-08-22 22:20:42 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 1
    2021-08-22 22:20:42 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 1: value_type 2, value = 1
    2021-08-22 22:20:43 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;1
    2021-08-22 22:20:43 DEBUG (MainThread) [mysensors.transport] Receiving 2;2;1;1;2;1
    2021-08-22 22:20:43 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 2
    2021-08-22 22:20:44 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 2: value_type 2, value = 1
    2021-08-22 22:20:44 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;0
    2021-08-22 22:20:44 DEBUG (MainThread) [mysensors.transport] Receiving 2;2;1;1;2;0
    2021-08-22 22:20:44 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 2
    2021-08-22 22:20:44 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 2: value_type 2, value = 0
    2021-08-22 22:20:45 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;1
    2021-08-22 22:20:45 DEBUG (MainThread) [mysensors.transport] Receiving 2;3;1;1;2;1
    2021-08-22 22:20:45 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 3
    2021-08-22 22:20:46 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 3: value_type 2, value = 1
    2021-08-22 22:20:46 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;0
    2021-08-22 22:20:46 DEBUG (MainThread) [mysensors.transport] Receiving 2;3;1;1;2;0
    2021-08-22 22:20:46 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 3
    2021-08-22 22:20:46 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 3: value_type 2, value = 0
    2021-08-22 22:20:50 DEBUG (SyncWorker_7) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:20:50 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;1
    2021-08-22 22:20:50 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;1
    2021-08-22 22:20:50 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;0
    2021-08-22 22:20:50 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;1
    2021-08-22 22:20:51 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;0
    2021-08-22 22:20:51 DEBUG (MainThread) [mysensors.transport] Receiving 2;3;1;1;2;0
    2021-08-22 22:20:51 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 3
    2021-08-22 22:20:51 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 3: value_type 2, value = 0
    2021-08-22 22:20:51 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;1
    2021-08-22 22:20:51 DEBUG (MainThread) [mysensors.transport] Receiving 2;3;1;1;2;1
    2021-08-22 22:20:51 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 3
    2021-08-22 22:20:51 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: Relay 2 3: value_type 2, value = 1
    2021-08-22 22:20:54 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;1
    2021-08-22 22:20:54 DEBUG (MainThread) [mysensors.transport] Receiving 0;255;3;0;14;Gateway startup complete.
    2021-08-22 22:20:54 INFO (MainThread) [mysensors.handler] n:0 c:255 t:3 s:14 p:Gateway startup complete.
    2021-08-22 22:20:54 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
    2021-08-22 22:20:54 DEBUG (MainThread) [mysensors.transport] Sending 255;255;3;0;20;
    2021-08-22 22:20:54 DEBUG (MainThread) [mysensors.transport] Receiving 0;255;0;0;18;2.3.2
    2021-08-22 22:20:54 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
    2021-08-22 22:20:54 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;0
    2021-08-22 22:20:58 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;1
    2021-08-22 22:20:58 DEBUG (MainThread) [mysensors.transport] Sending 2;2;1;1;2;1
    2021-08-22 22:21:00 DEBUG (SyncWorker_5) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_1807bac09b2c92dd559b8aff02414565.json
    2021-08-22 22:21:01 DEBUG (MainThread) [mysensors.transport] Sending 2;1;1;1;2;0
    2021-08-22 22:21:02 DEBUG (MainThread) [mysensors.transport] Sending 2;3;1;1;2;0
    2021-08-22 22:21:06 DEBUG (MainThread) [mysensors.transport] Sending 2;4;1;1;2;0
    


  • @kacper_zet Try moving this code from receive to void loop and add a check to see if the status has changed from it's last state. Maybe then it will work for you.

     digitalWrite(message.getSensor()-1+RELAY_PIN, 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());
    

    If not then it might be HA relsted and I can't help with that.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts