Rather than using the ‘sun’ integration with ‘sunrise’ and ‘sunset’ events, or trying to offset these events by some fixed amount, I use an ESPHome sensor built from a Wemos D1 Mini (ESP8266) and a BH1750 ambient light sensor. This sensor is located in a window on the south side of my home, which receives the best amount of sunlight on any given day, year-round. I’ve found the ideal switch-point for turning on outdoor lights and switching camera profiles to be around 30 to 50 lux. With this in mind, I defined one Shell script in HA’s configuration.yaml file, and two automations in HA’s automation.yaml file.
First, the ESPHome code that runs on the Wemos D1 Mini ambient light sensor:
esphome:
name: ambientlight
platform: ESP8266
board: d1_mini_pro
wifi:
fast_connect: false
domain: .local
power_save_mode: NONE
reboot_timeout: 5min
networks:
- password: supersecret
ssid: SSID
use_address: ambientlight.local
i2c:
sda: D2
scl: D1
scan: True
sensor:
- platform: bh1750
name: "BH1750 Luminance"
address: 0x23
update_interval: 15s
accuracy_decimals: 0
logger:
logs: {}
level: DEBUG
api:
password: supersecret
reboot_timeout: 5min
port: 6053
ota:
password: supersecret
safe_mode: true
port: 8266
Then, the configuration.yaml shell command:
#
# Shell commands
# BASH shell scripts executed by HA Automations
#
shell_command:
dahua_profile_day: '/home/admin/DahuaDayNight DAY'
dahua_profile_night: '/home/admin/DahuaDayNight NIGHT'
Last, the two automation.yaml automations:
- id: '999999010'
alias: Dahua Cameras to Night Profile
description: Switch Dahua cameras to night time video profile
trigger:
- platform: numeric_state
entity_id: sensor.bh1750_luminance
below: '50'
for: 0:03:00
action:
- service: shell_command.dahua_profile_night
mode: single
- id: '999999020'
alias: Dahua Cameras to Day Profile
description: Switch Dahua cameras to day time video profile
trigger:
- platform: numeric_state
entity_id: sensor.bh1750_luminance
above: '40'
for: 0:03:00
action:
- service: shell_command.dahua_profile_day
mode: single
Finally, the Linux BASH script that runs on the Home Assistant Linux system:
#!/bin/bash
#
# Name:
# -----
# DahuaDayNight.bash
#
# Function:
# ---------
# Switch to day (1) or night (2) profiles on Dahua Cameras
#
# Argument(s):
# ------------
# 1 "day" or "night"
#
#
# Default MODE to zero - no actions
#
MODE=0
#
# Accepts "day", "Day", "DAY", "night", "Night", "NIGHT"
# Any other input performs no action.
# Convert input argument to UPPER case
#
arg=$( echo ${1:-null} | tr [:lower:] [:upper:] )
#
# Its DAY - use 1
#
if [ ${arg} = "DAY" ]
then
MODE=1
fi
#
# Its NIGHT - use 2
#
if [ ${arg} = "NIGHT" ]
then
MODE=2
fi
#
# Camera.dat - ASCII Text File, LF terminators, pipe-delimited
# 1 IP Address
# 2 Port Number
# 3 Manufacturer
# 4 Username
# 5 Password
# 6 Description
#
#
# Only if DAY or NIGHT was input, set
# the desired mode on all Dahua cameras
#
if [ ${MODE} != 0 ]
then
grep Dahua cameras.dat | awk -F\| '{printf "%s %s %s %s \"%s\"\n", $1, $2, $4, $5, $6}' | while read IP PORT USERNAME P
ASSWORD DESCRIPTION
do
/usr/bin/curl -s -g --digest -u ${USERNAME}:${PASSWORD} http://${IP}:${PORT}/cgi-bin/configManager.cgi?action=setCo
nfig\&VideoInMode[0].TimeSection[0][0]=${MODE}
done
fi
Here’s an example ‘cameras.dat’ file for your review:
#
# cameras.dat - Blue Iris camera config data
#
# 1 IP Address
# 2 Port Number
# 3 Manufacturer
# 4 Username
# 5 Password
# 6 Description
#
192.168.1.11|8011|Dahua|username|password|Back Door
192.168.1.12|8011|Dahua|username|password|Inside Garage
192.168.1.15|8011|Dahua|username|password|Driveway
192.168.1.17|8011|Amcrest|username|password|Bedroom
192.168.1.19|8011|Amcrest|username|password|Kitchen