Loosing relays state after OH restart



  • Hi. I connect Arduino with MySensors to server machine with linux by serial gateway. Everything is working fine, but when I restart OH process, or machine, all relays are turning off. When i had only lights it wan snot big problem. But now i have some important things that i really won't turning off. Is there possibility to keep relays status when OH is restarting?

    My codes:
    things

    Bridge mysensors:bridge-ser:gateway3 "Gateway Przekaźniki RelPol" [ serialPort="/dev/ttyACM4", sendDelay=50, networkSanCheckEnabled=true ]
    {
      light relay_0 'przekaźnik_00' [ nodeId=0, childId=0 ]
      light relay_1 'przekaźnik_01' [ nodeId=0, childId=1 ]
      light relay_2 'przekaźnik_02' [ nodeId=0, childId=2 ]
      light relay_3 'przekaźnik_03' [ nodeId=0, childId=3 ]
      light relay_4 'przekaźnik_04' [ nodeId=0, childId=4 ]
      light relay_5 'przekaźnik_05' [ nodeId=0, childId=5 ]
    }
    

    items

    Switch relay_0 "stan" (relpols) { channel="mysensors:light:gateway3:relay_0:status" }
    Switch relay_1 "stan" (relpols) { channel="mysensors:light:gateway3:relay_1:status" }
    Switch relay_2 "stan" (relpols) { channel="mysensors:light:gateway3:relay_2:status" }
    Switch relay_3 "stan" (relpols) { channel="mysensors:light:gateway3:relay_3:status" }
    Switch relay_4 "stan" (relpols) { channel="mysensors:light:gateway3:relay_4:status" }
    Switch relay_5 "stan" (relpols) { channel="mysensors:light:gateway3:relay_5:status" }
    

    arduino

    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    
    // Enable and select radio type attached
    //#define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level.
    //#define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    // Define a lower baud rate for Arduinos running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    #if F_CPU == 8000000L
    #define MY_BAUD_RATE 38400
    #endif
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    
    // Inverses behavior of inclusion button (if using external pullup)
    //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
    
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3
    
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    #define MY_NODE_ID 1
    
    // Inverses the behavior of leds
    //#define MY_WITH_LEDS_BLINKING_INVERSE
    
    // Flash leds on rx/tx/err
    // Uncomment to override default HW configurations
    #define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    
    #include <MySensors.h>
    
    class Relay {
      const int pin;
      int state;
    
      public:
        Relay(int attachTo) :
          pin(attachTo)
        {
        }
    
        void setup() {
          pinMode(pin, OUTPUT);
        }
        void on() {
          state = 1;
        }
        void off() {
          state = 0;
        }
        void setState() {
          digitalWrite(pin, state);
        }
    };
    
    
    Relay relay0(8);
    Relay relay1(9);
    Relay relay2(10);
    Relay relay3(11);
    Relay relay4(12);
    
    
    Relay relays[] = {relay0, relay1, relay2, relay3, relay4,};
    
    void presentation () {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("relays", "0.4");
    
      for(int i = 0; i < 5; i++){
        present(i, S_BINARY);
      }
    }
    
    void setup()
    {
      Serial.begin(115200);
    
    	presentation();
    
      for(int i = 0; i < 5; i++){
        relays[i].setup();
      }
    }
    
    void loop()
    {
      for(int i = 0; i < 5; i++){
        relays[i].setState();
      }
      wait(20);
    }
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
        if(message.type==V_LIGHT && message.getInt() == 0){
          relays[(message.sensor)].off();
        }
        if(message.type==V_LIGHT && message.getInt() == 1){
          relays[(message.sensor)].on();
        }
    }
    
    


  • @Dawidi
    Do you persist the relay states? This will make the previous state of the relays known after a restart of OH.
    Check this information: https://www.openhab.org/docs/configuration/persistence.html



  • Thanks. I have just made persist. Looks like that:
    config:

    # configure specific rrd properties for given items in this file.
    # please refer to the documentation available at
    # https://www.openhab.org/addons/persistence/rrd4j/
    #
    # default_numeric and default_other are internally defined defnames and are used as
    # defaults when no other defname applies
    
    relpols.def=GAUGE,60,U,U,30
    relpols.archives=LAST,0.5,1,365:LAST,0.5,7,300
    relpols.items= relay_leda, relay_ledb, relay_ledc, ... , relay_custom5
    

    persist:

    Strategies {
        // for rrd charts, we need a cron strategy
        everyMinute : "0 * * * * ?"
    }
    
    Items {
        // persist items on every change and every minute
        relay_leda, relay_ledb, ... , relay_custom4, relay_custom5 : strategy = everyChange, everyMinute, restoreOnStartup
    }
    

    And it looks quite good. Because after restart, states are restored, but it looks like MySensors are not connected in time. And logs looks like (pseudocode):

    [log] system start
    [log] some relays state changed from null to ON (restored states)
    [log] some relays state changed from null to OFF
    [log] MySensors bridge is changed from OFFLINE (COMMUNICATION_ERROR)  to OFFLINE
    [log] MySensors bridge is changed from OFFLINE (COMMUNICATION_ERROR)  to OFFLINE
    [log] MySensors bridge is changed from OFFLINE to ONLINE (finnaly)
    [log] all relays changed from OFFLINE to ONLINE
    

    and then all relays are turning OFF (physicaly) but in OH they have god states. For example relay_ao1 is physicaly off but in OH it is on, so to turn it ON i have to click it 2 time (first time it is switching to OFF).

    Any idea to fix it?

    edit:
    Now i think arduino is restarting after it connect with OH. Can it be true?



  • OK!
    Sorry but I cannot help you there. I don't know enough about the Serial GW. At least it seems OH is doing the right thing.


  • Hero Member

    In the relay actuator example the methods saveState() and loadState() are used. The current status of the pin (RELAY_ON or RELAY_OFF) is stored in the EEPROM with every change and restored after a restart of the node.

    The node will return to the last known state after a restart.

    BUT: while restarting the state of the pin may change (from on to off to on again while booting up). In case you want the relays to stay activated while booting up you may use an external pull up.


Log in to reply
 

Suggested Topics

  • 22
  • 19
  • 7
  • 1
  • 2
  • 4

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts