request() not working with HomeAssistant



  • Hi everyone,

    I've been going crazy trying to figure this one out.

    I have a sketch hooked up to HomeAssistant. The basic sketch just toggles a switch back and forth. The actual sending of the updates works -- as the sketch toggles the switch, I see the switch moving back and forth in the HA UI.

    However, when I try to request the V_STATUS of the switch, I see the message going out through the gateway and onto the MQTT, but I never get anything back. receive() never gets called with a status message.

    I'm currently on the development version of MySensors, but I had the same issue on 2.0. I just switched to development an hour ago to see if that would solve the problem, but alas, it didn't.

    Any thoughts? Thanks in advance

    My Sketch

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define RELAY_PIN  3  // Arduino Digital I/O pin number for relay 
    #define CHILD_ID 2   // Id of the sensor child
    #define RELAY_ON 0
    #define RELAY_OFF 1
    
    bool state;
    
    MyMessage msg(CHILD_ID,V_STATUS);
    
    void setup()  
    {  
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Button", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_BINARY);
    
    
    }
    
    
    int flip = 0;
    void loop() 
    {
      send(msg.set(flip),false);
      flip=flip?0:1;
      wait(5000);
      request(CHILD_ID,V_STATUS);
      wait(2000,C_SET,V_STATUS);
      
    } 
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      Serial.println("Got a message");
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_STATUS) {
        Serial.println("Got V_STATUS back:");
        Serial.println(message.getBool());
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    
    
       } 
    }
    

    Gateway log:

    Nov  8 05:31:52 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=2,t=2,pt=0,l=0,sg=0:
    Nov  8 05:31:52 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/2/0/2
    Nov  8 05:31:54 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=1,t=2,pt=2,l=2,sg=0:0
    Nov  8 05:31:54 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/1/0/2
    Nov  8 05:31:59 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=2,t=2,pt=0,l=0,sg=0:
    Nov  8 05:31:59 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/2/0/2
    Nov  8 05:32:01 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=1,t=2,pt=2,l=2,sg=0:1
    Nov  8 05:32:01 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/1/0/2
    Nov  8 05:32:06 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=2,t=2,pt=0,l=0,sg=0:
    Nov  8 05:32:06 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/2/0/2
    Nov  8 05:32:08 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=1,t=2,pt=2,l=2,sg=0:0
    Nov  8 05:32:08 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/1/0/2
    Nov  8 05:32:13 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=2,t=2,pt=0,l=0,sg=0:
    Nov  8 05:32:13 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/2/0/2
    Nov  8 05:32:15 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=1,t=2,pt=2,l=2,sg=0:1
    Nov  8 05:32:15 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/1/0/2
    Nov  8 05:32:20 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=2,t=2,pt=0,l=0,sg=0:
    Nov  8 05:32:20 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/2/0/2
    Nov  8 05:32:22 raspberrypi mysGateway: TSF:MSG:READ,1-1-0,s=2,c=1,t=2,pt=2,l=2,sg=0:0
    Nov  8 05:32:22 raspberrypi mysGateway: Sending message on topic: mysensors-out/1/2/1/0/2
    

    mosquitto_sub:

    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-in/1/2/1/0/2', ... (1 bytes))
    mysensors-in/1/2/1/0/2 1
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/1/2/1/0/2', ... (1 bytes))
    mysensors-out/1/2/1/0/2 0
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/1/2/2/0/2', ... (0 bytes))
    mysensors-out/1/2/2/0/2 (null)
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-in/1/2/1/0/2', ... (1 bytes))
    mysensors-in/1/2/1/0/2 0
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/1/2/1/0/2', ... (1 bytes))
    mysensors-out/1/2/1/0/2 1
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/1/2/2/0/2', ... (0 bytes))
    mysensors-out/1/2/2/0/2 (null)
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-in/1/2/1/0/2', ... (1 bytes))
    mysensors-in/1/2/1/0/2 1
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/1/2/1/0/2', ... (1 bytes))
    mysensors-out/1/2/1/0/2 0
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/1/2/2/0/2', ... (0 bytes))
    mysensors-out/1/2/2/0/2 (null)
    Client mosqsub/4008-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-in/1/2/1/0/2', ... (1 bytes))
    mysensors-in/1/2/1/0/2 0
    
    

    Serial output

    811358 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    816367 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    818379 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
    823388 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    825397 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    830406 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    832415 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
    837424 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    839433 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    844442 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    846451 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
    851462 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    853471 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    858480 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    860496 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
    865506 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    867515 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    872524 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    874533 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
    879543 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    881552 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    886561 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    888570 TSF:MSG:SEND,1-1-0-0,s=2,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
    893580 TSF:MSG:SEND,1-1-0-0,s=2,c=2,t=2,pt=0,l=0,sg=0,ft=0,st=OK:
    
    

    (Note that the logs above are not perfectly lined up -- I copied and pasted as they were rolling, so they may be a few lines offset)


  • Plugin Developer

    Hi!

    Looking at the MQTT log, it looks as you're getting a reply for your request from home assistant, cause you're getting lots of these:

    mysensors-in/1/2/1/0/2 0
    

    and these:

    mysensors-in/1/2/1/0/2 1
    

    which are messages sent from broker to gateway. So just from this I would say that the problem lies somewhere between gateway and node, not between gateway and home assistant.

    Do you have the same problem if you toggle the switch in home assistant? Ie, do you not see any update on the node side, then?



  • When I toggle the switch in HomeAssistant, I do get a V_STATUS update message in receive() and see it print through in the logs.

    So, the toggle itself causes a status message, but I cannot figure out a way to actually poll the status.

    Thanks!


  • Plugin Developer

    I can't see anything obviously wrong with your sketch. Do you have the possibility of testing with a serial or ethernet gateway instead of mqtt? If that works we should be able to rule out your sketch as the problem.


  • Admin

    @ajlisy Here are a couple of ideas:

    1. Do you ever see Got a message in your log? If not, what if you turn on acks in the send()? If you are receiving messages, you might want to add logging of the message.type.

    2. wait() will return true if it receives the message and command specified in the arguments and false if it doesn't (timer expired) so you could change your wait(2000, C_SET,V_STATUS); to the following to see what is happening at this point in the sketch:

    bool resp = wait( 2000, C_SET,V_STATUS );
    Serial.print( "wait( 2000, C_SET, V_STATUS ) return = " );
    Serial.println(resp ? "true" : "false");
    
    1. If wait() returns true above, then that confirms that the node is receiving a V_STATUS back from the gateway so the basic request response is functioning. If that is the case, then what happens if you change the wait(2000,C_SET,V_STATUS); in loop() to wait( 1000 );? Perhaps wait()'ing on a specific message consumes that message from the queue preventing the receive() callback from being called? It is not clear that the second wait() is even needed... I would think this simplified version would work:
    void loop()
    {
       send( msg.set( flip = flip ? 0 : 1 ), false );
       request( CHILD_ID, V_STATUS );
       wait( 1000 ); 
    }
    
    1. If you want to synchronize the node state with the Home Assistant controller, you can send the request message in setup() [NOTE: Requires development branch]
    void setup()  
    {
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
      //Synchronize relay state with gateway - this will result in receive() being called.
      request( CHILD_ID, V_STATUS );
    }
    

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.