dawidchyrzynski / arduino-home-assistant

ArduinoHA allows to integrate an Arduino/ESP based device with Home Assistant using MQTT.
https://dawidchyrzynski.github.io/arduino-home-assistant/
GNU Affero General Public License v3.0
480 stars 116 forks source link

Allow easier definition and update of devices #180

Closed ChristofSchmid closed 7 months ago

ChristofSchmid commented 1 year ago

I am using ArduinoHA with 40+ sensors. My attempt to streamline definition and use of HASensor and HASensorNumber by using vectors screws the functionality of the lib. Is there a way to make the ArduinoHA project work unsing vectors or anotherway to streamline definition and processing insead of writing every single line of code as in the samples?

  WiFiClient wClient;
  HADevice haDevice;
  HAMqtt mqtt(wClient, haDevice, 48);

  std::vector<HASensor*> sName;
  std::vector<HASensorNumber*> sTime;
  std::vector<HASensorNumber*> sSoil;
  std::vector<HASensorNumber*> sVolt;
  std::vector<HASensorNumber*> sBatt;
  std::vector<HASensorNumber*> `sTemp;

void setup(){

   for (int i = 1; i <= 8; i++) {

        sName.push_back( new HASensor(("Sensor" + std::to_string(i)).c_str()));
        sTime.push_back( new HASensorNumber(("Time" + std::to_string(i)).c_str()));
        sSoil.push_back( new HASensorNumber(("Soil" + std::to_string(i)).c_str()));
        sVolt.push_back( new HASensorNumber(("Volt" + std::to_string(i)).c_str()));
        sBatt.push_back( new HASensorNumber(("Batt" + std::to_string(i)).c_str()));
        sTemp.push_back( new HASensorNumber(("Temp" + std::to_string(i)).c_str()));
    }
}

//Init sensors
void initmqtt(){

    byte mac[WL_MAC_ADDR_LENGTH];
    WiFi.macAddress(mac);
    haDevice.setUniqueId(mac, sizeof(mac));

    haDevice.setName("Irrigation Controller");
    haDevice.setSoftwareVersion(FIRMWARE_VERSION);
    haDevice.setManufacturer("CS");
    haDevice.setModel("ESP32");
    haDevice.setAvailability(true);

    haDevice.enableSharedAvailability();
    haDevice.enableLastWill();

    mqtt.onMessage(onMqttMessage);
    mqtt.onConnected(onMqttConnected);
    mqtt.setDiscoveryPrefix("ESP_Irrigation");
    mqtt.setDataPrefix("SoilSensors");

  for (int i = 0; i < 8; i++ ){

    String cn = "S" + String(i+1);

    sName[i]->setIcon("mdi:home");
    sName[i]->setName( (cn + "_Name").c_str() );  

    sTime[i]->setIcon("mdi:clock-time-eight-outline");
    sTime[i]->setName( (cn + "_Time").c_str()  );
    sTime[i]->setUnitOfMeasurement("");

    sSoil[i]->setIcon("mdi:water-percent");
    sSoil[i]->setName((cn + "_Soil_Hum").c_str());
    sSoil[i]->setUnitOfMeasurement("%");

    sTemp[i]->setIcon("mdi:thermometer");
    sTemp[i]->setName((cn + "_Amb_Temp").c_str());
    sTemp[i]->setUnitOfMeasurement("°C");

    sVolt[i]->setIcon("mdi:battery-50");
    sVolt[i]->setName((cn + "_Batt").c_str() );
    sVolt[i]->setUnitOfMeasurement("%");

  }

  for (int i = 1; i <= 8; i++){
    updatemqtt(i);
  }
  mqtt.begin("192.168.1.227", 1883 , "mqtt-user", "mqtt-user#");
} 

//update Sensors
void updatemqtt(int sensor){
Serial.println("HA Sensor " + String(sensor) + " update");
    sName[sensor-1]->setValue( SensorNames[sensor-1].c_str()); 
    sTime[sensor-1]->setValue( SoilSensorData[sensor-1].nEpoche , true );
    sSoil[sensor-1]->setValue( SoilSensorData[sensor-1].soilHum, true );  
    sTemp[sensor-1]->setValue( SoilSensorData[sensor-1].ambTemp, true );
    sVolt[sensor-1]->setValue( SoilSensorData[sensor-1].batVolt, true );
}

void loop() {
    mqtt.loop();
}
dawidchyrzynski commented 7 months ago

@ChristofSchmid The library seamlessly accommodates any pattern for storing entity pointers, whether through vectors, maps, or even custom container classes. I've integrated it into numerous complex projects without encountering any issues.

Considering the example above, it's likely that you've reached the default limit for devices amount.

https://dawidchyrzynski.github.io/arduino-home-assistant/documents/library/device-types.html#limitations