Relay shows up in mysensors.json but not in gui [homeassistant]



  • I had multiple sensors on my homeassistant controller, all working fine. I then attempted to attach another relay (RelayAcuator project and sketch). The relay showed up in mysensors.json but not the gui. I eliminated all other sensors and attempted a new sketch upload, with the same results. I ran across other posts on this subject and the common opinion was that an initial value was not sent to Home Assistant. Ideas???

    Sketch:

    /*
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2018 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Example sketch showing how to control physical relays.
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */
    
    // 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
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <MySensors.h>
    
    #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    
    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, loadState(sensor)?RELAY_ON: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);
      }
    }
    
    
    void loop()
    {
    
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS) {
        // Change relay state
        digitalWrite(message.sensor-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      }
    }
    
    

    Home assistant logs:

    2019-07-01 15:26:03 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 255
    2019-07-01 15:26:03 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=255,c=3,t=12,pt=0,l=3,sg=0:1.0
    2019-07-01 15:26:03 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 2 child 255
    2019-07-01 15:26:03 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=1,c=0,t=3,pt=0,l=0,sg=0:
    2019-07-01 15:26:03 WARNING (MainThread) [mysensors.sensor] child_id 1 already exists in children of node 2, cannot add child
    2019-07-01 15:26:03 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=255,c=3,t=26,pt=1,l=1,sg=0:2
    2019-07-01 15:26:03 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:TSF:MSG:SEND,0-0-2-2,s=255,c=3,t=27,pt=1,l=1,sg=0,ft=0,st=OK:1
    2019-07-01 15:26:13 DEBUG (SyncWorker_1) [mysensors.persistence] Saving sensors to persistence file /home/homeassistant/.homeassistant/mysensors.json```
    
    Mysensors.json
    
    {
        "0": {
            "sensor_id": 0,
            "children": {},
            "type": 18,
            "sketch_name": null,
            "sketch_version": null,
            "battery_level": 0,
            "protocol_version": "2.1.1",
            "heartbeat": 0
        },
        "2": {
            "sensor_id": 2,
            "children": {
                "1": {
                    "id": 1,
                    "type": 3,
                    "description": "",
                    "values": {}
                }
            },
            "type": 18,
            "sketch_name": "Relay",
            "sketch_version": "1.0",
            "battery_level": 0,
            "protocol_version": "2.3.1",
            "heartbeat": 0
        }
    


  • Are you using a relatively recent build of Home Assistant?

    Check in the Unused Entities section for your sensor. I think home assistant no longer adds discovered devices to the front-end by default.



  • Version 0.94.4

    Thank you for the recommendation. I learned to look there through trial-and-error but the relay does not show up.

    However, everything works as expected (4 relays show up in Unused Entities) when I use the following sketch:

    Copy to clipboard
    // Override Setting for Manual Node ID to 2
    #define MY_NODE_ID 2
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define RELAY_1  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: 4
    
    // Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
    #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 initialValueSent = false;
    
    //Init MyMessage for Each Child ID
    MyMessage msg1(1, V_LIGHT);
    MyMessage msg2(2, V_LIGHT);
    MyMessage msg3(3, V_LIGHT);
    MyMessage msg4(4, V_LIGHT);
    
    void before() { 
      for (int sensor=1, pin=RELAY_1; 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, loadState(sensor)?RELAY_ON: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_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_LIGHT);
      }
    }
    
    
    void loop() 
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg1.set(loadState(1)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg2.set(loadState(2)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg3.set(loadState(3)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg4.set(loadState(4)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        Serial.println("Sending initial value: Completed");
        wait(5000);
      }
    }
    
    void receive(const MyMessage &message) {
      Serial.println("=============== Receive Start =======================");
      if (message.isAck()) {
         Serial.println(">>>>> ACK <<<<<");
         Serial.println("This is an ack from gateway");
         Serial.println("<<<<<< ACK >>>>>>");
      }
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
        Serial.println(">>>>> V_LIGHT <<<<<");
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
         // Update relay state to HA
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         switch (message.sensor) {
            case 1:
              Serial.print("Incoming change for sensor 1");
              send(msg1.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 2:
              Serial.print("Incoming change for sensor 2");
              send(msg2.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 3:
              Serial.print("Incoming change for sensor 3");
              send(msg3.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 4:
              Serial.print("Incoming change for sensor 4");
              send(msg4.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;                    
            default: 
              Serial.println("Default Case: Receiving Other Sensor Child ID");
            break;
         }
         // Store state in Arduino eeprom
         saveState(message.sensor, message.getBool());
         Serial.print("Saved State for sensor: ");
         Serial.print( message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
         Serial.println("<<<<<< V_LIGHT >>>>>>");
       } 
       Serial.println("=============== Receive END =======================");
    }```


  • i have the same very problem adding a water mater pulse sensor with the following sketch from mysensors.org

    /*
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2018 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * Version 1.1 - GizMoCuz
     *
     * DESCRIPTION
     * Use this sensor to measure volume and flow of your house water meter.
     * You need to set the correct pulsefactor of your meter (pulses per m3).
     * The sensor starts by fetching current volume reading from gateway (VAR 1).
     * Reports both volume and flow back to gateway.
     *
     * Unfortunately millis() won't increment when the Arduino is in
     * sleepmode. So we cannot make this sensor sleep if we also want
     * to calculate/report flow.
     * http://www.mysensors.org/build/pulse_water
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    #define MY_NODE_ID 6
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    
    #define DIGITAL_INPUT_SENSOR 3                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
    
    #define PULSE_FACTOR 1000                       // Number of blinks per m3 of your meter (One rotation/liter)
    
    #define SLEEP_MODE false                        // flowvalue can only be reported when sleep mode is false.
    
    #define MAX_FLOW 40                             // Max flow (l/min) value to report. This filters outliers.
    
    #define CHILD_ID 1                              // Id of the sensor child
    
    uint32_t SEND_FREQUENCY =
        30000;           // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    
    MyMessage flowMsg(CHILD_ID,V_FLOW);
    MyMessage volumeMsg(CHILD_ID,V_VOLUME);
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
    
    double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
    
    volatile uint32_t pulseCount = 0;
    volatile uint32_t lastBlink = 0;
    volatile double flow = 0;
    bool pcReceived = false;
    uint32_t oldPulseCount = 0;
    uint32_t newBlink = 0;
    double oldflow = 0;
    double volume =0;
    double oldvolume =0;
    uint32_t lastSend =0;
    uint32_t lastPulse =0;
    
    void setup()
    {
      // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
      pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
    
      pulseCount = oldPulseCount = 0;
    
      // Fetch last known pulse count value from gw
      request(CHILD_ID, V_VAR1);
    
      lastSend = lastPulse = millis();
    
      attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Water Meter", "1.1");
    
      // Register this device as Water flow sensor
      present(CHILD_ID, S_WATER);
    }
    
    void loop()
    {
      uint32_t currentTime = millis();
    
      // Only send values at a maximum frequency or woken up from sleep
      if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
        lastSend=currentTime;
    
        if (!pcReceived) {
          //Last Pulsecount not yet received from controller, request it again
          request(CHILD_ID, V_VAR1);
          return;
        }
    
        if (!SLEEP_MODE && flow != oldflow) {
          oldflow = flow;
    
          Serial.print("l/min:");
          Serial.println(flow);
    
          // Check that we don't get unreasonable large flow value.
          // could happen when long wraps or false interrupt triggered
          if (flow<((uint32_t)MAX_FLOW)) {
            send(flowMsg.set(flow, 2));                   // Send flow value to gw
          }
        }
    
        // No Pulse count received in 2min
        if(currentTime - lastPulse > 120000) {
          flow = 0;
        }
    
        // Pulse count has changed
        if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
          oldPulseCount = pulseCount;
    
          Serial.print("pulsecount:");
          Serial.println(pulseCount);
    
          send(lastCounterMsg.set(pulseCount));                  // Send  pulsecount value to gw in VAR1
    
          double volume = ((double)pulseCount/((double)PULSE_FACTOR));
          if ((volume != oldvolume)||(!SLEEP_MODE)) {
            oldvolume = volume;
    
            Serial.print("volume:");
            Serial.println(volume, 3);
    
            send(volumeMsg.set(volume, 3));               // Send volume value to gw
          }
        }
      }
      if (SLEEP_MODE) {
        sleep(SEND_FREQUENCY);
      }
    }
    
    void receive(const MyMessage &message)
    {
      if (message.type==V_VAR1) {
        uint32_t gwPulseCount=message.getULong();
        pulseCount += gwPulseCount;
        flow=oldflow=0;
        Serial.print("Received last pulse count from gw:");
        Serial.println(pulseCount);
        pcReceived = true;
      }
    }
    
    void onPulse()
    {
      if (!SLEEP_MODE) {
        uint32_t newBlink = micros();
        uint32_t interval = newBlink-lastBlink;
    
        if (interval!=0) {
          lastPulse = millis();
          if (interval<500000L) {
            // Sometimes we get interrupt on RISING,  500000 = 0.5 second debounce ( max 120 l/min)
            return;
          }
          flow = (60000000.0 /interval) / ppl;
        }
        lastBlink = newBlink;
      }
      pulseCount++;
    }
    

    Node 6 as Water Meter shows up in mysensors.json and homeassistant logs but fails to appear in GUI.
    Thank you for your help



  • From my limited use with sensors other then temperature, sometimes items don't appear in the GUI on Home Assistant until they send some data.

    Also check in the States section of Developer Tools at what data HA has acquired from your sensor.

    Beyond that, you might have to ask on the Home Assistant forums.



  • @mcchots thank you, that's what I thought, too. Let me rig it up on my water meter and see if starts sending any data



  • So I too was having this issue and pulling my hair out trying to understand what was wrong.
    The homeassistant/mysensor doc page says you have to send intial values, but the important nuanced point is that ALL possible value types must have an initial value sent for a given sensor type.

    In my example, I could not figure out why my motion sensor (S_MOTION) was not registering in HA UI - I was sending an initial (V_TRIPPED) state.

    However, S_MOTION sensors can have both V_TRIPPED and V_ARMED.
    ref: https://www.mysensors.org/download/serial_api_20#presentation
    Once I sent an initial value for V_ARMED, it started working - whew!

    So check carefully that you are sending some initial value for EVERY possible value type of your sensor type.



  • @jens-jensen thank you for your input
    could you have a look at my sketch above and point me in the right direction on what needs to be changed/added?
    I see this part which looks good to me:

    MyMessage flowMsg(CHILD_ID,V_FLOW);
    MyMessage volumeMsg(CHILD_ID,V_VOLUME);
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);```
    thanks


  • @bereska
    looks mostly correct, althought I would take a look at the example here to use their technique for ensuring you send initial values for V_FLOW and V_VOLUME.
    ref: https://www.home-assistant.io/components/mysensors/

    From your sketch, it seems possible that either may not be sent right away, e.g. if volume or flow is not changing, etc.

    Also, where in HA UI are you looking? I had to add my sensor to one of my lovelace cards. Still getting the hang of the new UI (but definitely better than it was before). I also saw it in developer, states under current entities



  • @jens-jensen thank you, like you said the sketch looks ok. Yet this only sensor fails to appear in HA, neither on dashboard nor in states or unused entities. The only thing I have noticed though is that this sketch is missing this part:

    Bounce debouncer = Bounce();
    bool state = false;
    bool initialValueSent = false;
    

    maybe that's the key to this problem?



  • this is HA log:

    2019-07-04 23:24:05 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on mygateway1-out/6/255/3/0/11: b'Water Meter',
    2019-07-04 23:24:05 DEBUG (MainThread) [mysensors.gateway_mqtt] Receiving 6;255;3;0;11;Water Meter,
    2019-07-04 23:24:23 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on mygateway1-out/6/255/3/0/11: b'Water Meter',
    2019-07-04 23:24:23 DEBUG (MainThread) [mysensors.gateway_mqtt] Receiving 6;255;3;0;11;Water Meter



  • Looking at your HA log, do you see that both V_FLOW and V_VOLUME values are sent after some time?

    With the sketch, their idea is that you use initialValueSent to send initial values (for all types) only once (at first boot) in loop section.



  • @jens-jensen said in Relay shows up in mysensors.json but not in gui [homeassistant]:

    V_FLOW and V_VOLUME values are sent after some time

    no, V_FLOW and V_VOLUME values are NOT sent after some time
    how to i modify the sketch to make it happen?



  • @bereska
    look at how the reference sketch in the homeassistant mysensors page works.
    I starts with a bool initialValueSent set to false.
    It checks for this in loop, and if not already set to true, it will send initial values, then set initialValueSent = false, so that it never runs again (until sensor is restarted).

    Also regarding your sketch logic, should it periodically send values of all sensors (like at least once per hour, per day, etc)? You may want to look at that as well.



  • ok, I think i have also may have found a bug in the HA mysensors implementation, i.e. if you name your sketch with some reserved names, it never registers in HA.

    I.e.
    sendSketchInfo("motion_sensor", "1.0"); won't work in HA
    but sendSketchInfo("motion_sensor_foo", "1.0"); shows up 😞
    I didn't see any mention of this in https://www.home-assistant.io/components/mysensors/
    Maybe it's a bug (should mysensors ha component rename it or prefix with some namespace, etc; or should this be documented in guide - and then what are the reserved names which we cannot name sketches to?)



  • nm, I seems like it eventually sends the sketch name



  • @jens-jensen it does send and register the name, no problem.



  • i can't get this sensor to show up in HA. Any ideas anyone?


 

199
Online

8.6k
Users

9.4k
Topics

99.0k
Posts