(Solved) S_Multimeter in HomeAssistant



  • Hi everybody,

    I've been struggling for quite some time now to get a V_Voltage MySensor to show up in HomeAssistant (just updated successfully to 0.104.3, still no dice).
    I am using a serial gateway and have multiple other sensors (BME280) working successfully. The code I try to get working is this:

    #include <Arduino.h>
    // 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 debug prints
    
    #define MY_DEBUG
    
    #define MY_RADIO_RFM95
    #define MY_NODE_ID 120
    #define CHILD_ID_VOLTAGE 0
    
    #include <MySensors.h>
    
    MyMessage msgVoltage(CHILD_ID_VOLTAGE,V_VOLTAGE);
    
    int counter=0;
    
    long readVcc() {
      // Read 1.1V reference against AVcc
      // set the reference to Vcc and the measurement to the internal 1.1V reference
      // #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
      //   ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
      // #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
      //    ADMUX = _BV(MUX5) | _BV(MUX0) ;
      // #else
         ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
      //#endif  
     
      delay(2); // Wait for Vref to settle
      ADCSRA |= _BV(ADSC); // Start conversion
      while (bit_is_set(ADCSRA,ADSC)); // measuring
      uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
      uint8_t high = ADCH; // unlocks both
      long result = (high<<8) | low;
      result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
      return result; // Vcc in millivolts
    }
    
    void preHwInit ()
    {
    }
    
    void setup()
    {
    }
    
    void presentation()
    {
      sendSketchInfo("Voltmeter", "1.0");
      present(CHILD_ID_VOLTAGE,S_MULTIMETER, "Batterie");
      send(msgVoltage.set(6,1));
      request(CHILD_ID_VOLTAGE, V_VOLTAGE);
      Serial.println("Presentation done");
    }
    
    void loop()
    {
      float voltage; voltage = readVcc();
      Serial.println(voltage/1000);
      send(msgVoltage.set(voltage/1000,1));
      
      sleep(30000);
    }
    
    void receive(const MyMessage &message) {
      Serial.print("Incoming message for child: ");
      Serial.println(message.sensor);
      if (message.isAck() ) {//&& message.type == V_TRIPPED) {  // Test if ack received and for which type of msg, you could also test for which child id
         Serial.println("This is an ack from gateway");
         // set your "flag" ack variable to true here. Test it in your retry loop and reset it to false
      }
    }
    
    

    The errors I get in HomeAssistant are:

    2020-01-30 22:11:23 DEBUG (MainThread) [mysensors] Receiving 120;255;0;0;17;2.3.1
    2020-01-30 22:11:23 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 120 child 255
    2020-01-30 22:11:23 DEBUG (MainThread) [mysensors] Receiving 120;255;3;0;6;0
    2020-01-30 22:11:23 DEBUG (MainThread) [mysensors] Sending 120;255;3;0;6;M
    2020-01-30 22:11:23 DEBUG (MainThread) [mysensors] Receiving 120;255;3;0;11;Voltmeter
    2020-01-30 22:11:23 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 120 child 255
    2020-01-30 22:11:23 DEBUG (MainThread) [mysensors] Receiving 120;255;3;0;12;1.0
    2020-01-30 22:11:23 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 120 child 255
    2020-01-30 22:11:23 DEBUG (MainThread) [mysensors] Receiving 120;0;0;0;30;Batterie
    2020-01-30 22:11:23 WARNING (MainThread) [mysensors] Not a valid message: Not valid message sub-type: 30 for dictionary value @ data['sub_type']
    2020-01-30 22:11:24 DEBUG (MainThread) [mysensors] Receiving 120;0;1;0;38;3.44
    2020-01-30 22:11:24 WARNING (MainThread) [mysensors] Child 0 is unknown
    

    So I think "Not a valid message..." and "Child 0 is unknown" are problems here. HA does not show the sensor in its UI.
    Any one has a clue how I can make the sensor known?

    Thanks for any help,

    Joost

    PS: This is the debug output from the sensor; I guess looking all good:

    3.44
    8249 TSF:MSG:SEND,120-120-0-0,s=1,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.4
    8267 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    8282 TSF:TDI:TSL
    8288 MCO:SLP:WUP=-1
    8294 TSF:TRI:TSB
    


  • Btw., found this in config/mysensors.json:

    ...
        "120": {
            "sensor_id": 120,
            "children": {},
            "type": 17,
            "sketch_name": "Voltmeter",
            "sketch_version": "1.0",
            "battery_level": 0,
            "protocol_version": "2.3.1",
            "heartbeat": 0
        }
    

    So no child is recognized with this node. All my other sensors have the necessary/presented children listed in here.


  • Hero Member

    @Joost

    Did you find a solution to your problem? I am using S_Multimeter in Hassio and measuring both voltage and current without problems so at least I can confirm that it should work.

    Perhaps you could try entering the child details into your mysensors.json file directly then reboot and see if that helps.

    should be something like this

    "120": {
            "sensor_id": 120,
            "children": {
                "0": {
                    "id": 0,
                    "type": 30,
                    "description": "Batterie",
                    "values": {
                        "38": "5.2"
                    }
                }
            },
            "type": 17,
            "sketch_name": "Voltmeter",
            "sketch_version": "1.0",
            "battery_level": 0,
            "protocol_version": "2.3.1",
            "heartbeat": 0
        }
    


  • Here is some code I use with NRF24 for a temperature sensor . So far works great with lastest HA version.

    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 10
    #define DEBUG 0
    #define BATTERY_SENSOR 1
    
    #include <MySensors.h>
    
    #define TH1_CHILD_ID 11
    #define VOLTAGE_CHILD_ID 12
    #define TH_PIN 3
    
    int _nominal_resistor = 4700;
    int _nominal_temperature = 25;
    int _b_coefficient = 3950;
    int _series_resistor = 4877;
    int _pin = 0;
    
    float BatteryMin = 2.4;
    float BatteryMax = 3.0;
    
    MyMessage msgTemp(TH1_CHILD_ID, V_TEMP);
    MyMessage msgVoltage(VOLTAGE_CHILD_ID, V_VOLTAGE);
    
    void setup()
    {
      //Serial.begin(115200);
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temp battery", "1.0");
      present(TH1_CHILD_ID, S_TEMP, "Temp");
      present(VOLTAGE_CHILD_ID, S_MULTIMETER, "Batt");
    }
    
    void loop()
    {
      tempReport();
      batteryReport();
      sleep(180000);
    }
    
    // Measure VCC
    float getVcc() {
    #ifndef MY_GATEWAY_ESP8266
      // Measure Vcc against 1.1V Vref
    #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
      ADMUX = (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
    #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
      ADMUX = (_BV(MUX5) | _BV(MUX0));
    #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
      ADMUX = (_BV(MUX3) | _BV(MUX2));
    #else
      ADMUX = (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
    #endif
      // Vref settle
      wait(70);
      // Do conversion
      ADCSRA |= _BV(ADSC);
      while (bit_is_set(ADCSRA, ADSC)) {};
      // return Vcc in mV
      return (float)((1125300UL) / ADC) / 1000;
    #else
      return (float)0;
    #endif
    }
    
    // Send a battery level report to the controller
    void batteryReport() {
      // measure the board vcc
      float volt = getVcc();
      // calculate the percentage
      int percentage = ((volt - BatteryMin) / (BatteryMax - BatteryMin)) * 100;
      if (percentage > 100) percentage = 100;
      if (percentage < 0) percentage = 0;
    #if DEBUG == 1
      Serial.print(F("BATT V="));
      Serial.print(volt);
      Serial.print(F(" P="));
      Serial.println(percentage);
    #endif
    #if BATTERY_SENSOR == 1
      // report battery voltage
      send(msgVoltage.set(volt, 3));
    #endif
      // report battery level percentage
      sendBatteryLevel(percentage);
    }
    
    void tempReport()
    {
      // set TH pin in output mode
      pinMode(TH_PIN, OUTPUT);
      // set TH_PIN HIGH
      digitalWrite(TH_PIN, HIGH);
      wait(1);
      // read the voltage across the thermistor
      float adc = analogRead(_pin);
      // set TH_PIN LOW
      digitalWrite(TH_PIN, LOW);
      // calculate the temperature
      float reading = (1023 / adc)  - 1;
      reading = _series_resistor / reading;
      float temperature;
      temperature = reading / _nominal_resistor;     // (R/Ro)
      temperature = log(temperature);                  // ln(R/Ro)
      temperature /= _b_coefficient;                   // 1/B * ln(R/Ro)
      temperature += 1.0 / (_nominal_temperature + 273.15); // + (1/To)
      temperature = 1.0 / temperature;                 // Invert
      temperature -= 273.15;                         // convert to C
    #if DEBUG == 1
      Serial.print(F("THER I="));
      Serial.print(TH1_CHILD_ID);
      Serial.print(F(" V="));
      Serial.print(adc);
      Serial.print(F(" T="));
      Serial.println(temperature);
    #endif
      send(msgTemp.set(temperature, 1));
    }
    


  • @Joost Perhaps you started the node at some point during development without presenting the sensors? If HA has already seen your node before, I think it will not update its children/sensors again even if you restart and have the node send the presentation again. What I do when this happens is to shut down HA, remove the entry from the json file and restart HA again. Then restart the node so it presents itself again.



  • Hi everybody,

    first of all please excuse that I didn't get back here. I was completely expecting to get email notifications for replies, but it seems I turned that off (or it is not switched on by default, can not remember). Very sorry and also thankful for your replies, which help me check my code against your working examples and rule out mistakes in my sketch.

    So, my journey to get this working continues. My current workflow to test new iterations is like this (I guess you were on the right track @maghac ) :

    1. Set persistence to false for mysensors integration.

    2. Restart HASS and check it's really off: yep, all nodes notified as "unknown"

    3. Power on the node I am testing with to get a new presentation to HASS. The log for this is

    2020-02-17 20:35:48 WARNING (MainThread) [mysensors] Node 105 is unknown
    2020-02-17 20:35:48 DEBUG (MainThread) [mysensors] Receiving 105;255;3;0;0;33
    2020-02-17 20:35:48 WARNING (MainThread) [mysensors] Node 105 is unknown
    2020-02-17 20:35:57 DEBUG (MainThread) [mysensors] Receiving 131;0;1;0;0;19.9
    2020-02-17 20:35:57 WARNING (MainThread) [mysensors] Node 131 is unknown
    2020-02-17 20:35:57 DEBUG (MainThread) [mysensors] Receiving 131;1;1;0;1;51.9
    2020-02-17 20:35:57 WARNING (MainThread) [mysensors] Node 131 is unknown
    2020-02-17 20:35:57 DEBUG (MainThread) [mysensors] Receiving 131;2;1;0;4;1009.23
    2020-02-17 20:35:57 WARNING (MainThread) [mysensors] Node 131 is unknown
    2020-02-17 20:35:57 DEBUG (MainThread) [mysensors] Receiving 131;9;1;0;24;3.36
    2020-02-17 20:35:57 WARNING (MainThread) [mysensors] Node 131 is unknown
    2020-02-17 20:36:00 DEBUG (MainThread) [mysensors] Receiving 131;255;3;0;0;79
    2020-02-17 20:36:00 WARNING (MainThread) [mysensors] Node 131 is unknown
    
    
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;255;0;0;17;2.3.2
    2020-02-17 20:36:11 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 255
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;255;3;0;6;0
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Sending 121;255;3;0;6;M
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;255;3;0;11;BME280 Voltmeter 
    2020-02-17 20:36:11 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 255
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;255;3;0;12;1.0.1
    2020-02-17 20:36:11 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 255
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;9;0;0;30;Batt
    2020-02-17 20:36:11 WARNING (MainThread) [mysensors] Not a valid message: Not valid message sub-type: 30 for dictionary value @ data['sub_type']
    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;0;0;0;6;Temperatur
    2020-02-17 20:36:11 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 0
    2020-02-17 20:36:12 DEBUG (MainThread) [mysensors] Receiving 121;1;0;0;7;Luftfeuchtigkeit
    2020-02-17 20:36:12 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 1
    2020-02-17 20:36:12 DEBUG (MainThread) [mysensors] Receiving 121;2;0;0;8;Luftdruck
    2020-02-17 20:36:12 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 2
    2020-02-17 20:36:12 DEBUG (MainThread) [mysensors] Receiving 121;9;1;0;38;3.43
    2020-02-17 20:36:12 WARNING (MainThread) [mysensors] Child 9 is unknown
    2020-02-17 20:36:13 DEBUG (MainThread) [mysensors] Receiving 121;255;3;0;0;90
    2020-02-17 20:36:13 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 255
    2020-02-17 20:36:13 DEBUG (MainThread) [mysensors] Receiving 121;2;1;0;4;1008.92
    2020-02-17 20:36:13 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 2
    2020-02-17 20:36:13 INFO (MainThread) [homeassistant.components.mysensors] Adding new devices: [<Entity BME280 Voltmeter 121 2: None>]
    2020-02-17 20:36:13 DEBUG (MainThread) [homeassistant.components.mysensors.device] Entity update: BME280 Voltmeter 121 2: value_type 4, value = 1008.92
    

    (I set apart the lines where the node is registered)

    So, three children (ids 0-2) are registered/updated withut problems, those are standard readings from a BME280. Nevertheless, child id 9 , which is presented with

    void presentation()
    {
      sendSketchInfo("BME280 Voltmeter", "1.0.1");
      present(CHILD_ID_VOLT, S_MULTIMETER, "Batt");
      present(CHILD_ID_TEMP, S_TEMP, "Temperatur");
      present(CHILD_ID_HUM, S_HUM, "Luftfeuchtigkeit");
      present(CHILD_ID_BARO, S_BARO,"Luftdruck");
    }
    

    get's a

    2020-02-17 20:36:11 DEBUG (MainThread) [mysensors] Receiving 121;9;0;0;30;Batt
    2020-02-17 20:36:11 WARNING (MainThread) [mysensors] Not a valid message: Not valid message sub-type: 30 for dictionary value @ data['sub_type']
    

    from HASS 😞 . I do really not know what might cause this, also I find this error message slightly unhelpful (which message data is received by HASS? What is message sub-type 30, and what would be expected here?...). So, the child is not added and further sent values are discarded...

    At this point, I do not know how to go on, but have found a workaround; I'm sending the Voltage as a custom sensor value and give it a manual unit of "V" in HASS to get a graph there. Works ok, but I'm really curious why this is so stubbornly not working...

    Thanks again for all your reassurance regarding the sketch and further ideas,

    Joost



  • Just a quick addition esp. withregard to @Boots33 :
    Also followed your advice and edited the persistence file while HASS was in persistence:false mode and filled in your fields.

    When I restarted, I got this in my log:

    020-02-17 22:15:40 DEBUG (MainThread) [mysensors] Receiving 121;0;1;0;38;3.38
    2020-02-17 22:15:40 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 121 child 0
    2020-02-17 22:15:40 WARNING (MainThread) [homeassistant.components.mysensors.helpers] Child type 30 is not supported
    2020-02-17 22:15:42 DEBUG (SyncWorker_11) [mysensors.persistence] Saving sensors to persistence file /config/mysensors.json
    

    I concluded from this that the persistence file edit was successfull (HASS knows about the child id now), but somehow the child type 30 (which I suppose corresponds to S_Multimeter as in MySensors Serial API) is for whatever reason not supported by my installation (?? - this is a hassio on a raspberry 3, updated to latest HASS version.


  • Hero Member

    @Joost do you have hassio set to use V2.3 of the mysensors protocol in the configuration.yaml ?



  • @Boots33 Wow, that's it!! I would never have found this by myself (as numerous hours of trial and error have already proven)... Thank you very much for staying with me and finally finding the solution!!

    So, I'm pretty relieved to finally have this working and am just left wondering why oh why there is a default to a (deprecated?) Mysensors API version, with so little notice regarding this detail in the setup instructions...

    Thanks again,

    Joost


  • Hero Member

    @Joost great news! always a good feeling when you find a solution to a pesky problem 🙂


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 8
  • 2
  • 3

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts