#!/usr/bin/perl # saunaheater: sauna heater sizing and sauna temperature directives for ikiwiki. # # [[!saunaheater length=6 width=5 height=7 unit=ft glass=12]] # [[!saunatemp 194F]] # # Copyright (c) 2026 DIVELUX Inc. MIT License (see LICENSE). # https://github.com/divelux/ikiwiki-saunaheater package IkiWiki::Plugin::saunaheater; use warnings; use strict; use utf8; use IkiWiki 3.00; use POSIX qw(floor); our $VERSION = '1.0.0'; sub import { hook(type => "getsetup", id => "saunaheater", call => \&getsetup); hook(type => "preprocess", id => "saunaheater", call => \&preprocess_heater); hook(type => "preprocess", id => "saunatemp", call => \&preprocess_temp); } sub getsetup () { return plugin => { safe => 1, rebuild => undef, section => "widget", }, } # --------------------------------------------------------------------------- # The sizing method. A line-for-line port of heater.js, the calculator behind # the DIVELUX web tools; t/03-reference.t checks it against the original. # # 1. 1 kW of heater power per cubic metre of room (Harvia; HUUM says the same). # 2. Each m² of glass, stone or similar uninsulated surface adds 1.2 m³ to the # volume (Harvia). HUUM uses 1.0, a North American HUUM sheet 1.5. # 3. Bare log walls: room volume x 1.5 (Harvia). # 4. Low end of the kW range: 1 kW per 50 cu ft, the retail shortcut that # matches Harvia's worked example (8.5 m³ -> at least 6 kW). # 5. Wood stoves are chosen by room volume, so they get no kW figure. # --------------------------------------------------------------------------- our $M_PER_FT = 0.3048; our $CUFT_PER_M3 = 35.3147; our $SQFT_PER_M2 = 10.7639; our $KW_PER_M3 = 1; our $CUFT_PER_KW_LOOSE = 50; our $LOG_FACTOR = 1.5; our %SURFACE_M3_PER_M2 = (harvia => 1.2, huum => 1.0, huumNA => 1.5); our %LIMITS = ( side => { min => 0.9, max => 6 }, height => { min => 1.7, max => 3 }, area => { min => 0, max => 40 }, ); # JavaScript Math.round: nearest integer, halves go up (also for negatives). sub js_round ($) { my $x = shift; my $f = floor($x); return ($x - $f >= 0.5) ? $f + 1 : $f; } # heater.js round(v, digits) sub round_to ($;$) { my ($v, $digits) = @_; my $p = 10 ** ($digits || 0); return js_round($v * $p) / $p; } sub m3_to_cuft ($) { return $_[0] * $CUFT_PER_M3 } sub m2_to_sqft ($) { return $_[0] * $SQFT_PER_M2 } sub sqft_to_m2 ($) { return $_[0] / $SQFT_PER_M2 } # calc(\%input): lengths in metres, areas in m². Same fields as heater.js calc(). # Returns { ok => 0, errors => [[field, message], ...] } or the full result. sub calc ($) { my $in = shift; my ($L, $W, $H) = map { $in->{$_} } qw(length width height); my $glass = defined $in->{glass} ? $in->{glass} : 0; my $masonry = defined $in->{masonry} ? $in->{masonry} : 0; my $log = $in->{log} ? 1 : 0; my $heater = (defined $in->{heater} && $in->{heater} eq 'wood') ? 'wood' : 'electric'; my @errors; my %err; my $add = sub { my ($k, $m) = @_; push @errors, [$k, $m] unless exists $err{$k}; $err{$k} = $m; }; my $side = sub { my ($v, $key) = @_; if (! defined $v) { $add->($key, "Enter the $key.") } elsif ($v < $LIMITS{side}{min} - 1e-9 || $v > $LIMITS{side}{max} + 1e-9) { $add->($key, "Check the $key: this tool covers rooms from 3 ft to 19 ft 8 in (0.9 to 6 m) a side."); } }; $side->($L, 'length'); $side->($W, 'width'); if (! defined $H) { $add->('height', 'Enter the height.') } elsif ($H < $LIMITS{height}{min} - 1e-9 || $H > $LIMITS{height}{max} + 1e-9) { $add->('height', 'Check the height: this tool covers ceilings from 5 ft 7 in to 9 ft 10 in (1.7 to 3 m).'); } $add->('glass', 'Check the glass area.') if $glass < 0 || $glass > $LIMITS{area}{max}; $add->('masonry', 'Check the stone, tile or brick area.') if $masonry < 0 || $masonry > $LIMITS{area}{max}; if (! @errors) { # All six faces of the room: nothing can be more uninsulated than that. my $faces = 2 * ($L * $W + $L * $H + $W * $H); if ($glass + $masonry > $faces + 1e-9) { $add->('glass', 'Glass plus stone is more than the whole surface of this room. Check the areas.'); } } return { ok => 0, errors => \@errors } if @errors; my $volume = $L * $W * $H; my $surface = $glass + $masonry; my $base = $volume * ($log ? $LOG_FACTOR : 1); my $calc_volume = $base + $surface * $SURFACE_M3_PER_M2{harvia}; my %by_rule = ( harvia => $calc_volume, # HUUM counts log walls as surface by area, not with a factor: no guess. huum => $log ? undef : $volume + $surface * $SURFACE_M3_PER_M2{huum}, huumNA => $log ? undef : $volume + $surface * $SURFACE_M3_PER_M2{huumNA}, ); my $kw_high = $calc_volume * $KW_PER_M3; my $kw_low = m3_to_cuft($calc_volume) / $CUFT_PER_KW_LOOSE; my @notes; if ($surface > 0 && $surface * $SURFACE_M3_PER_M2{harvia} / $calc_volume >= 0.3) { push @notes, 'Glass and stone make up ' . js_round($surface * $SURFACE_M3_PER_M2{harvia} / $calc_volume * 100) . '% of the calculated volume. With this much uninsulated surface, ask the manufacturer before you buy.'; } push @notes, 'Bare log walls soak up heat, so the room volume is multiplied by 1.5 (Harvia).' if $log; if ($H > 2.3 + 1e-9) { push @notes, 'A tall ceiling adds air to heat. Every extra foot of height adds ' . round_to(m3_to_cuft($L * $W * $M_PER_FT), 0) . ' cu ft to this room.'; } my $r1 = sub { defined $_[0] ? round_to($_[0], 1) : undef }; my $r0cu = sub { defined $_[0] ? round_to(m3_to_cuft($_[0]), 0) : undef }; return { ok => 1, input => { length => $L, width => $W, height => $H, glass => $glass, masonry => $masonry, log => $log, heater => $heater }, volumeM3 => round_to($volume, 2), volumeCuFt => round_to(m3_to_cuft($volume), 0), surfaceM2 => round_to($surface, 2), surfaceSqFt => round_to(m2_to_sqft($surface), 1), logFactor => $log ? $LOG_FACTOR : 1, extraM3 => round_to($surface * $SURFACE_M3_PER_M2{harvia}, 1), extraCuFt => round_to(m3_to_cuft($surface * $SURFACE_M3_PER_M2{harvia}), 0), calcVolumeM3 => round_to($calc_volume, 1), calcVolumeCuFt => round_to(m3_to_cuft($calc_volume), 0), byRule => { map { $_ => $r1->($by_rule{$_}) } keys %by_rule }, byRuleCuFt => { map { $_ => $r0cu->($by_rule{$_}) } keys %by_rule }, kw => $heater eq 'electric' ? { low => round_to($kw_low, 1), high => round_to($kw_high, 1) } : undef, notes => \@notes, }; } # --------------------------------------------------------------------------- # Directive arguments and text # --------------------------------------------------------------------------- # Whole numbers without ".0", others with up to two decimals. sub num_text ($) { my $r = round_to(shift, 2); return "$r" if $r == int($r); (my $s = sprintf('%.2f', $r)) =~ s/0+$//; return $s; } sub number ($$) { my ($text, $what) = @_; my $s = defined $text ? $text : ''; $s =~ s/^\s+|\s+$//g; $s =~ tr/,/./; die "Enter the $what as a number, for example 6 or 2.4.\n" unless $s =~ /\A(?:\d+(?:\.\d+)?|\.\d+)\z/; return 0 + $s; } sub flag ($$) { my ($text, $what) = @_; my $s = lc(defined $text ? $text : ''); $s =~ s/^\s+|\s+$//g; return 0 if $s eq '' || $s =~ /\A(?:no|n|false|0|off)\z/; return 1 if $s =~ /\A(?:yes|y|true|1|on)\z/; die "$what must be yes or no.\n"; } my %UNIT_ALIAS = (ft => 'ft', feet => 'ft', foot => 'ft', m => 'm', meter => 'm', metre => 'm', meters => 'm', metres => 'm'); # heater(%args) -> hash with the sentences to show. Dies with a plain message # when an argument is wrong. Named arguments: length width height unit glass # stone log heater. Three bare numbers and a bare unit work too: # [[!saunaheater 6 5 7 ft]]. sub heater { my @pairs = @_; my (%named, @bare); while (@pairs) { my ($k, $v) = splice(@pairs, 0, 2); if (defined $v && $v eq '' && ($k !~ /^(?:length|width|height|unit|glass|stone|masonry|heater)$/i)) { push @bare, $k; } else { $named{lc $k} = $v; } } for my $b (@bare) { my $lb = lc $b; if (exists $UNIT_ALIAS{$lb}) { $named{unit} = $lb unless defined $named{unit}; next } if ($lb eq 'log') { $named{log} = 'yes'; next } if ($lb eq 'wood') { $named{heater} = 'wood'; next } my ($slot) = grep { ! defined $named{$_} } qw(length width height); die "Too many values: give the length, width and height, then ft or m.\n" unless $slot; $named{$slot} = $b; } for my $k (qw(length width height)) { die "Give the length, width and height, for example [[!saunaheater length=6 width=5 height=7 unit=ft]].\n" unless defined $named{$k} && $named{$k} ne ''; } my $unit = lc(defined $named{unit} && $named{unit} ne '' ? $named{unit} : 'ft'); $unit =~ s/^\s+|\s+$//g; die "The unit must be ft or m.\n" unless exists $UNIT_ALIAS{$unit}; $unit = $UNIT_ALIAS{$unit}; my $ft = $unit eq 'ft'; my ($l, $w, $h) = map { number($named{$_}, $_) } qw(length width height); my $stone_arg = defined $named{stone} ? $named{stone} : $named{masonry}; my $glass = (defined $named{glass} && $named{glass} =~ /\S/) ? number($named{glass}, 'glass area') : 0; my $stone = (defined $stone_arg && $stone_arg =~ /\S/) ? number($stone_arg, 'stone area') : 0; my $log = flag($named{log}, 'log'); my $kind = lc(defined $named{heater} ? $named{heater} : ''); $kind =~ s/^\s+|\s+$//g; $kind = 'electric' if $kind eq ''; die "heater must be electric or wood.\n" unless $kind eq 'electric' || $kind eq 'wood'; my $r = calc({ length => $ft ? $l * $M_PER_FT : $l, width => $ft ? $w * $M_PER_FT : $w, height => $ft ? $h * $M_PER_FT : $h, glass => $ft ? sqft_to_m2($glass) : $glass, masonry => $ft ? sqft_to_m2($stone) : $stone, log => $log, heater => $kind, }); die join(' ', map { $_->[1] } @{$r->{errors}}) . "\n" unless $r->{ok}; my $room = join(' × ', map { num_text($_) } $l, $w, $h) . " $unit"; my $vol = $ft ? "$r->{volumeCuFt} cu ft" : num_text($r->{volumeM3}) . ' m³'; my $calc_vol = $ft ? "$r->{calcVolumeCuFt} cu ft" : num_text($r->{calcVolumeM3}) . ' m³'; my $calc_vol_other = $ft ? num_text($r->{calcVolumeM3}) . ' m³' : "$r->{calcVolumeCuFt} cu ft"; my $answer = $kind eq 'electric' ? num_text($r->{kw}{low}) . '–' . num_text($r->{kw}{high}) . ' kW electric heater' : "a wood-burning stove rated for at least $calc_vol ($calc_vol_other)"; my @details = ("Room $room: $vol."); if ($r->{surfaceM2} > 0) { my $area = $ft ? num_text($r->{surfaceSqFt}) . ' sq ft' : num_text($r->{surfaceM2}) . ' m²'; my $extra = $ft ? "$r->{extraCuFt} cu ft" : num_text($r->{extraM3}) . ' m³'; push @details, "Glass and stone $area count as $extra more."; } push @details, "Volume to heat: $calc_vol ($calc_vol_other)."; return { room => $room, unit => $unit, kind => $kind, result => $r, answer => $answer, details => \@details, notes => $r->{notes}, source => "Heater makers' published sizing rules (Harvia, HUUM). " . 'Confirm with the heater manufacturer; have an electrician check the circuit.', }; } # temp("194F"[, "only"]) -> { text, title, value, unit, to_value, to_unit } sub temp { my ($arg, $mode) = @_; my $s = defined $arg ? $arg : ''; $s =~ s/^\s+|\s+$//g; $s =~ tr/,/./; my ($v, $u) = $s =~ /\A([+-]?\d+(?:\.\d+)?)\s*(?:°|º|deg\s*)?\s*([CcFf])\z/ or die "Write a temperature with its unit, for example 194F or 90C.\n"; $v += 0; $u = uc $u; my ($to, $to_unit); if ($u eq 'F') { die num_text($v) . " °F is below absolute zero.\n" if $v < -459.67; ($to, $to_unit) = (js_round(($v - 32) * 5 / 9), 'C'); } else { die num_text($v) . " °C is below absolute zero.\n" if $v < -273.15; ($to, $to_unit) = (js_round($v * 9 / 5 + 32), 'F'); } $mode = lc(defined $mode ? $mode : ''); $mode =~ s/^\s+|\s+$//g; die "The only other option is \"only\".\n" unless $mode eq '' || $mode eq 'only'; my $from = num_text($v) . " °$u"; my $into = "$to °$to_unit"; return { value => $v, unit => $u, to_value => $to, to_unit => $to_unit, text => $mode eq 'only' ? $into : "$from ($into)", title => "$from = $into, rounded to the nearest whole degree", }; } # --------------------------------------------------------------------------- # HTML # --------------------------------------------------------------------------- sub esc ($) { my $s = shift; $s =~ s/&/&/g; $s =~ s//>/g; $s =~ s/"/"/g; return $s; } sub heater_html ($) { my $f = shift; # One line, so markdown leaves the block alone. return '
' . '

Sauna heater: ' . esc($f->{answer}) . '

' . '

' . esc(join(' ', @{$f->{details}})) . '

' . join('', map { '

' . esc($_) . '

' } @{$f->{notes}}) . '

' . esc($f->{source}) . '

' . '
'; } sub temp_html ($) { my $t = shift; return '' . esc($t->{text}) . ''; } # Strip the arguments ikiwiki adds to every directive. sub user_params { my @p = @_; my @out; while (@p) { my ($k, $v) = splice(@p, 0, 2); next if defined $k && $k =~ /^(?:page|destpage|preview)$/; push @out, $k, $v; } return @out; } sub preprocess_heater (@) { my $f = eval { heater(user_params(@_)) }; if ($@) { chomp(my $e = $@); error($e) } return heater_html($f); } sub preprocess_temp (@) { my @p = user_params(@_); my (@words, $mode, $value); while (@p) { my ($k, $v) = splice(@p, 0, 2); if (lc $k eq 'only' && (! defined $v || $v eq '' || $v =~ /^(?:yes|1|true)$/i)) { $mode = 'only'; next } if ($k =~ /^(?:temp|value)$/i) { $value = $v; next } push @words, $k if defined $v && $v eq ''; } $value = join(' ', @words) unless defined $value; my $t = eval { temp($value, $mode) }; if ($@) { chomp(my $e = $@); error($e) } return temp_html($t); } 1