|
| 1 | +{ rooms }: |
| 2 | +{ config, lib, ... }: |
| 3 | +let |
| 4 | + mkAutomation = name: { climateDevices ? { }, temperatureSensors ? [ ] }: |
| 5 | + let |
| 6 | + sync_script_name = lib.toLower "synchronize_room_temperature_to_danfoss_in_${name}"; |
| 7 | + automation_name = sync_script_name; |
| 8 | + timer_id = "${automation_name}_timer"; |
| 9 | + in |
| 10 | + if climateDevices == { } || temperatureSensors == [ ] then { } else { |
| 11 | + config = { |
| 12 | + script.${sync_script_name}.sequence = [ |
| 13 | + { |
| 14 | + variables = { |
| 15 | + temperatures = map (sensor: "{{ states('${sensor}')|float*100|round(0) }}") temperatureSensors; |
| 16 | + }; |
| 17 | + } |
| 18 | + { |
| 19 | + variables = { |
| 20 | + mean_temperature = "{{ temperatures|average }}"; |
| 21 | + }; |
| 22 | + } |
| 23 | + ] ++ |
| 24 | + (map |
| 25 | + (climateDeviceName: { |
| 26 | + service = "number.set_value"; |
| 27 | + target.entity_id = "number.${climateDeviceName}_external_measured_room_sensor"; |
| 28 | + data.value = "{{ mean_temperature|float }}"; |
| 29 | + }) |
| 30 | + (builtins.attrNames climateDevices)); |
| 31 | + |
| 32 | + timer = lib.mapAttrs' |
| 33 | + (climateDevice: _: lib.nameValuePair "${timer_id}_${climateDevice}" { |
| 34 | + duration = "300"; |
| 35 | + }) |
| 36 | + climateDevices; |
| 37 | + |
| 38 | + automation = |
| 39 | + lib.mapAttrsToList (alias: value: { id = alias; inherit alias; } // value) |
| 40 | + (lib.mapAttrs' |
| 41 | + (climateDevice: _: lib.nameValuePair "${automation_name}_${climateDevice}" { |
| 42 | + # on temperature change of any of the sensors |
| 43 | + trigger = map |
| 44 | + (sensor: { |
| 45 | + platform = "state"; |
| 46 | + entity_id = sensor; |
| 47 | + }) |
| 48 | + temperatureSensors |
| 49 | + # or every 30 minutes |
| 50 | + ++ [{ |
| 51 | + platform = "event"; |
| 52 | + event_type = "timer.finished"; |
| 53 | + event_data.entity_id = "${timer_id}_${climateDevice}"; |
| 54 | + }]; |
| 55 | + variables = { |
| 56 | + radiator_covered_status = "{{ states('switch.${climateDevice}_radiator_covered') }}"; |
| 57 | + min_update_minutes = "{% if radiator_covered_status == 'off' %}30{% else %}5{% endif %}"; |
| 58 | + max_update_minutes = "{% if radiator_covered_status == 'off' %}180{% else %}30{% endif %}"; |
| 59 | + }; |
| 60 | + condition = [ |
| 61 | + { |
| 62 | + condition = "template"; |
| 63 | + value_template = "{{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id, 'last_triggered'),0) > 60 * min_update_minutes }}"; |
| 64 | + } |
| 65 | + ]; |
| 66 | + action = [ |
| 67 | + { service = "script.${sync_script_name}"; } |
| 68 | + { service = "timer.start"; target.entity_id = "timer.${timer_id}_${climateDevice}"; data.duration = "{{ max_update_minutes * 60 }}"; } |
| 69 | + ]; |
| 70 | + mode = "single"; |
| 71 | + }) |
| 72 | + climateDevices); |
| 73 | + }; |
| 74 | + }; |
| 75 | +in |
| 76 | +lib.mkMerge (map |
| 77 | + (room: |
| 78 | + { services.home-assistant = mkAutomation room rooms.${room}; }) |
| 79 | + (builtins.attrNames rooms)) |
0 commit comments