optimistic: false and delayed state changes



  • Hello,

    I have been toying with MySensors and MYSController for the past few weeks and have reached the point of trying to get some tools working with Home Assistant.

    The setup for my first test is:

    • Home Assistant - raspberry pi
    • Serial gateway - Arduino Mega running essentially the tutorial "gateway" sketch
    • Sensor node - Arduino Leonardo with 5 press buttons / 5 LEDs that presents itself as 5x S_BINARY child sensors

    The sensor sketch is fairly straightforward. If a button is toggled, a state update for that S_BINARY is sent. If a message is received, the internal state for the appropriate S_BINARY is updated and the new state is parroted back to the gateway. If I understand correctly this is how it should be if we want to use the "optimistic: false" configuration in Home Assistant - we're expected to echo back any incoming state changes.

    For clarity, this is receive()

    void receive(const MyMessage &message) {
      int sensorIndex;
      // only accept messages we have some idea what to do with
      if(message.sensor < 1 || message.sensor > cntButtons || message.type != V_STATUS)
        return;
    
      sensorIndex = message.sensor - 1;
      buttonState[sensorIndex] = message.getBool();
      send(msg[sensorIndex].set(buttonState[sensorIndex]?1:0));
    }
    

    This all works flawlessly on MYSController. When the gateway is instead connected to Home Assistant:

    • the 5 buttons are successfully shown as switches in the Home Assistant interface
    • toggling the buttons results in a successful update of the switch state in the Home Assistant interface
    • toggling the switch from the Home Assistant interface fails
      • the log shows a call to the switch.turn_on or switch.turn_off service respectively
      • there is no state_changed event
      • after a few moments the Home Assistant interface reverts the state of the switch
      • the message is however received by the Arduino, and it lights/de-lights the LED appropriately

    Initially I thought the send() call in receive() must be failing, however if I then manually press one of the buttons on the Leonardo, suddenly state_changed events will fire for each button that is out of sync between the device and the web interface.

    The behaviour is just as though there is some transmit/receive queue that needs to receive another message in order to be flushed. I haven't yet figured out how to get fine-grained MySensors debugging info out of the gateway, but from seeing both the gateway and the node behaving so well on MYSController I don't think I need to at this stage.

    Am I misunderstanding how the Home Assistant component expects to receive confirmation of state changes? Is there some obvious misconfiguration I've made? I think setting optimistic: true would work as a patch job for the immediate problem, but I'd like to understand what I am doing wrong here.


  • Plugin Developer

    @tjkerr

    Hi!

    You can add print statements of the different variables to the receive method to see more what's going on.

    Also, I suggest you post the serial logs and home assistant debug log and your full sketch, to help me or others to troubleshoot this better.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts