An HASS service to send messages to nodes



  • It would be nice to have an HA service that allows sending messages to mysensors nodes. With HA automation, this would allow to expand the use of sensors/actuators that for now are not well covered by HA.

    For instance, I could have a node that sends IR codes to a TV, let say 'select a source', this doesn't need to be in HA UI but could be useful for HA automation.



  • Not sure if this would help you or not but, V2.0 will feature direct Node to Node communication. Then, HA could receive the status update easily (assuming the NODE is set to send status updates).


  • Hero Member

    Node to node communication has always been supported as far as I know. It might be more simple in 2.0 to set it up though I'm not sure. I have not looked in to what 2.0 contains yet.



  • I already use node-to-node communication for other proposes, however that doesn't help if I want to use HA automation (and not only update the status of a sensor) ...


  • Hero Member

    Ahh, now I understand. Your request was directed at "Home Assistant". I don't know anything about Home Assistant sorry.


  • Plugin Developer

    @xlcnd

    Hi!

    Could you give a detailed example how you would use the service and what message to send using the mysensors protocol? Then I can start building a solution around that.



  • Hi,

    Thanks for your promptly reply.

    I have a node that acts like a IR remote to one of the newest Samsung TVs. This node runs a sketch that extracts IR codes from received messages and send them to TV and returns a feedback message to HASS. The node (with id 40) has a child sensor (with id 3) and his of type S_IR and accepts messages with type V_IR_SEND. In HASS this (ms)sensor is defined as a sensor (his identity_id is sensor.ir_remote) and displays the IR code that was sent, but there is no way in HASS to send a given IR code (for instance in automation) to this node... so a service to do that would be nice.



  • @xlcnd

    By the way, for now I implemented some of more useful function by using several child sensors of the type S_LIGHT and with the help of 'boolean' logic I select the intended IR code. But this produces complex sketches and a fair amount of rules in automation... just to send a code!



  • In the next version of HASS there is a new component that will do this (acting as an IR remote), however doesn't use mysensors but LIRC and a Raspberry Pi...


  • Plugin Developer

    My current plan is to move the IR send type to HA switch platform, and add a custom service that takes an attribute for the code.

    I haven't really looked into the details if this will work but I think so. Some of the devs have had a discussion about if there should be a common "remote" component where different platforms, LIRC, mysensors, etc, can interface their devices, but no actual plan has been finished.

    Until a better option is realised, I think the switch platform will be OK.



  • @martinhjelmare But would be possible to change that attribute in automation? E.g., could the same switch receive several codes?

    If not, this will not be a major improvement relative to the present situation...


  • Plugin Developer

    @xlcnd

    I imagine it should work similar to how you can set brightness for a light in automation etc. So yes, you should be able to use logic to set different codes. Although, as I said, I haven't looked into all details, so we'll see what will be possible.

    One problem I foresee is that it should preferably work as a push button, but switches in HA usually work as flip switches. Ie they stay in the state you switch them into. But I guess we want the device to turn on, send a code and then turn back to off state.



  • @martinhjelmare In that case it will be VERY useful.

    I have been playing with HA and LIRC on a Raspberry Pi (that acts as a remote near one of the new Samsung TVs) and found that the ability to send an IR code from HA is only useful for 'direct functions' like POWER, MUTE, SOURCE, VOL UP and VOL DOWN. Due to security reasons the appliance manufacturer's are limiting these cases and relying on context codes, like using arrows to select the appropriate choice, this makes the remote a not very useful concept to use in automation! Fortunately, if HA sends a code that represents the final goal (e.g. goto BBC), instead of the IR code, it is relatively easy to guess the appropriated steps in the node and send a sequence of codes to the appliance. The logic for that is better to be in the node not in HA because, even if in HA you know the sequence of IR codes, playing directly these codes in the node, make the process very unreliable (a small delay in the transmission and you will end up on Fox News instead!).

    So your solution, in fact, is BETTER than a generic remote.

    However, I don't see advantages to move 'IR send' to a switch, let it stay as a sensor whose state is the 'last code sent' together with a service that sends messages to that sensor... anyway this is a thing that will be useful for automation not so much as a UI gadget


  • Plugin Developer

    @xlcnd

    Sensors are read only so it doesn't make sense to have an actuator like an IR send device be a sensor. That said, I'll know more how all this will work out once I start planning the code in detail. The IR receive should still be a sensor.



  • @martinhjelmare Yes, I know that a sensor is read only, but it is important that the state is available on HA, how will you do it with a switch?


  • Plugin Developer

    @xlcnd

    The IR code will be available as a state attribute. You will be able to check it in automations with a template and is_state_attr for example.




  • Plugin Developer

    PR here:
    https://github.com/home-assistant/home-assistant/pull/2239

    Example sketch used for testing:

    /*
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * http://www.mysensors.org/build/ir
     */
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <IRLib.h>
    
    #define SN "IR Sensor"
    #define SV "1.0"
    #define CHILD_ID 1
    
    MySensor gw;
    
    char code[10] = "abcd01234";
    char oldCode[10] = "abcd01234";
    MyMessage msgCodeRec(CHILD_ID, V_IR_RECEIVE);
    MyMessage msgCode(CHILD_ID, V_IR_SEND);
    MyMessage msgSendCode(CHILD_ID, V_LIGHT);
    
    void setup()
    {
      gw.begin(incomingMessage);
      gw.sendSketchInfo(SN, SV);
      gw.present(CHILD_ID, S_IR);
      // Send initial values.
      gw.send(msgCodeRec.set(code));
      gw.send(msgCode.set(code));
      gw.send(msgSendCode.set(0));
    }
    
    void loop()
    {
      gw.process();
      // IR receiver not implemented, just a dummy report of code when it changes
      if (String(code) != String(oldCode)) {
        Serial.print("Code received ");
        Serial.println(code);
        gw.send(msgCodeRec.set(code));
        strcpy(oldCode, code);
      }
    }
    
    void incomingMessage(const MyMessage &message) {
      if (message.type==V_LIGHT) {
        // IR sender not implemented, just a dummy print.
        if (message.getBool()) {
          Serial.print("Sending code ");
          Serial.println(code);
        }
        gw.send(msgSendCode.set(message.getBool() ? 1 : 0));
        // Always turn off device
        gw.wait(100);
        gw.send(msgSendCode.set(0));
      }
      if (message.type == V_IR_SEND) {
        // Retrieve the IR code value from the incoming message.
        String codestring = message.getString();
        codestring.toCharArray(code, sizeof(code));
        Serial.print("Changing code to ");
        Serial.println(code);
        gw.send(msgCode.set(code));
      }
    }
    


  • Thanks!

    I am traveling but as soon as I get home I will test it.



  • It works like a charm in version 0.22.1.

    For complex sequences of IR codes, I just send a 'fake' code and then the node selects the appropriate sequence.

    Thanks again!


  • Plugin Developer

    @xlcnd

    Thanks for the feedback and live testing!



  • Let me complete the example given for the node when you use V_IR_SEND.

    In order to send IR codes you need to transform the
    code in an unsigned long:

        char ircode[11] = {0};  // in case your code takes the form of 0xE0E0FF0F
        ...
        if (message.type == V_IR_SEND) {
          String hexstring = message.getString();
          hexstring.toCharArray(ircode, sizeof(ircode));
          ...
          // get the code as an unsigned long
          unsigned long code = strtoul(ircode, NULL, 0);
    
          // with IRLib send the code to TV
          sendSAMSUNG(code);
          ...
    

    Very simple, ** but is hard to find an example**!



Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts