WThermostatBeca icon indicating copy to clipboard operation
WThermostatBeca copied to clipboard

ME81H set command sometimes doesn't work

Open roleoroleo opened this issue 2 years ago • 125 comments

ME81H.16 with 1.20n.

This is the strange behavior:

  • I set a parameter sending an mqtt command with MQTT Explorer (for example targetTemperature): {"targetTemperature":21.0}
  • The thermostat understands the command
  • The thermostat replies with the state in the "properties" topic where I can see the parameter correctly changed (targetTemperature = 21.0
  • After a second, the thermostat sends another state message where the parameter comes back to the previous value.

If I try to set the value 2 or 3 times, finally it works.

Anyone have the same problem?

roleoroleo avatar Nov 14 '21 18:11 roleoroleo

This is a set command example and the log. Topic: climate_general/thermostat/set Payload: {"schedulesMode": "off"}

Log of a subscribed client:

root@mqtt:~# mosquitto_sub -v -h 127.0.0.1 -t climate_general/#
climate_general/thermostat/set {"schedulesMode": "off"}
climate_general/thermostat/properties {"idx":"climate_general","ip":"192.168.1.227","alive":true,"firmware":"1.20n","temperature":20.50,"targetTemperature":20.00,"deviceOn":true,"schedulesMode":"off","locked":false,"state":"heating","systemMode":"heat","sensorSelection":"internal"}
climate_general/thermostat/properties {"idx":"climate_general","ip":"192.168.1.227","alive":true,"firmware":"1.20n","temperature":20.50,"targetTemperature":20.00,"deviceOn":true,"schedulesMode":"auto","locked":false,"state":"heating","systemMode":"heat","sensorSelection":"internal"}

Same problem if I use Topic: climate_general/thermostat/set/schedulesMode Payload: off

roleoroleo avatar Nov 15 '21 18:11 roleoroleo

I can confirm that I am experiencing same behaviour.

MrNomNom avatar Nov 27 '21 11:11 MrNomNom

Hello, I can confirm that I have also the same issue, sometimes its firing on the first command, sometimes I need to send the MQTT 3-4 times. If I can help debug it somehow let me know!

fraintt avatar Dec 09 '21 11:12 fraintt

Hello,

I have a ME102H.31 thermostat that came with a WBR3 module that I swapped with an ESP module & with WThermostatBeca 1.24beta.

I can confirm that I also have this problem. Sometimes I have to send the set command 2-3 times for it to stay. This is for both set/targetTemperature and set/deviceOn.

Maybe related to something @AlbertWeterings mentioned in the below link ... https://github.com/klausahrenberg/WThermostatBeca/issues/246#issuecomment-1002149625

"undoable with this flood on data being send from the mcu. I noticed in a curtail WiFi switch a year ago that the wifi module has to send some kind of acknowledge message back to the MCU to stop it from repeating the same transmission over and over again."

Maybe the newer MCUs send to much data to the ESP module and overload it/slow it down?

PhilipTS avatar Dec 29 '21 10:12 PhilipTS

Small video of the issue ... very apparent after 2:00

philip-table_20211229_2018.zip

PhilipTS avatar Dec 29 '21 18:12 PhilipTS

This issue can be solved by changing two interval times in WTuyaDevice.h `

  • #define HEARTBEAT_INTERVAL 10000
  • Changed to #define HEARTBEAT_INTERVAL 20000
  • #define MINIMUM_INTERVAL 2000
  • Changed to #define MINIMUM_INTERVAL 10000 ` HEARTBEAT_INTERVAL is being send to the MCU to tell the TUYA device is still alive, I doubled the time as it still seems enough for the MCU to know the TUYA device is still alive. MINIMUM_INTERVAL is querying the MCU for settings. It is useful to have this at a 2 second interval during debug activities but it is meaningless during normal operation.

@PhilipTS You can test this binary

P.S.:You can't change target temperature over MQTT when scheduler is on Auto.

AlbertWeterings avatar Dec 30 '21 08:12 AlbertWeterings

@AlbertWeterings

The WiFi icon is now constantly flashing (with WThermostat_1.0.2_alpha.bin) Maybe HEARTBEAT_INTERVAL 20000 is to high?

PhilipTS avatar Dec 30 '21 08:12 PhilipTS

@AlbertWeterings

After power cycling the thermostat everything is working fine with WThermostat_1.0.2_alpha.bin Will now test to see if the SET value problem is solved :)

As a future enhancement maybe you could do GPI0 2 & 14 go low together? In order not to need the extra wire ... for our next ME102Hs :)

PhilipTS avatar Dec 30 '21 08:12 PhilipTS

@AlbertWeterings

I managed to reproduce the problem. I assume that if we send the set command while the ESP is querying the MCU this set gets lost/reverted? And from the tasmota web log that I attached in issue #246 I can see that the MCU sends a lot when queried. What if some logic was implemented to hold the set command and send it after the query, or resend it if not set?

image

PhilipTS avatar Dec 30 '21 08:12 PhilipTS

@PhilipTS Yes you are right the problem can be reproduced. I also see that when the MCU is being queried when the set target temperature is being send it doesn't pickup the new value. I can make the command send twice by the WIFI module that is not a nice solution but might help. An other option is to do this in for example Node-Red.. We can compare there the value being send from the device with the entered value and resend if there is a mismatch

AlbertWeterings avatar Dec 30 '21 09:12 AlbertWeterings

@AlbertWeterings Now that is a good idea. Resend from Node-Red or in my case with an Automation from HA. But we will have to monitor all set values ... but I guess it will work ...

PhilipTS avatar Dec 30 '21 09:12 PhilipTS

@PhilipTS I tried for the past hour several way's to get this work correctly nothing does. I also noticed that a lot of times the MCU is not executing commands being send. So I give up I think the best way is to check the status messages and resend commands if they where not executed right.

AlbertWeterings avatar Dec 30 '21 10:12 AlbertWeterings

I spent the whole day trying to figure out automations in HA and after many hours of reading and trying I finally fixed the automation. And I tested it and it would catch and fix this error. BUT ... big BUT When I changed the temperature from the thermostat ... my automation detected that THIS is not the value that was set by HA and returned it to the previous value !!! ARGGHHH

Guess i have to to make a time window in which the correction will happen. Something for 2022

PhilipTS avatar Dec 30 '21 17:12 PhilipTS

@PhilipTS i think if you put in a time Windows of 2 to 3 seconds you are safe. The thermostat sends 1 message when the new temperature is set. I noticed it sends two messages right after each other when if puts the temperature back to previous value.

AlbertWeterings avatar Dec 30 '21 19:12 AlbertWeterings

For anybody that has this problem and uses HA below is a dirty fix. It works with both @klausahrenberg and @AlbertWeterings firmware.

The first automation saves the sent targetTemperature in a Helper. The second one compares any received (within 3 second) targetTemperature properties to the Helper. If the values are different it resends the set command.

Update: After testing I made some changes in the Automations (and increased to 9 secs)

In configuration.yaml

climate:
  - platform: mqtt
    unique_id: central_thermostat_ME102H
    name: Central Thermostat
    current_temperature_topic: "AVATTO_ME102H/thermostat/properties/Temperature"
    temperature_state_topic: "AVATTO_ME102H/thermostat/properties/TargetTemperature"
    temperature_command_topic: "AVATTO_ME102H/thermostat/set/TargetTemperature"
    modes: [ "heat", "off" ]
    mode_state_topic: "AVATTO_ME102H/thermostat/properties/deviceOn"
    mode_state_template: >-
        {% if value == 'false' %}
            off
        {% else %}
            heat
        {% endif %}
    mode_command_topic: "AVATTO_ME102H/thermostat/set/deviceOn"
    mode_command_template: >-
        {% if value == 'off' %}
            false
        {% else %}
            true
        {% endif %}
    min_temp: 5
    max_temp: 35

Create a Number Helper with

Name: TargetTemperature
Entity ID: input_number.targettemperature

First Automation

alias: Save Target Temperature
description: 'Save Target Temperature with Helper'
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: climate
      service: set_temperature
      service_data:
        entity_id: climate.central_thermostat
condition: []
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.targettemperature
    data:
      value: '{{ trigger.event.data.service_data.temperature }}'
mode: single

Second Automation

alias: Check Target Temperature
description: 'Check Target Temperature with Helper'
trigger:
  - platform: mqtt
    topic: AVATTO_ME102H/thermostat/properties/TargetTemperature
condition:
  - condition: and
    conditions:
      - condition: template
        value_template: >-
          {{ states('input_number.targettemperature')|float != trigger.payload|float }}
      - condition: template
        value_template: >-
          {{ as_timestamp(now()) - as_timestamp(states.input_number.targettemperature.last_changed) <= 9 }}
action:
  - service: mqtt.publish
    data:
      topic: AVATTO_ME102H/thermostat/set/TargetTemperature
      payload: '{{ states(''input_number.targettemperature'') }}'
mode: single

PhilipTS avatar Dec 30 '21 21:12 PhilipTS

This MCU is ... stubborn ... I increased the time window to 60sec,

In the image below I set the target to 8 and the MCU changed it back 6 times !!! This was going on for 33sec !!!

image

PhilipTS avatar Dec 31 '21 09:12 PhilipTS

And very reproducible, but this fix is more stubborn than the MCU and in the end it sets the target temperature.

image

PhilipTS avatar Dec 31 '21 09:12 PhilipTS

@PhilipTS just a long shot can you try to change only 1 degree at the time and then see what happens?

AlbertWeterings avatar Dec 31 '21 17:12 AlbertWeterings

@AlbertWeterings That is what I did above, I would increase by 1 degree and wait 2-3 sec and then increase and then the up-down began down from the MCU up from HA. when it stabilized I started increasing again by 1 degree. All the decreases are from the MCU.

PhilipTS avatar Dec 31 '21 18:12 PhilipTS

@PhilipTS ah that is bad luck I was hoping for the mcu to be less going it’s own way by only 1 degree move at the time. I think we need data logging from the original tuya module to figure out if we are missing something or this same behavior is there to.

AlbertWeterings avatar Dec 31 '21 19:12 AlbertWeterings

Out of curiosity I Googled for Logic Analyzer and I could NOT believe what I found !!! SparkFun USB Logic Analyzer ... 20 Euros ... 20 Euros for an 8 channel Logic Analyzer !!! I've been out of the game for a long time ... and I like these new toys ... I ordered one :) Now I have to find the courage to swap back the WBR3 module. Or just order a second ME102H.31 for tracing the WBR3 module.

PhilipTS avatar Jan 01 '22 17:01 PhilipTS

@PhilipTS You could just use any TTL to RS232 interface to log the communication between MCU and WBR3 with the free software package Realterm It is indeed needed to return to the WBR3 WIFI module.

AlbertWeterings avatar Jan 02 '22 13:01 AlbertWeterings

Before I forget one important notice DO NOT power the thermostat from its own supply when you connect it to your PC it will blow your mainboard. You need to power it from a separate 12 and 5 volt supply

AlbertWeterings avatar Jan 02 '22 16:01 AlbertWeterings

Will be careful, Thanks for the warning. I might also try something else when I find some time. I downloaded the Tuya SDK and it has an MCU emulator. so I thought I might connect the WBR3 to my PC and run the MCU emulator and see how that works. Maybe it will give us some clues. And then I might even connect an ESP on the emulator.

PhilipTS avatar Jan 02 '22 18:01 PhilipTS

@PhilipTS that is a good idea. I also had one, I flashed my esp12 in a way the there is only wifi and ota updates. I can now connect the wbr3 module in parallel and see what is going on on the serial port

AlbertWeterings avatar Jan 02 '22 20:01 AlbertWeterings

@PhilipTS Hello, thank you very much for your setup, this is very helpful for setting the thermostate "manually" from the UI.

I have created myself some scripts which I could use in Automations:

This is for setting a temperature: (it checks if in auto mode, if yes, first runs "set manual mode" script and afterwards the temp)

alias: "Thermostate - Set 19 °C" 
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.thermostat_schedulesmode
            state: auto
        sequence:
          - service: script.thermostate_set_manual_mode
          - repeat:
              until:
                - condition: state
                  entity_id: sensor.thermostat_target_temp
                  state: '19.0'
              sequence:
                - service: mqtt.publish
                  data:
                    topic: thermostat_15633253/thermostat/set/targetTemperature
                    payload: '19.0'
                - delay:
                    seconds: '{{ range(8, 11)|random|int }}'
      - conditions:
          - condition: state
            entity_id: sensor.thermostat_schedulesmode
            state: 'off'
        sequence:
          - repeat:
              until:
                - condition: state
                  entity_id: sensor.thermostat_target_temp
                  state: '19.0'
              sequence:
                - service: mqtt.publish
                  data:
                    topic: thermostat_15633253/thermostat/set/targetTemperature
                    payload: '19.0'
                - delay:
                    seconds: '{{ range(8, 11)|random|int }}'
    default: []
mode: single

This is a Script for setting Manual Mode (you can create the same for "auto/schedules" mode)

alias: Thermostate - Set Manual Mode
sequence:
  - repeat:
      until:
        - condition: state
          entity_id: sensor.thermostat_schedulesmode
          state: 'off'
      sequence:
        - service: mqtt.publish
          data:
            topic: thermostat_15633253/thermostat/set/schedulesMode
            payload: 'off'
        - delay:
            seconds: '{{ range(8, 11)|random|int }}'
mode: single

As you can notice I have created a random delay as I noticed that the MCU behaves differently when you send the command in different times, If i send the command exactly every 10 seconds it did not work, afterwards when pressing it 4 seconds later manually it worked, so thats why I used the random delay.

I also expose the states with an mqtt sensor as from the climate entity these are not propagated with the attributes.

Hope it helps as well.

fraintt avatar Jan 02 '22 22:01 fraintt

@fraintt A repeat with until and a random delay !!! Very nice ... very very nice ... this was my first automation and I see I have much to learn :) Thanks for sharing.

PhilipTS avatar Jan 03 '22 12:01 PhilipTS

Welcome to HomeAssistant ;) I suggest you to join also the community here , where you can get a lot of informations and/or help.

fraintt avatar Jan 03 '22 12:01 fraintt

@AlbertWeterings The emulator approach was not very helpful. I could not figure out a way to connect the WBR3 module to my WiFi network. I would only get the same over and over in the emulator. And some of the elements in the GUI are only in Chinese.

image

PhilipTS avatar Jan 03 '22 14:01 PhilipTS

@PhilipTS This is fun ;-) well with my WBR3 module connected in parallel no luck, then i tried the MCU emulator and with under protocol selected WIFI locked I was able to connect with my phone to my fake thermostat... And there it stopped. So I will try to put the WBR3 back in my thermostat and then try to debug. I hope my PCB will survive as I already have soldered and de-soldered the modules a lot of time.

AlbertWeterings avatar Jan 03 '22 17:01 AlbertWeterings