Need some help with a Generic Thermostat please

I have three heat pumps, two controlled by Broadlink with the smartir component (awesome) and the one in the lounge has been added to my Harmony remote. While they all work well, they are Home Assistant unaware as HA does not know if they are actually on. This creates a problem is someone turns on/off the heat pump with the remote.

I’ve added a Sonoff POW R2 with Tasmota inline with the lounge heat pump and now want to change my template switch so that power more than 5w is on otherwise off. Not quite sure how to do that though. I have these two sensors and the code for the harmony based climate follows that. If anyone can point me in the right direction, that would be appreciated.

## LOUNGE ##    
binary_sensor:
  - platform: template
    sensors:
      hvac_lounge_power_state:
        friendly_name: "HVAC Lounge Running"
        device_class: heat
        value_template: "{{ states('sensor.hvac_lounge_power')|float > 5 }}"

sensor:
## Sonoff POW R2 under house near heat pump ##
  - platform: mqtt
    name: "HVAC Lounge Power"
    state_topic: "tele/hvac_lounge/SENSOR"
    value_template: '{{ value_json["ENERGY"]["Power"] }}'
    unit_of_measurement: "W" 

And here the the template switch that is integrated into the Generic Thermostat that I’m looking to modify.

switch:
  - platform: template
    switches:
      lounge_heat_pump:
        value_template: "{{ is_state('input_boolean.lounge_heat_pump', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.lounge_heat_pump
          - service: script.lounge_aircon_on
        turn_off:
          - service: input_boolean.turn_off
            entity_id: input_boolean.lounge_heat_pump
          - service: script.lounge_aircon_off

I tried replacing the value_template with "{{ is_state('binary_sensor.hvac_lounge_power_state', 'on') }}" but that did not work.

That is what I would expect to function correctly. Does the binary sensor actually change state when you turn the heat pump on/off?

Do you actually get a power reading for the other sonoff sensor?

Thanks. Do I leave the input boolean section or is that not necessary if I’ve for the power sensor?

OK, this seems to be working with the following conditions:

switch:
  - platform: template
    switches:
      lounge_heat_pump:
        value_template: "{{ is_state('binary_sensor.hvac_lounge_power_state', 'on') }}"
        turn_on:
          - service: script.lounge_aircon_on
        turn_off:
          - service: script.lounge_aircon_off
  1. The teleperiod for the Sonoff POW must be set to the lowest value (10) for this to be useful. The default is 300 and I had it at 60.
  2. The lounge temperature is being measured by a MiFlora plant sensor and only updating every 15 mins without retain. If HA is restarted, and the MiFlora is between ‘reads’, no value is presented to the generic thermostat and it will not work. I’ve now added retain to the lux readings for my two MiFlora sensors.

OK, not quite there yet. Now works if I turn it on and off with the template switch but if I turn it off with the remote, the template switch does not update.

The power is less than 5W so the binary sensor is off. The power has to exceed 5W to turn on.

binary_sensor:
  - platform: template
    sensors:
      hvac_lounge_power_state:
        friendly_name: "HVAC Lounge Running"
        device_class: heat
        value_template: "{{ states('sensor.hvac_lounge_power')|float > 5 }}"

Sorry, that was my point. I turned it off with the remote and the sensor power dropped below 10w but the thermostat did not switch off in the GUI.

Did the template switch turn off?

What does your thermostat config look like?

I found this in another part of my config that works for my coffee machine so testing that now. Fingers crossed. Negates the need for the other binary sensor too.

value_template: "{% if states('sensor.hvac_lounge_power') | float < 10 %}off{% else %}on{% endif %}"

Here is the full thermostat config. Not too complex.

climate:
  - platform: generic_thermostat
    name: Lounge AC
    heater: switch.lounge_heat_pump
    target_sensor: sensor.lounge_temperature
    min_temp: 15
    max_temp: 25
    ac_mode: false
    target_temp: 23

Damn, still not working. Seems the power consumption changing has no affect on the template switch when invoked remotely. If I turn the heat pump on or off with the remote, HA is still unaware. If however I use the GUI, the state does report correctly (heating or off) when the power changes either above or below 10w.

I’m obviously missing something.

I’m not even sure why you have the binary sensor. Try this switch:

switch:
  - platform: template
    switches:
      lounge_heat_pump:
        value_template: "{{ states('sensor.hvac_lounge_power')|float > 5 }}"
        turn_on:
          - service: script.lounge_aircon_on
        turn_off:
          - service: script.lounge_aircon_off

Thanks, but I had this value_template: "{% if states('sensor.hvac_lounge_power') | float < 10 %}off{% else %}on{% endif %}" and neither that or yours works. Think it might have something to do with this? Generic thermostat - Home Assistant as my coffee machine one below works just fine [disregard the toggle code].

switch:
  - platform: template
    switches:
      coffee_machine:
        friendly_name: Espresso Machine
        value_template: "{% if states('sensor.xiaomi_coffee_machine_power') | float < 2 %}off{% else %}on{% endif %}" 
        turn_on: &toggle_coffee
          - service: switch.turn_on
            entity_id: switch.coffee_machine_shelly_relay
          - delay:
              milliseconds: 400
          - service: switch.turn_off
            entity_id: switch.coffee_machine_shelly_relay
        turn_off: *toggle_coffee

I’m surprised this works:

value_template: "{% if states('sensor.xiaomi_coffee_machine_power') | float < 2 %}off{% else %}on{% endif %}"

As the value template should evaluate to true or false not on or off. i.e. it should be:

value_template: "{{ states('sensor.xiaomi_coffee_machine_power') | float < 2 }}"

It has nothing to do with keep_alive. So forget that and your coffe por for now.

Look at it step by step.

Does the value of this template evaluate to true or false when the heat pump is on or off (use the developer tools template editor)?

{{ states('sensor.hvac_lounge_power')|float > 5 }}

I had not added the switch.lounge_heat_pump to LL so I just did that and can confirm that the switch itself is actually working (using your value_template). It turns on and off as I use the remote but the thermostat itself does not update. So it seems it is an anomaly with the way the generic thermostat expects to see the switch behavior?

Seems I might be misunderstanding how the generic thermostat supposed to work? I don’t actually think it will ever behave the way I want it too (like a normal thermostat).

I think it’s designed for a dumb device like say a heated floor with a smart switch to turn the element on and off. When the floor temp is above or below the set level, it simply turns the switch on or off to energize the element. Might be wrong? I just don’t know now.

Yes that is how it works.

So it won’t work the way I have it?

Why not?

What did you expect it to do?

I thought that the power status (off or heating) would reflect my switch state based on the power consumption. While I can just use the template switch directly in LL, the thermostat card gave it more of a family friendly look and was consistent with the other two thermostats (Broadlink based) in the dashboard. Looks like I’ll switch to Broadlink with this one too.

Thank you so much for all your help and feedback though. Very much appreciated.

It does.

Your thermostat controls the heating based on the temperature you set by switching the the heat pump on and off. It gets feedback from the power monitoring switch to confirm that when switched off it really is off.

So if someone switches the heat pump off with the remote, the thermostat will see that the heat pump is off. It may then turn the heat pump back on, based on the temperature set points you have.