Skip to content

Instantly share code, notes, and snippets.

@frauhottelmann
Last active November 13, 2023 12:30
Show Gist options
  • Save frauhottelmann/24e6895eedee39420d295f5f1112589a to your computer and use it in GitHub Desktop.
Save frauhottelmann/24e6895eedee39420d295f5f1112589a to your computer and use it in GitHub Desktop.
Update external measured room sensor
blueprint:
name: Update external measured room sensor
description: Calibrate Danfoss TRVs.
domain: automation
input:
trv:
name: Danfoss TRV
selector:
entity:
domain: climate
temperature_sensor:
name: Temperature Sensor
description: Sensor used to calibrate your TRV
selector:
entity:
domain: sensor
device_class: temperature
trv_calibration_number:
name: TRV Calibration Entity
description: Choose External measured room sensor
selector:
entity:
domain: number
mode: restart
variables:
trv: !input trv
trv_calibration_number: !input trv_calibration_number
temperature_sensor: !input temperature_sensor
trigger:
- platform: state
entity_id: !input trv
for:
hours: 0
minutes: 0
seconds: 0
- platform: state
entity_id: !input trv_calibration_number
for:
hours: 0
minutes: 0
seconds: 0
- platform: state
entity_id: !input temperature_sensor
for:
hours: 0
minutes: 0
seconds: 0
condition:
- condition: template
value_template: '{{ state_attr(trv, "current_temperature") != None }}'
- condition: template
value_template: '{{ states(trv_calibration_number) != "unavailable" and states(trv_calibration_number) != "unknown" }}'
- condition: template
value_template: '{{ states(temperature_sensor) != "unavailable" and states(temperature_sensor) != "unknown" }}'
action:
- delay:
hours: 0
minutes: 1
seconds: 0
milliseconds: 0
- service: number.set_value
target:
entity_id: !input trv_calibration_number
data:
value: >-
{% set room_temperature = states(temperature_sensor) | float %}
{% set trv_temperature = state_attr(trv, "current_temperature") | float(0) %}
{% set trv_calibration = states(trv_calibration_number) | float %}
{% set trv_calibration_min = state_attr(trv_calibration_number, "min") | float(-5) %}
{% set trv_calibration_max = state_attr(trv_calibration_number, "max") | float(5) %}
{% set trv_calibration_step = state_attr(trv_calibration_number, "step") | float(1) %}
{% set step = trv_calibration_step | string %}
{% if step.split(".") | length > 1 -%}
{% set trv_calibration_step_decimals = step.split(".")[1] | length %}
{%- else -%}
{% set trv_calibration_step_decimals = 0 %}
{%- endif %}
{% set new_trv_calibration = room_temperature - trv_temperature + trv_calibration %}
{% set new_trv_calibration_bounded = [ [ (new_trv_calibration - new_trv_calibration % trv_calibration_step) | round(trv_calibration_step_decimals), trv_calibration_min ] | max, trv_calibration_max ] | min %}
{% if new_trv_calibration_bounded < trv_calibration - trv_calibration_step or trv_calibration + trv_calibration_step < new_trv_calibration_bounded -%}
{{ new_trv_calibration_bounded }}
{%- else -%}
{{ states(trv_calibration_number) }}
{%- endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment