Generic Thermostat: changing target temperature does not trigger automation

Hi,

I’ve created a generic_thermostat to control my “dumb” air conditioner using a Zigbee2MQTT IR Blaster. My idea was to create an automation to send a IR command for each change in the thermostat (on, off, each target temperature).

Turning on and off works fine, but changing the target temperature is not triggering the automation, nor it shows in my logbook.

Here’s the configuration.yaml for the thermostat:

climate:
  - platform: generic_thermostat
    name: Living Room AC
    unique_id: climate.living_room_ac_ir
    heater: switch.living_room_ac
    target_sensor: sensor.front_door_device_temperature
    min_temp: 18
    max_temp: 25
    ac_mode: true
    target_temp: 19
    cold_tolerance: 3
    hot_tolerance: 3
    min_cycle_duration:
      seconds: 5
    initial_hvac_mode: "off"
    precision: 1

The automation is a bit long as I used several triggers, but it is as follows.

alias: IR - Living Room Air Conditioner
description: ""
trigger:
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    to: cool
    id: turn_on
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    to: "off"
    id: turn_off
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "18"
    id: temp_18
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "19"
    id: temp_19
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "20"
    id: temp_20
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "21"
    id: temp_21
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "22"
    id: temp_22
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "23"
    id: temp_23
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "24"
    id: temp_24
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    to: "25"
    id: temp_25
condition: []
action:
  - if:
      - condition: trigger
        id:
          - turn_on
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "AAAAAAA"}
  - if:
      - condition: trigger
        id:
          - turn_off
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "BBBBBB"}
  - if:
      - condition: trigger
        id:
          - temp_18
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "CCCCCCC"}
  - if:
      - condition: trigger
        id:
          - temp_19
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "DDDDDD"}
  - if:
      - condition: trigger
        id:
          - temp_20
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "EEEEEEE"}
  - if:
      - condition: trigger
        id:
          - temp_21
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "FFFFFF"}
  - if:
      - condition: trigger
        id:
          - temp_22
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "GGGGGG"}
  - if:
      - condition: trigger
        id:
          - temp_23
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "HHHHHH"}
  - if:
      - condition: trigger
        id:
          - temp_24
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "IIIIIII"}
  - if:
      - condition: trigger
        id:
          - temp_25
    then:
      - service: mqtt.publish
        data:
          qos: 0
          retain: false
          topic: zigbee2mqtt/Linving Room IR Blaster/set
          payload: >-
            {"ir_code_to_send":
            "JJJJJJJ"}
mode: single

Thank you in advance!

The issue is that your data types are different and the State trigger is very literal in how it matches. The the value for a climate entitiy’s temperature attribute is a float, but your triggers are looking for strings. You can make it work by switching to editing the automation in YAML and deleting the quotation marks around the numbers. Keep in mind that, even with these changes, the triggers will only fire when the temperature is set to one of the exact numbers you have created a trigger for. This can be very frustrating for other people in the household if they are trying to set it to a value that isn’t set up as a trigger.

You can make this a lot more compact with a couple templates:

alias: IR - Living Room Air Conditioner
description: ""
trigger:
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    to: cool
    id: turn_on
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    to: "off"
    id: turn_off
  - platform: state
    entity_id:
      - climate.living_room_ac_ir
    attribute: temperature
    id: temp
condition: []
action:
  - variables:
      int:  "{{ '0' if trigger.id in ['turn_on', 'turn_off'] else trigger.to_state.attributes.temperature | int }}"
      command: |
        {% set com_map = {       
        0: 'A' if trigger.id == 'turn_on' else 'B'
        18: 'C', 19: 'D', 20: 'E', 21: 'F',
        22: 'G', 23: 'H', 24: 'I', 25: 'J' } %}
        {{ com_map.get(int) * 7 }}
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: zigbee2mqtt/Living Room IR Blaster/set
      payload: >-
        {% raw %}{"ir_code_to_send":"{%-endraw-%}{{-command-}}{%-raw-%}"}{%-endraw-%}
mode: single

Thank you very much! That was it! I removed the quotes and it worked perfectly.

I’m not really worried about someone setting the temperature a different value because I restricted the minimum, maximum and step temperatures when creating the thermostat…

Thank you also for your code using variables. I still don’t know how to use them, so my code is not so elegant.