Blog

You may or may not find something useful here.

Browse the archive too see all entries.

calculate reduced pressure in home assistant

Yet another fun exercise to calculate something!

Pressure sensors measure the pressure on their altitude, but we eventually want the pressure at sea-level, for example to check with the weather forecast.

That can be done, by using some equations, if we have a sensor that can measure temperature, humidity and pressure (for example a BME280):

template:
  - sensor:
    - name: "Pressure sea level"
        unique_id: "pressure_red"
        state: |-
          {%- set g0 = 9.80665 %}
          {%- set RG = 287.05 %}
          {%- set Ch = 0.12 %}
          {%- set a = 0.0065 %}
          {%- set h = 162.0 %}
          {%- set p = states('sensor.outdoor_pressure') | float %}
          {%- set phi = 0.01 * (states('sensor.outdoor_humidity') | float) %}
          {%- set t = (states('sensor.outdoor_temperature') | float) %}
          {%- set T = 273.15 + t %}
          {%- set E = phi * 6.112 * e**((17.62 * t) / (243.12 + t)) %}
          {%- set x = (g0 / (RG * (T + Ch * E + a * (h / 2)))) * h %}
          {{ (p * e**x) | round(1) }}
        icon: "mdi:gauge"
        unit_of_measurement: 'hPa'
        state_class: "measurement"
        device_class: "pressure"

The only thing you have to set is the height (h) in meters.

Posted Mon Nov 14 13:24:02 2022
calculate dew point in home assistant

If you have a temperature and humidity sensor, you can calculate the dewpoint using the Magnus formula. See for example the german Wikipedia.

It is pretty straight forward to create a template sensor out of this:

template
  - sensor:
    - name: "Dew Point"
      unique_id: "indoor_dewpoint"
      state: |-
        {%- set t = states('sensor.indoor_temperature') | float %}
        {%- set lnrh = log(0.01 * states('sensor.indoor_humidity') | float) %}
        {%- set k2 = 17.62 %}
        {%- set k3 = 243.12 %}
        {%- set q1 = (k2 * t) / (k3 + t) %}
        {%- set q2 = (k2 * k3) / (k3 + t) %}
        {{ (k3 * ((q1 + lnrh) / (q2 - lnrh))) | round(1) }}
      unit_of_measurement: "°C"
      icon: "mdi:thermometer-water"
      device_class: "temperature"
      state_class: "measurement"
Posted Sun Nov 13 13:36:32 2022
IMAPSIEVE with dovecot and user scripts

I use k9-mail on Android. It works perfectly fine, except for one thing: it does not support yearly based archives... However, k9-mail supports archives in general, thus you can easily set an archive folder and move the messages there.

That's were IMAPSIEVE comes into play - at least I thought so on first glance. Then I discovered, that it is actually not that easy to configure, as the documentation is very sparse. But fortunately, I got it working and want to document the steps I took.

For user and mailbox specific scripts, you also need to enable IMAP METADATA. Thus, the first step is to enable that.

Then you can enable IMAPSIEVE. The required parts are (for me on Debian):

in /etc/dovecot/conf.d/20-imap.conf set:

protocol imap {
    # ...
    mail_plugins = $mail_plugins imap_sieve
    # ...
}

and in /etc/dovecot/conf.d/90-sieve.conf set:

plugin {
    # This option should already be set when you configured sieve...
    sieve = file:~/sieve;active=~/.dovecot.sieve
    # ...
    # These two are the important bits for imapsieve:
    sieve_plugins = sieve_imapsieve

    imapsieve_url = sieve://your_managesieve_server:4190
    # ...
}

I simply assume that you have already configured managesieve and sieve correctly and to your needs. The imapsieve_url seems to be important and you have to configure the server URL to managesieve here. AFAIK it is only there to hint you the managesieve url though...

Now that you have configured all the important bits, you can test if the things are enabled. Run openssl s_client to connect to your IMAP like:

$ openssl s_client -crlf -connect  your_mail_server:993

you can login with a login your_username your_password

now, you should get a list of the CAPABILITIES of the server. Check that in the list there is METADATA and IMAPSIEVE=sieve://your_managesieve_server:4190.

Now, you can set mailbox specific sieve scripts, using the IMAP commandline. The important command here is SETMETADATA and GETMETADATA to verify. Information about these commands can be found in the RFC 5464.

Basically, you need to set the key /shared/imapsieve/script with the sieve script as value, per mailbox you want to filter.

For example, if you have uploaded a sieve script called archivefilter (this will physically be stored in %h/sieve/archivefilter.sieve if you configured the sieve option as I did), then you need to type the following command to activate it for the mailbox Archives:

a SETMETADATA "Archives" (/shared/imapsieve/script "archivefilter")

You can verify that by typing:

a GETMETADATA "Archives" /shared/imapsieve/script

if you are done, logout: a LOGOUT

Now for the sieve script itself. I want to redirect all messages which are moved into the Archives folder to Archives.YYYY - so that I can use the archives setting from k9-mail but keep my yearly archive.

So far, I was able to solve this using this sieve script:

require ["imapsieve", "environment", "date", "fileinto", "mailbox", "variables"];

# Rule for imapsieve, move messages from Archives to Archives.YEAR
if anyof (environment :is "imap.cause" "APPEND", environment :is "imap.cause" "COPY") {
    if date :matches "received" "year" "*" {
        fileinto :create "Archives.${1}";
        stop;
    }
    # No received date available... odd but OK... sort by currentdate:
    # (Not sure though if this can actually happen?)
    if currentdate :matches "year" "*" {
        fileinto :create "Archives.${1}";
        stop;
   }
}
Posted Sat Oct 29 15:19:46 2022
Voronoi ordered to random

Somewhere I saw a picture of a high-performance computer, where the doors would have a hexagonal pattern that would "degrade" into a irregular Voronoi pattern as ventilation holes.

That can be replicated quite easily with a short python script! If you like, you can then use my Voronoi pattern generator for FreeCAD to build such ventilation holes yourself.

The python code is pretty straight forward. First, you need a couple of libraries:

import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
from itertools import count

Next up, we generate a couple of points that are the centers of tiled hexagons:

def hex_pattern(a: float = 1, nx: int = 20, ny: int = 10, start: int = 1) -> np.array:
    """
    Create a pattern with nx times ny cells
    :param float d: edge length of the hexagon
    :param int nx: number of cells in x-direction
    :param int ny: number of cells in y-direction
    :param int start: if the offset is on even (0) or odd (1) rows
    """
    points = np.empty((nx * ny, 2), dtype=float)
    if start not in (0, 1):
        raise ValueError('start must be either 0 or 1')
    if nx < 1:
        raise ValueError("there must be a minimum of 1 cells in x")
    if ny < 1:
        raise ValueError("there must be a minimum of 1 cells in y")
    if a <= 0:
        raise ValueError("Edge length of hexagon must be positive and non-zero")
    dx = 2 * a
    dy = np.sqrt(3) * a
    i = count()
    for y in range(ny):
        offset = a if y % 2 == start else 0
        for x in range(nx):
            points[next(i)] = dx * x + offset, dy * y
    return points

points = hex_pattern()

Now, we want some randomness in there. We set up several functions to add the randomness:

def linear(x: np.array, x0: float = -0.5, x1: float = 0) -> np.array:
    """
    Linear gradient between x0 and x1, values lower x0 are set to 0,
    higher than x1 to 1
    """
    x = x.copy()
    zeros = x < x0
    ones = x > x1
    z = x[(x >= x0) & (x <= x1)]
    r = z.max() - z.min()
    z = (z - z.min()) / r
    x[(x >= x0) & (x <= x1)] = z
    x[zeros] = 0
    x[ones] = 1
    return x


def norm_interval(x: np.array, a: float = -1, b: float = 1) -> np.array:
    """Normalize values to the new interval [a, b]"""
    r = x.max() - x.min()
    return (((x - x.min()) / r) * (b - a)) + a


def add_random(p: np.array, fun, k: float = 1, coord: int = 0) -> np.array:
    """Add a random displacement to a point with a threshold function"""
    r = k * np.random.random(p.shape)
    # The idea here is to scale the x (or y) values of the points to [-1, 1],
    # then give it to the threshold function, which returns scaling factors [0, 1]
    c = fun(norm_interval(p[:, coord])).reshape(-1, 1)
    return p + r * c

points = add_random(points, linear)

Now, we can use the Voronoi function to generate the cells (or you use the points directly in the FreeCAD script):

vor = Voronoi(points)

plt.figure(figsize=(10,5))
voronoi_plot_2d(vor, plt.gca(), show_points=False, show_vertices=False)
plt.axis('equal')
plt.xlim(np.min(points[:, 0]), np.max(points[:, 0]))
plt.ylim(np.min(points[:, 1]), np.max(points[:, 1]))
plt.show()

And here we are:

Posted Thu Sep 22 20:13:36 2022
creating qcow2 with vmdb2

vmdb2 is the new version of vmdebootstrap. It can create disk images, for example for Raspberry PIs or VMs. You could also use it as a debian installer, if you like...

However, there was is a shortcomming in vmdb2: it can only create raw images - but what you eventually want for VMs is qcow2. However, when you install this patch (actually, only the changes around line 92 are required), you can do the following to install onto a qcow2 image directly:

qemu-img create -f qcow2 myimage.qcow2 40G
modprobe nbd max_part=8
qemu-nbd --connect=/dev/nbd0 --format=qcow2 --cache=none --detect-zeroes=on --aio=native myimage.qcow2
vmdb2 --image /dev/nbd0 config.vmdb2
qemu-nbd --disconnect /dev/nbd0

Note however, that you must not use the kpartx module in the vmdb2 script. For this purpose, it is not necessary because nbd will detect the partitions automatically. A minimal vmdb script would for example be:

steps:

  - mklabel: msdos
    device: "{{ image }}"

  - mkpart: primary
    device: "{{ image }}"
    start: 10M
    end: 100%
    tag: rootfs

  - mkfs: ext4
    partition: rootfs
    options: -E lazy_itable_init=0,lazy_journal_init=0

  - mount: rootfs

  - debootstrap: bullseye
    mirror: http://deb.debian.org/debian
    target: rootfs

  - apt: install
    packages:
      - linux-image-amd64
    tag: rootfs

  - grub: bios
    tag: rootfs
    image-dev: "{{ image }}"
Posted Tue Jan 4 13:54:01 2022
brother printer on arm64

I own a Brother HL-L5000D printer. This printer has no LAN port, thus I used an external print server. Many years I used a cheap Wifi access point with OpenWRT that had an extra USB port to run simultaneously as print server using the wonderful package p910nd. This server basically forwards data from port 9100 to the USB device. On the cheapo accesspoint, this was the only viable solution, as the device had only a tiny amount of RAM and flash...

Some day, the access point broke, so I swapped it for a Raspberry Pi 4B. It can easily handle cups but there is an issue: The Brother drivers are only published on their website for i686 and amd64... A week ago I noticed, that on the German website also a driver for Raspian is offered. The weird thing is, that is only shown there but not on the international page... Installing the package is easy, just enable armhf, install a libc and you are good to go:

dpkg --add-architecture armhf
apt update
apt install libc6:armhf

However, that package would not work for me. It would add the printer just fine, but I could not print the testpage. So lets look at the package content more closely:

# dpkg -L brgenprintml2pdrv:armhf
/.
/usr
/usr/share
/usr/share/doc
/var
/var/spool
/var/spool/lpd
/var/spool/lpd/BrGenPrintML2
/etc
/etc/opt
/etc/opt/brother
/etc/opt/brother/Printers
/etc/opt/brother/Printers/BrGenPrintML2
/etc/opt/brother/Printers/BrGenPrintML2/inf
/opt
/opt/brother
/opt/brother/Printers
/opt/brother/Printers/BrGenPrintML2
/opt/brother/Printers/BrGenPrintML2/LICENSE_JPN.txt
/opt/brother/Printers/BrGenPrintML2/cupswrapper
/opt/brother/Printers/BrGenPrintML2/cupswrapper/brother-BrGenPrintML2-cups-en.ppd
/opt/brother/Printers/BrGenPrintML2/cupswrapper/paperconfigml2
/opt/brother/Printers/BrGenPrintML2/cupswrapper/Copying
/opt/brother/Printers/BrGenPrintML2/cupswrapper/lpdwrapper
/opt/brother/Printers/BrGenPrintML2/lpd
/opt/brother/Printers/BrGenPrintML2/lpd/armv7l
/opt/brother/Printers/BrGenPrintML2/lpd/armv7l/brprintconflsr3
/opt/brother/Printers/BrGenPrintML2/lpd/armv7l/rawtobr3
/opt/brother/Printers/BrGenPrintML2/lpd/i686
/opt/brother/Printers/BrGenPrintML2/lpd/i686/brprintconflsr3
/opt/brother/Printers/BrGenPrintML2/lpd/i686/rawtobr3
/opt/brother/Printers/BrGenPrintML2/lpd/lpdfilter
/opt/brother/Printers/BrGenPrintML2/lpd/x86_64
/opt/brother/Printers/BrGenPrintML2/lpd/x86_64/brprintconflsr3
/opt/brother/Printers/BrGenPrintML2/lpd/x86_64/rawtobr3
/opt/brother/Printers/BrGenPrintML2/inf
/opt/brother/Printers/BrGenPrintML2/inf/setupPrintcap
/opt/brother/Printers/BrGenPrintML2/inf/brBrGenPrintML2rc
/opt/brother/Printers/BrGenPrintML2/inf/brBrGenPrintML2func
/opt/brother/Printers/BrGenPrintML2/LICENSE_ENG.txt

Uhhhm okay, there are x86_64 and i686 files as well... And now for the crucial part:

# ls -al /opt/brother/Printers/BrGenPrintML2/lpd
insgesamt 32
drwxr-xr-x 5 root root 4096 10. Dez 17:30 .
drwxr-xr-x 5 root root 4096 10. Dez 16:56 ..
drwxr-xr-x 2 root root 4096 10. Dez 16:56 armv7l
lrwxrwxrwx 1 root root   60 10. Dez 16:57 brprintconflsr3 -> /opt/brother/Printers/BrGenPrintML2/lpd/i686/brprintconflsr3
drwxr-xr-x 2 root root 4096 10. Dez 16:56 i686
-rwxr-xr-x 1 root root 6698 19. Apr 2017  lpdfilter
lrwxrwxrwx 1 root root   53 10. Dez 16:57 rawtobr3 -> /opt/brother/Printers/BrGenPrintML2/lpd/i686/rawtobr3
drwxr-xr-x 2 root root 4096 10. Dez 16:56 x86_64

Okay... thats the issue! The install scripts just assumes that it should install i686. Extracting the Debian package shows exactly that:

# mkdir -p brgen/DEBIAN
# dpkg -e brgenprintml2pdrv-4.0.0-1.armhf.deb brgen/DEBIAN/
# head -n 11 brgen/DEBIAN/postinst
#!/bin/sh
if [ "$(echo $(uname -m) | grep -i 'arm')" != '' ]; then
  ln -s /opt/brother/Printers/BrGenPrintML2/lpd/armv7l/rawtobr3         /opt/brother/Printers/BrGenPrintML2/lpd/rawtobr3
  ln -s /opt/brother/Printers/BrGenPrintML2/lpd/armv7l/brprintconflsr3         /opt/brother/Printers/BrGenPrintML2/lpd/brprintconflsr3
elif [ "$(echo $(uname -m) | grep -i -e 'x86_64' -e 'amd64')" != '' ]; then
  ln -s /opt/brother/Printers/BrGenPrintML2/lpd/x86_64/rawtobr3         /opt/brother/Printers/BrGenPrintML2/lpd/rawtobr3
  ln -s /opt/brother/Printers/BrGenPrintML2/lpd/x86_64/brprintconflsr3         /opt/brother/Printers/BrGenPrintML2/lpd/brprintconflsr3
else
  ln -s /opt/brother/Printers/BrGenPrintML2/lpd/i686/rawtobr3         /opt/brother/Printers/BrGenPrintML2/lpd/rawtobr3
  ln -s /opt/brother/Printers/BrGenPrintML2/lpd/i686/brprintconflsr3         /opt/brother/Printers/BrGenPrintML2/lpd/brprintconflsr3
fi

The reason is, that arm64 is actually called aarch64... Okay, so pretty easy to fix:

ln -s -f /opt/brother/Printers/BrGenPrintML2/lpd/armv7l/rawtobr3 /opt/brother/Printers/BrGenPrintML2/lpd/
ln -s -f /opt/brother/Printers/BrGenPrintML2/lpd/armv7l/brprintconflsr3 /opt/brother/Printers/BrGenPrintML2/lpd/

But then I noticed something else: The original driver actually ships the same files - including armhf, i686 and x86_64 - but a different PPD. The Raspbian uses a generic printer, but the other has a proper HL-L5000D.ppd. (Okay, to be fair: they are not the same files but the same folder structure and filenames)

So what you can do to get the original HL-L5000D printer driver:

mkdir -p hll5000dcupswrapper/DEBIAN
mkdir -p hll5000dlpr/DEBIAN
dpkg -x hll5000dcupswrapper-3.5.1-1.i386.deb hll5000dcupswrapper
dpkg -e hll5000dcupswrapper-3.5.1-1.i386.deb hll5000dcupswrapper/DEBIAN
dpkg -x hll5000dlpr-3.5.1-1.i386.deb hll5000dlpr
dpkg -e hll5000dlpr-3.5.1-1.i386.deb hll5000dlpr/DEBIAN
sed -i '/^Architecture/s/i386/all/' hll5000dcupswrapper/DEBIAN/control
sed "s/-i 'arm'/-i -e 'arm' -e 'aarch64'/" hll5000dlpr/DEBIAN/postinst
sed -i '/^Architecture/s/i386/all/' hll5000dlpr/DEBIAN/control
dpkg-deb -Z xz -b hll5000dcupswrapper
dpkg-deb -Z xz -b hll5000dlpr

This repackages the package to be installed on all architectures (okay, that is not correct but we do not care...) and also patches the line in the postinstall script to symlink the correct binaries. It would be neat to add the dependencies, like cups, because the postinst script actually requires some commands, but that is a job for another day...

I'm not sure if this works the same for other Brother printers, but I would assume it does.

Posted Fri Dec 10 18:38:58 2021
paraview reverse vector direction

I want to visualize vector fields in paraview. However, paraview has the property that it shows the vectors with the tail attached to the vertex at which they are specified. While this might be okay if you plot say velocity fields, it might be weird to look at when plotting force vectors acting on some surface vertices.

In this case, the vectors show inward into the surface, which is a bit confusing:

Thus, it would be nice to have the vectors pointing their tip at the specified node. Unfortunately, paraview does not seem to have a setting to do this automatically.

However, there is a trick to make it work! First of all, you have to invert the direction of the vector by using the calculator. Add a new Calculator filter, use Attribute Type "Point Data", select the vector and use the equation -dir (In my case the vector is called dir).

Then apply a new Glyph filter, however under the advanced properties, select "Invert":

Please find a paraview state file as an example here:

Posted Thu Jun 10 10:38:48 2021
wireguard dns only for vpn

I have to connect to a remote network using wireguard. The remote network has a DNS server to resolve all the internal hosts in the net. On Linux, I had a nice setup using systemd-resolved which would only ask the DNS server to resolve me hosts from that network but would still use my DNS server for all other requests. One reason might be that you also have internal hostnames on your own DNS and thus would loose ability to resolve them. Or, the VPN DNS server can not resolve AAAA records but you want to have a IPv6 connection as well. For Windows 10, I also had the issue that the DNS client would "loose" track which DNS server to ask and suddenly, I was not able to resolve the internal domains - even though the DNS server was set as the primary one.

The Linux setup is easy. Say there are the two DNS servers 10.0.0.1 and 10.0.0.2 in the remote network and the internal hosts are all in the domain internal.domain.name. Then specify in the wireguard config:

[Interface]
# ...
PostUp = resolvectl dns %i 10.0.0.1 10.0.0.2
PostUp = resolvectl dnssec %i no  # optional: enable, if the DNS server has DNSSec.
PostUp = resolvectl default-route %i no
PostUp = resolvectl domain %i internal.domain.name
# ...

You do not need a PostDown here, because resolvectl removes the entries on the interface automatically, if the interface is going down. Of course, this requires you to actually use systemd...

However, now I have to use a Windows 10 machine. The key component is the NRPT - the network resolution policy table. Windows provides some PowerShell commands to edit this table. With this tool, you can do exactly what we want: specify certain domains which are exclusivly resolved via another DNS server than the system's one.

First, enable script execution in Wireguard in the registry:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\WireGuard]
"DangerousScriptExecution"=dword:00000001

Read more about that in the Wireguard for Windows Documentation. Then, you can use PostUp and PostDown in the wireguard config:

PostUp = powershell -command "Add-DnsClientNrptRule -Namespace 'internal.domain.name','.internal.domain.name' -NameServers '10.0.0.1','10.0.0.2'"
PostDown = powershell -command "Get-DnsClientNrptRule | Where { $_.Namespace -match '.*internal\.domain\.name' } | Remove-DnsClientNrptRule -force"

Note, that we have to give the domain name twice: the first time to be able to resolve internal.domain.name and then with a prefixed dot, in order to resolve all hosts below. Also make sure that you comment out the DNS=10.0.0.1, 10.0.0.2 line in the wireguard config, as it would interfer with the powershell scripts.

One problem is, that you can not use nslookup to test if the setup works. You can use ping, which should be able to resolve correctly. Or you can use the powershell command Resolve-DnsName, which also gives a explaination how the name was resolved.

Posted Tue Mar 16 08:38:01 2021
no more nodm

Some time ago, I posted a recipe for running kodi-standalone in nodm.

Unfortunately, it is no longer viable, because nodm is deprecated and has two flaws:

  1. When the system is in shutdown, nodm will respawn itself after being killed by the system
  2. Since kodi 18.something, videos will freeze at the end and you can no longer see the kodi menus.

While the first one can be solved using a workaround in the systemd unit for nodm, the latter seems to be not fixable. Furthermore, nodm is no longer developed and also the developer suggest using lightdm and the autologin feature - which, by the way, solves both issues.

Posted Sat Nov 21 11:13:28 2020
recode samsung slowmo video

My Samsung S9 can produce nice slow-motion with 120fps. However, on the phone the videos are stored in 120fps, recoded to 29.99fps - so that you can view the slow-motion directly. But sometimes you also want the original video back. Here is a quick script to recode the video to normal speed:

FILENAME=video_from_phone.mp4
ffmpeg -i "$FILENAME" -vn -acodec pcm_s16le -ar 44100 -ac 2 output.wav
sox output.wav fixed.wav speed 8.0
ffmpeg -i "$FILENAME" -vf setpts=1/8*PTS -an -r 30 -crf 18 output.mp4
ffmpeg -i output.mp4 -i fixed.wav -c:v copy -c:a aac realtime.mp4
rm output.wav output.mp4 fixed.wav

Unfortunately, you can not use ffmpeg for the audio too... But the trick using sox works fine!

Posted Mon Nov 2 11:30:27 2020