[SOLVED] Setpoint does not update

Hello everyone
I cannot set the temperature from OH2, the setpoint value does not change when I press the arrow UP or DOWN, while from the Nextion display, openhab it is updated. The other commands work (es. thermostat ON/OFF)
my system consists of: OpenHab2 , NodeMCU / Nextion display, mosquitto broker

arduino void:

void callback(char* topic, byte* payload, unsigned int length) {
  topicString = topic;
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  
  // setpoint
  if (topicString == "openhab/dst2/setpoint/command") {
    if ((char)payload[1] == 'U')  {
      setpoint++;
      myNextion.setComponentText ("t0", String (setpoint));
      
    }
    if (topicString == "openhab/dst2/setpoint/command") {
      if ((char)payload[1] == 'D')  {
        setpoint--;
        myNextion.setComponentText ("t0", String (setpoint));
      }
    }
  }
}

item:

Number Temperature_SP        "regola set point [%.1f °C]"    <temperature>    (Home,TSTAT, Corridoio)                      ["TargetTemperature"] 	  {mqtt=">[mosquitto:openhab/dst2/setpoint/command:command:*:default], <[mosquitto:openhab/dst2/setpoint/state:state:default] "}

sitemap:

Setpoint item=Temperature_SP             step=0.5 minValue=5 maxValue=35

rule:

rule "temperature setpoint"
when

   Item Temperature_SP received command

then

   var Number CurrentTemp
   var Number NewTemp

CurrentTemp =  Temperature_SP.state

if (receivedCommand.toString == "UP" ) {
		NewTemp = CurrentTemp + 1.0
		if (NewTemp > MaxTemperature) {
			NewTemp = MaxTemperature
			publish("mosquitto", "/openhab/dst2/setpoint", NewTemp.toString)
			}
		  }
if (receivedCommand.toString == "DOWN" ) {
		NewTemp = CurrentTemp - 0.5
		if (NewTemp < MinTemperature) {
			NewTemp = MinTemperature
			publish("mosquitto", "/openhab/dst2/setpoint", NewTemp.toString)
			}
			}
end

rule "temperature setpoint Status Rule" //Update the topic 'RemoteSetTemperature' with the latest set temp
    when
    	Item Temperature_SP received update
    then
	    var Number CurrentTemp
		CurrentTemp = Temperature_SP.state
		
    end

Thanks for your help
Davide

Sitemap Setpoint widget does not work without a “starting point” number e.g. if your Item state is NULL, the setpoint won’t work.
It’s not really clear if you can read a value from device or not.

These are MQTT binding version 1 configurations, and will not work with MQTT version 2 things and channels binding.

Setpoint widgets do not generate UP etc. commands, they send the new numeric value as a command.

You need to install the MQTT publish action to use that.

It’s not clear why you would want to configure your Item to send outbound command topics and also a rule to do the same at the same time?
Are you mixing parts of old tutorials here?

Looking in openhab.log and events.log should give you a clearer view.

i want to set the temperature from OH from the setpoint item.
I thinked that need a rule to change the item setpoint value, (the rule come from an old post) because if i push on the arrow the value not change. Example, If i push the arrow Send the new temperature value (i see it with “MQTTlens”) but not change on nextion display connected at ESP2866 and not even on basicUI panel, while if i change the value from nextion display, OH update correctly the setpoint item

I believe it should be:

    if ((char)payload[0] == 'U')  {

Indexing starts at 0 not 1

modified, but not resolve the problem

on the arduino serial windows display this message when i push setpoin item:
Message arrived [openhab/dst2/setpoint/command] 6.5

Good, what does that tell you?

I did say that sitemap Setpoint widget does not send up/down, it sends the new numeric as a command.
This can be seen in your events.log

arduino:

Message arrived  [openhab / dst2 / setpoint / command] 6.5

OH system log:

Item 'Temperature_SP' received command 12.5

I saw the new number in event.log as you said. Arduino from the serial monitor shows that it receives it, but does not set it on the nextion and OH does not update it, on the contrary yes. Setpoint receives a starting point number from arduino when it connects and the item is not NULL, I don’t understand if the error is on OH or on the sketch and why I should use version 2 of the MQTT binding, when I now use Mosquitto and MQTT version 1 also with other devices (arduino, shelly, sonoff) and everything works correctly.
Sorry if I don’t understand, and maybe I’ll explain myself wrongly, but until now I have only configured ON / OFF commands with MQTT and arduino and not numerical values that change

Nobody said you had to use MQTT 2 binding. You just didn’t provide info to start with.

I know nothing about arduino sketches, but yours seems to look for an U/D character that is not present in the message.

exact. I think the problem is right in the Payload. I thought, wrongly, that receiving the message UP or DOWN (U / D) updated the numerical value of the item setpoint. I think the error is in the sketck, which should recognize a FLOAT type variable as the temperature, in fact it receives the message as a number but not understanding the message, arduino, then it doesn’t update the setpoint on OH

We are getting back to changing the Arduino code as advised above

i change it, but not resolve the problem

Can you post the code, please?
What is the serial output of the arduino, did you insert debug serial outputs in your code to see what is going on? Does it work?

here it is, consider that I’m still working on it so it’s not tidy and clean.
I think the error is in “void callback” at the setpoint entry. I put a question mark in the payload value

#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <ESP8266mDNS.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <IRsend.h>
#include <DHT.h>

//NEXTION
#include <SoftwareSerial.h>
#include <Nextion.h>
SoftwareSerial nextion(4, 2);// Nextion TX to pin 2 and RX to pin 3 of Arduino
Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE, 20);

IPAddress staticIP(xxxxx);
IPAddress gateway(xxxxxxx);
IPAddress subnet(xxxxx);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2(8, 8, 4, 4);

const char* ssid =        "xxxxxxx";
const char* password =    "xxxxxxxx";
const char* mqtt_server = "xxxxxxxx";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

unsigned long previousMillis = 0;    // intervallo lettura temperature
unsigned long previousMillis2 = 0;   //  intervallo timer
unsigned long previousMillis3 = 0;   //  intervallo MQTT
const long interval = 10000;         //intervallo 10 secondi lettura temperature
const long interval2 = 1000;         //imposta intervallo timer ++ a un secondo
const long interval3 = 11000;         //intervallo 1 minuto MQTT

String topicString = "";

const int relayPin = 14; //D5
int stato_rele = 0;

float setpoint = 0;
float bloccaLetturaSet = 0;
float bloccaLetturaTemp = 0;
float bloccaLetturaUmy = 0;
float SETpointAvvio = 16.00;
float SETpointECO = 20.00;
float SETpointNORMAL = 22.00;
float SETpointCOMFORT = 25.00;
float SETpointTIMER = 30.00;
float MaxTemp = 35.00;
float MinTemp = 15.00;

int vecchio_stato = 0;
int bloccadimmer = 0;
int dimmer = 0;

int sistema = 0;
int flag_sistema = 0;
int flag_timer = 0;
int val;
int timerPlus, timerMinus;

WiFiUDP ntpUDP;
int16_t utc = 2; //UTC (1 ora solare) (2 ora legale)
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", utc * 3600, 60000);

const char* inTopic_sistema            = "openhab/dst2/sistema/command";
const char* inTopic_setpoint           = "openhab/dst2/setpoint/command";
const char* switchTopic_sistema        = "openhab/dst2/sistema/state";
const char* switchTopic_temperature    = "openhab/dst2/temperature/state";
const char* switchTopic_humidity       = "openhab/dst2/humidity/state";
const char* switchTopic_setpoint       = "openhab/dst2/setpoint/state";


//**********************************************  funzioni ***************************************************************
void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.config(staticIP, gateway, subnet, dns1, dns2);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    for (int i = 0; i < 500; i++) {
      delay(1);
    }
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());
}

void refreshTheme() {

  float humidity = dht.readHumidity();
  float temperatura = dht.readTemperature();
  if (sistema == 0) {
    //  myNextion.sendCommand("page page2");//page attendere
    //  delay(500);
    digitalWrite(relayPin, LOW);
    myNextion.sendCommand("page page0");//home
    myNextion.setComponentText("t0", String(setpoint));
    myNextion.setComponentText("t3", String(timerPlus ) + String("m"));
    timeClient.update();
    myNextion.setComponentText("t4", String(timeClient.getFormattedTime()));
    myNextion.sendCommand("b0.picc=0");//power off
    myNextion.setComponentText("t8", "");//fiamma off
  }
  if ( sistema == 1) {
    //   myNextion.sendCommand("page page2");//page attendere
    //  delay(500);
    digitalWrite(relayPin, LOW);
    myNextion.sendCommand("page page0");//home
    myNextion.setComponentText("t0", String(setpoint));
    myNextion.setComponentText("t3", String(timerPlus) + String("m"));
    timeClient.update();
    myNextion.setComponentText("t4", String(timeClient.getFormattedTime()));
    myNextion.sendCommand("b0.picc=1");//power on
    myNextion.setComponentText("t8", "");//fiamma off
    //myNextion.setComponentText("t9", "");
  }
  if ( sistema == 2) {
    //  myNextion.sendCommand("page page2");//page attendere
    //  delay(500);
    myNextion.sendCommand("page page0");//home
    myNextion.setComponentText("t0", String(setpoint));
    myNextion.setComponentText("t3", String(timerPlus) + String("m"));
    timeClient.update();
    myNextion.setComponentText("t4", String(timeClient.getFormattedTime()));
    myNextion.sendCommand("b0.picc=1");//power on
    myNextion.setComponentText("t9", "");//fiamma on
  }
  myNextion.setComponentText("t1", String(temperatura));
  myNextion.setComponentText("t2", String(humidity));
}

void lettura_temperatura() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    float humidity = dht.readHumidity();
    float temperatura = dht.readTemperature();
    if (temperatura  == bloccaLetturaTemp)  { //NON FARE NULLA
    } else {
      if ( temperatura != bloccaLetturaTemp) {
        myNextion.setComponentText("t1", String(temperatura));
        client.publish(switchTopic_temperature, String(temperatura).c_str(), true);
      }
      bloccaLetturaTemp = temperatura;
    }
    if (temperatura > MinTemp) {
      myNextion.setComponentText("t10", "");
    } else {
      myNextion.setComponentText("t11", "");
    }
    if (humidity  == bloccaLetturaUmy) {  //SE CAMBIA UMY AGGIORNA IL NEXTION
    } else {
      if ( humidity != bloccaLetturaUmy) {
        myNextion.setComponentText("t2", String(humidity));
        client.publish(switchTopic_humidity, String(humidity).c_str(), true);
      }
      bloccaLetturaUmy = humidity;
    }
    myNextion.setComponentText("t3", String(timerPlus / 60) + String("m"));
    timeClient.update();
    myNextion.setComponentText("t4", String(timeClient.getFormattedTime()));
    previousMillis = currentMillis;
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  topicString = topic;
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  // sistema
  float temperatura_casa = dht.readTemperature();
  if (topicString == "openhab/dst2/sistema/command") {
    if ((char)payload[1] == 'N')  {  //sistema on
      if (flag_sistema == 0)  {
        sistema = 1;
        flag_sistema = 1;
        myNextion.sendCommand("b0.picc=1");
        if ((setpoint > temperatura_casa) && (sistema == 1)) {
          digitalWrite(relayPin, HIGH);
          sistema = 2;
          refreshTheme();
        }

        if ((setpoint < temperatura_casa) && (sistema == 2)) {
          digitalWrite(relayPin, LOW);
          sistema = 1;
          refreshTheme();
        }
      }
    }
  }
  if (topicString == "openhab/dst2/sistema/command") {
    if ((char)payload[1] == 'F')  {  //sistema off
      if ((flag_sistema == 1) || (flag_sistema == 2)) {
        sistema = 0;
        flag_sistema = 0;
        flag_timer = 0;
        timerPlus = 0;
        digitalWrite(relayPin, LOW);
        myNextion.sendCommand("b0.picc=0");
        refreshTheme();
      }
    }
  }

  // setpoint
  if (topicString == "openhab/dst2/setpoint/command") {
    if ((char)payload[0] == '?')  {
      //????? code here
      myNextion.setComponentText ("t0", String (setpoint));
      
    }
    if (topicString == "openhab/dst2/setpoint/command") {
      if ((char)payload[0] == '?')  {
       //????? code here
        myNextion.setComponentText ("t0", String (setpoint));
      }
    }
  }
}


void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("termostato")) {
      Serial.println("connected");
      //Subscribe to incoming commands
      client.subscribe(inTopic_sistema);
      client.subscribe(inTopic_setpoint);

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      for (int i = 0; i < 5000; i++) {

        delay(1);
      }
    }
  }
}

//**********************************************  SETUP ***************************************************************

void setup() {

  myNextion.init();
  myNextion.sendCommand("page page2");//page attendere

  Serial.begin(9600);
  setup_wifi();
  timeClient.begin();
  client.setServer(mqtt_server, xxxxx); //connect to MQTT
  client.setCallback(callback);

  String line = "";
  dht.begin();
  float humidity = dht.readHumidity();
  float temperatura = dht.readTemperature();

  while (isnan(humidity) || isnan(temperatura)) {  // t o h a volte è nan, non inviare il valore nan agli slot
    Serial.println(temperatura);
    Serial.println(humidity);
  }

  myNextion.sendCommand("page page0");//home
  myNextion.setComponentText("t6", "");
  myNextion.setComponentText("t8", "");
  myNextion.setComponentText("t10", "");
  myNextion.setComponentText("t3", String("----"));
  timeClient.update();
  myNextion.setComponentText("t4", String(timeClient.getFormattedTime()));
  Serial.println(timeClient.getFormattedTime());

  myNextion.sendCommand("dims=100");

  myNextion.setComponentText("t1", String(temperatura));
  myNextion.setComponentText("t2", String(humidity));
  if (WiFi.status() != WL_CONNECTED) {
    myNextion.setComponentText("t6", "");
  } else {
    myNextion.setComponentText("t7", "");
  }

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);

  setpoint++;
  myNextion.setComponentText ("t0", String (setpoint));

}

//**********************************************  LOOP ***************************************************************

void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();
  lettura_temperatura();
  stato_rele = (digitalRead, relayPin);


  //////////////////////////////inizio nextion listen ///////////////////////////////

  String message = myNextion.listen(); {
    if (message != "") {
      Serial.println(message);

      if (message == "65 1 7 1 ff ff ff") {
        refreshTheme();
      }

      /////////////////////////// on-off sistema///////////////////////////////

      if (message == "65 10 1 ff ff ff") {
        if (flag_sistema == 0)  {
          sistema = 1;
          flag_sistema = 1;
          myNextion.sendCommand("b0.picc=1");
          refreshTheme();
          client.publish(switchTopic_sistema, "ON", true);
        }
        else if ((flag_sistema == 1) || (flag_sistema == 2)) {
          sistema = 0;
          flag_sistema = 0;
          flag_timer = 0;
          timerPlus = 0;
          myNextion.sendCommand("b0.picc=0");
          refreshTheme();
          client.publish(switchTopic_sistema, "OFF", true);
        }
      }

      /////////////////////////////////// setpoint //////////////////////////////////

      if (message == "65 2 1 ff ff ff") {
        setpoint++;
        myNextion.setComponentText ("t0", String (setpoint));        
        client.publish(switchTopic_setpoint, String(setpoint).c_str(), true);
      }
      if (message == "65 3 1 ff ff ff") {
        setpoint--;
        myNextion.setComponentText ("t0", String (setpoint));
        client.publish(switchTopic_setpoint, String(setpoint).c_str(), true);
      }

      //////////////////////// sistema on-off con il setpoint /////////////////////////

      float temperatura_casa = dht.readTemperature();
      if ((setpoint > temperatura_casa) && (sistema == 1)) {
        digitalWrite(relayPin, HIGH);
        sistema = 2;
        refreshTheme();
      }

      if ((setpoint < temperatura_casa) && (sistema == 2)) {
        digitalWrite(relayPin, LOW);
        sistema = 1;
        refreshTheme();
      }
      myNextion.setComponentText("t4", String(timeClient.getFormattedTime()));

      //////////////////////// timer /////////////////////////////////////////////

      if (message == "65 1 1 1 ff ff ff") {  //timer 1 ora
        setpoint = SETpointTIMER;
        myNextion.setComponentText ("t0", String (setpoint));
        sistema = 2;
        flag_sistema = 1;
        flag_timer = 1;
        digitalWrite(relayPin, HIGH);
        refreshTheme();
      }

      if (message == "65 1 2 1 ff ff ff") {  //timer 2 ore
        setpoint = SETpointTIMER;
        myNextion.setComponentText ("t0", String (setpoint));
        sistema = 2;
        flag_sistema = 1;
        flag_timer = 1;
        digitalWrite(relayPin, HIGH);
        refreshTheme();
      }

      if (message == "65 1 3 1 ff ff ff") {  //timer 3 ore
        setpoint = SETpointTIMER;
        myNextion.setComponentText ("t0", String (setpoint));
        sistema = 2;
        flag_sistema = 1;
        flag_timer = 1;
        digitalWrite(relayPin, HIGH);
        refreshTheme();
      }

      //////////////////////// preimpostate //////////////////////////////////////

      if (message == "65 1 4 1 ff ff ff") {  //economy 20 gradi
        setpoint = SETpointECO;
        float temperatura_casa = dht.readTemperature();
        myNextion.setComponentText ("t0", String (setpoint));
        sistema = 2;
        flag_sistema = 1;
        refreshTheme();
        myNextion.setComponentText("t8", "");//fiamma off
        if (setpoint > temperatura_casa) {
          digitalWrite(relayPin, HIGH);
          myNextion.setComponentText("t9", "");//fiamma on
        }
      }
      if (message == "65 1 5 1 ff ff ff") {  //normal 22 gradi
        setpoint = SETpointNORMAL;
        float temperatura_casa = dht.readTemperature();
        myNextion.setComponentText ("t0", String (setpoint));
        sistema = 2;
        flag_sistema = 1;
        refreshTheme();
        myNextion.setComponentText("t8", "");//fiamma off
        if (setpoint > temperatura_casa) {
          digitalWrite(relayPin, HIGH);
          myNextion.setComponentText("t9", "");//fiamma on
        }
      }
      if (message == "65 1 6 1 ff ff ff") {  //confort 25 gradi
        setpoint = SETpointCOMFORT;
        float temperatura_casa = dht.readTemperature();
        myNextion.setComponentText ("t0", String (setpoint));
        sistema = 2;
        flag_sistema = 1;
        refreshTheme();
        myNextion.setComponentText("t8", "");//fiamma off
        if (setpoint > temperatura_casa) {
          digitalWrite(relayPin, HIGH);
          myNextion.setComponentText("t9", "");//fiamma on
        }
      }

    }
  }
  //////////////////////// timer /////////////////////////////////////////////

  if (flag_timer == 1) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis2 >= interval2) {
      previousMillis2 = currentMillis;
      timerPlus++;
      if (timerPlus >= 3600) {
        sistema = 0;
        flag_timer = 0;
        timerPlus = 0;
        refreshTheme();
      }
    }
  }

  if (flag_timer == 1) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis2 >= interval2) {
      previousMillis2 = currentMillis;
      timerPlus++;
      if (timerPlus >= 7200) {
        sistema = 0;
        flag_timer = 0;
        timerPlus = 0;
        refreshTheme();
      }
    }
  }

  if (flag_timer == 1) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis2 >= interval2) {
      previousMillis2 = currentMillis;
      timerPlus++;
      if (timerPlus >= 10800) {
        sistema = 0;
        flag_timer = 0;
        timerPlus = 0;
        refreshTheme();
      }
    }
  }




  //Serial.println("timerPlus" + String(timerPlus));
  //Serial.println("flag_timer" + String(flag_timer));
  //Serial.println("interval2" +String(previousMillis2));
  //Serial.println(temperatura);
  //Serial.println("flag_sistema" + String(flag_sistema));
  // Serial.println("sistema" + String(sistema));
  //  Serial.println("stato rele" + String(stato_rele));
 // Serial.print((char)payload[i]);

}

Why do you test several times for the same topic?
Put all your code inside the topic if test

if (topic == "ddddddd" {
    if ((char)payload[1] == "N" {
    }
    else if ((char)payload[1] == "F" {
    }
    else {
        //convert payload to float
        //do you stuff
    }

You have a problem there as you suspected:

  if (topicString == "openhab/dst2/setpoint/command") {
    if ((char)payload[0] == '?')  {
      //????? code here
      myNextion.setComponentText ("t0", String (setpoint));
      
    }
    if (topicString == "openhab/dst2/setpoint/command") {
      if ((char)payload[0] == '?')  {
       //????? code here
        myNextion.setComponentText ("t0", String (setpoint));
      }
    }
  }

Capture ALL the payload into a String
Cast the string into a float

Solved!!! post the code:

  // setpoint
  if (topicString == "openhab/dst2/setpoint/command") {
    payload[length] = '\0'; 
    int temperature_set = atoi((char *)payload);
    //Serial.println("temperature set" + String(temperature_set));
    setpoint = temperature_set;
    //Serial.println(setpoint);
    myNextion.setComponentText ("t0", String (setpoint));
  }
}

thank you all for your help
Davide

Don’t forget your “temperature setpoint” rule does nothing useful, and can be removed.

right, already canceled