blueprint: name: "Sync Remote Temperature to TP-WGZBA" author: Sonoff description: > Synchronizes an external temperature reading with a SONOFF TP-WGZBA thermostat connected through ZHA or Zigbee2MQTT. The reading is applied whenever the thermostat's external temperature mode is enabled, immediately and on each source update. Temperature values in Celsius, Fahrenheit, or Kelvin are automatically converted to Celsius. domain: automation input: target_device: name: TP-WGZBA device description: Select the ZHA or Zigbee2MQTT thermostat that should receive the temperature. selector: device: filter: - integration: zha manufacturer: SONOFF model: TP-WGZBA - integration: mqtt manufacturer: SONOFF model: Zigbee thermostat panel model_id: TP-WGZBA source_entity: name: External temperature sensor description: Select a temperature sensor from another device. The target TP-WGZBA's own temperature is not a valid source. selector: entity: filter: domain: sensor device_class: temperature variables: target_device: !input target_device source_entity: !input source_entity target_integration: > {% set ns = namespace(value='') %} {% for identifier in (device_attr(target_device, 'identifiers') or []) %} {% if identifier[0] == 'zha' %} {% set ns.value = 'zha' %} {% elif identifier[0] == 'mqtt' %} {% set ns.value = 'mqtt' %} {% endif %} {% endfor %} {{ ns.value }} zha_external_temperature_sensor_switch: > {{ (device_entities(target_device) or []) | select('match', '^switch\\..*_external_temperature_sensor$') | list | first | default('', true) }} mqtt_temperature_sensor_select: > {{ (device_entities(target_device) or []) | select('match', '^select\\..*_temperature_sensor_select$') | list | first | default('', true) }} mqtt_external_temperature_input: > {{ (device_entities(target_device) or []) | select('match', '^number\\..*_external_temperature_input$') | list | first | default('', true) }} target_ieee: > {%- for identifier in (device_attr(target_device, 'identifiers') or []) -%} {%- if identifier[0] == 'zha' -%} {{ identifier[1] }} {%- endif -%} {%- endfor %} triggers: - trigger: state entity_id: !input source_entity id: source_temperature not_to: - "unknown" - "unavailable" - trigger: event event_type: state_changed id: external_temperature_sensor_enabled conditions: - condition: template value_template: > {{ is_number(states(source_entity)) and state_attr(source_entity, 'unit_of_measurement') in ['°C', '°F', 'K'] and source_entity not in (device_entities(target_device) or []) and device_attr(target_device, 'manufacturer') == 'SONOFF' and ((target_integration | trim == 'zha' and device_attr(target_device, 'model') == 'TP-WGZBA' and target_ieee | trim != '' and zha_external_temperature_sensor_switch | trim != '' and is_state(zha_external_temperature_sensor_switch, 'on') and (trigger.id == 'source_temperature' or (trigger.event.data.entity_id == zha_external_temperature_sensor_switch and trigger.event.data.new_state is not none and trigger.event.data.new_state.state == 'on'))) or (target_integration | trim == 'mqtt' and device_attr(target_device, 'model') == 'Zigbee thermostat panel' and mqtt_external_temperature_input | trim != '' and mqtt_temperature_sensor_select | trim != '' and trigger.id == 'source_temperature')) }} actions: - choose: - conditions: - condition: template value_template: "{{ target_integration | trim == 'zha' }}" sequence: - action: zha.set_zigbee_cluster_attribute data: ieee: "{{ target_ieee | trim }}" endpoint_id: 1 cluster_id: 0xFC11 cluster_type: in attribute: 0x7009 value: > {% set temperature = states(source_entity) | float %} {% set unit = state_attr(source_entity, 'unit_of_measurement') %} {{ (((temperature - 32) * 5 / 9 if unit == '°F' else temperature - 273.15 if unit == 'K' else temperature) * 100) | round(0) | int }} - conditions: - condition: template value_template: > {{ target_integration | trim == 'mqtt' and mqtt_external_temperature_input | trim != '' and mqtt_temperature_sensor_select | trim != '' and trigger.id == 'source_temperature' }} sequence: - action: number.set_value target: entity_id: "{{ mqtt_external_temperature_input | trim }}" data: value: > {% set temperature = states(source_entity) | float %} {% set unit = state_attr(source_entity, 'unit_of_measurement') %} {{ (temperature - 32) * 5 / 9 if unit == '°F' else temperature - 273.15 if unit == 'K' else temperature }} mode: restart