Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HVAC thermostat is not controllable from HASS #275

Open
andrewhack opened this issue Dec 4, 2024 · 4 comments
Open

HVAC thermostat is not controllable from HASS #275

andrewhack opened this issue Dec 4, 2024 · 4 comments

Comments

@andrewhack
Copy link

Hi,
Am I missing something - can't control the target temperature from HASS.
Changing the mode is working fine.
I am using the provided HVAC example with Arduino Mega and Ethernet shield.

These are the features which I am using

HAHVAC::TargetTemperatureFeature | HAHVAC::PowerFeature | HAHVAC::ModesFeature

image

@andrewhack
Copy link
Author

My investigation shows that mqtt "mode" and "target temp" entries are missing, just "current temp" is available.
I assume there is no initial or not complete mqtt setup.

Can somebody assist here?

@MarshallVisions
Copy link

MarshallVisions commented Dec 16, 2024

am having the same problem... find a solution by any chance? mine worked for over a year but stopped working. I think it stopped working after I started a new Home Assistant.

@simbycube
Copy link

same problem for me too.

@MarshallVisions
Copy link

MarshallVisions commented Jan 13, 2025

I seem to have gotten it working with the following example code. I think I needed to delete my old device with my actual thermostat code for current temperature to also start working. Probably because I think they were both getting the same uniqueid. Will do some more investigating.

#include <WiFi.h>
#include <ArduinoHA.h>

#define mqttServer IPAddress(192, 168, 0, 126)
const int mqttPort = 1883;
const char *mqttUser = "MQTT-User";
const char *mqttPassword = "3t3rn!tY";
#define WIFI_SSID       "MV-Terminal_2.4GHz"
#define WIFI_PASSWORD   "3t3rn!tY"
// #include <WiFiClient.h>

WiFiClient client;
HADevice device;
HAMqtt mqtt(client, device);
// 
// By default HAHVAC supports only reporting of the temperature.
// You can enable feature you need using the second argument of the constructor.
// Please check the documentation of the HAHVAC class.
HAHVAC hvac(
  "hvac-test",
  HAHVAC::TargetTemperatureFeature
);

unsigned long lastTempPublishAt = 0;
float lastTemp = 0;

void onTargetTemperatureCommand(HANumeric temperature, HAHVAC* sender) {
    float temperatureFloat = temperature.toFloat();

    Serial.print("Target temperature: ");
    Serial.println(temperatureFloat);

    sender->setTargetTemperature(temperature); // report target temperature back to the HA panel
}

void onPowerCommand(bool state, HAHVAC* sender) {
  if (state) {
    Serial.println("Power on");
  } else {
    Serial.println("Power off");
  }
}

void onModeCommand(HAHVAC::Mode mode, HAHVAC* sender) {
    Serial.print("Mode: ");
    if (mode == HAHVAC::OffMode) {
        Serial.println("off");
    } else if (mode == HAHVAC::AutoMode) {
        Serial.println("auto");
    } else if (mode == HAHVAC::CoolMode) {
        Serial.println("cool");
    } else if (mode == HAHVAC::HeatMode) {
        Serial.println("heat");
    } else if (mode == HAHVAC::DryMode) {
        Serial.println("dry");
    } else if (mode == HAHVAC::FanOnlyMode) {
        Serial.println("fan only");
    }

    sender->setMode(mode); // report mode back to the HA panel
}

void setup() {
    Serial.begin(9600);
    Serial.println("Starting...");

    // Unique ID must be set!
    byte mac[6];
    WiFi.macAddress(mac);
    device.setUniqueId(mac, sizeof(mac));

    // connect to wifi
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500); // waiting for the connection
    }
    Serial.println();
    Serial.println("Connected to the network");

    // set device's details (optional)
    device.setName("NodeMCU");
    device.setSoftwareVersion("1.0.0");

    // assign callbacks (optional)
    hvac.onTargetTemperatureCommand(onTargetTemperatureCommand);
    hvac.onPowerCommand(onPowerCommand);
    hvac.onModeCommand(onModeCommand);

    // configure HVAC (optional)
    hvac.setName("My HVAC");
    hvac.setMinTemp(10);
    hvac.setMaxTemp(30);
    hvac.setTempStep(0.5);

    // You can set retain flag for the HA commands
    // hvac.setRetain(true);

    // You can choose which modes should be available in the HA panel
    // hvac.setModes(HAHVAC::OffMode | HAHVAC::CoolMode);

  mqtt.begin(mqttServer, mqttPort, mqttUser, mqttPassword);

}
void loop() {
    mqtt.loop();

    if ((millis() - lastTempPublishAt) > 3000) {
        hvac.setTargetTemperature(lastTemp);
        lastTempPublishAt = millis();
        lastTemp += 0.5;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants