Display values from Home Assistant on a display (1602A) using ESP32

I wanted to create a simple way to quickly display the current power consumption of the household and the power generation of a PV system.
Since I still had an ESP32 and a 1602A display, I created the display using the following code.

After the current power consumption, a simple bar is created using the # symbol, which roughly visualises the consumption.

Continue reading “Display values from Home Assistant on a display (1602A) using ESP32”

Home Assistant: Convert a negative value to a positive value

My PV system delivers a negative watt value as production at the Shelly. As I didn’t want to change it in the Shelly, I converted this value in the Home Assistant as follows:

Bash
template:
  - sensor:
      - name: "bkw1.watt"
        unique_id: bkw1.watt
        unit_of_measurement: "W"
        state: >
          {% set wert = states('sensor.sensor_shelly_bkw1_power') | float %}
          {{ -wert if wert < 0 else wert }}

InfluxDB Backup Script

A simple script to back up an InfluxDB

Bash
#!/bin/bash

# Configuration
INFLUXDB_HOST="http://localhost:8086"
INFLUXDB_ORG="ORG"
INFLUXDB_BUCKET="BUCKET"
INFLUXDB_TOKEN="my-token"  # Insert the actual token here
BACKUP_DIR="/opt/influxbu"
ROTATION_WEEKS=4

# Date for the backup
TIMESTAMP=$(date +"%Y-%m-%d")
BACKUP_FILE="$BACKUP_DIR/backup-$TIMESTAMP.tar.gz"

# Ensure the backup directory exists
mkdir -p "$BACKUP_DIR"

# Perform the backup
influx backup --host "$INFLUXDB_HOST" --token "$INFLUXDB_TOKEN" --org "$INFLUXDB_ORG" --bucket "$INFLUXDB_BUCKET" "$BACKUP_DIR/backup-$TIMESTAMP"

# Archive the backup
tar -czf "$BACKUP_FILE" -C "$BACKUP_DIR" "backup-$TIMESTAMP"
rm -rf "$BACKUP_DIR/backup-$TIMESTAMP"

# Delete old backups
find "$BACKUP_DIR" -type f -name "backup-*.tar.gz" -mtime +$((ROTATION_WEEKS * 7)) -exec rm {} \;

# Done
echo "Backup completed: $BACKUP_FILE"

Run via cron, eg.

Bash
0 20 * * * /root/influxbu.sh >> /var/log/influxdb_backup.log 2>&1

Home Assistant & InfluxDB & Grafana: Show device positions on map

To display device positions on a map with history, I first have the positions written to an InfluxDB. I display the data from the InfluxDB with Grafana.

The pivot function must be used for this: The pivot() function in InfluxDB (Flux query language) is used to reshape data by converting a long, narrow table into a wide table. It moves values from a column into multiple columns, making it easier to analyse and visualize time series data.

Example in line 6
from(bucket: "hass")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "device_tracker.iphone")
  |> filter(fn: (r) => r["_field"] == "longitude" or r["_field"] == "latitude")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

Result:

ESPHome: Connect and Query DS18B20 to ESP32

Boot the ESP32 first only with the one_wire config and note down the hardware address from the DS18B20. Then add the second part of the yaml (see below) to monitor the DS18B20.

YAML
esphome:
  name: esp-temp
  friendly_name: esp-temp

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxx"

ota:
  - platform: esphome
    password: "xxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp-Temp Fallback Hotspot"
    password: "xxxxxxxxxx"

captive_portal:

# <<<<<<<<< add thsi part to get the hardware adress from the DS18B20 >>>>>>>>>

one_wire:
 - id: bus_one
   platform: gpio
   pin:
    number: 4
    mode:
      input: true
      pullup: true

# <<<<<<<<< add this part after the first boot >>>>>>>>>
sensor:
  - platform: dallas_temp
    address: 0xe200000039xxxxxx
    name: "temp1"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 2
  - platform: dallas_temp
    address: 0xf400000035xxxxxx
    name: "temp2"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 2
  - platform: dallas_temp
    address: 0x6600000034xxxxxx
    name: "temp3"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 2
  - platform: dallas_temp
    address: 0xf000000034xxxxxx
    name: "temp4"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 2
  - platform: dallas_temp
    address: 0x5100000035xxxxxx
    name: "temp5"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 2