Files
zorruno-homeassistant/esphome/esp-leafbat.yaml
2025-05-18 16:35:38 +12:00

249 lines
8.4 KiB
YAML

#############################################
#############################################
# Nissan Leaf 12V Battery Monitor
# Monitoring the status of a vehicle 12V battery with
# an esp8266 (D1 Mini). It will obviously only
# transmit when the vehicle is within wifi range.
# Voltage is measured with a resistor voltage divider
# into the analogue GPIO on the esp8266.
# https://zorruno.com/2022/vehicle-12v-battery-monitoring/
##############################################
#############################################
#############################################
# Variable Substitutions
#############################################
substitutions:
devicename: "esp-leafbat"
friendly_name: "Nissan Leaf 12V Battery Monitor"
description_comment: "Nissan Leaf 12V Battery Monitor (when home)"
api_key: !secret esp-leafbat_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-leafbat_ota_pass #unfortunately you can't use substitutions in secrets names
wifi_ssid: !secret wifi_ssid
wifi_password: !secret wifi_password
fallback_ap_password: !secret fallback_ap_password
#Add these if we are giving it a static ip, or remove them in the Wifi section
#A static IP will speed things up slightly in that it doesn't have to negotiate DHCP
static_ip_address: !secret esp-leafbat_static_ip
static_ip_gateway: !secret esp-leafbat_gateway
static_ip_subnet: !secret esp-leafbat_subnet
mqtt_server: !secret mqtt_server
mqtt_username: !secret mqtt_username
mqtt_password: !secret mqtt_password
mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
mqtt_commandstopic: "viewroad-commands" #main topic for commands (ie sleep), call it what you like
#web_server_username: !secret web_server_username
#web_server_password: !secret web_server_password
update_time: 10s #update time for for general temp sensors etc
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${devicename}
friendly_name: ${friendly_name}
comment: ${description_comment} #appears on the esphome page in HA
min_version: 2024.6.0
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: d1_mini
framework:
version: latest #recommended, latest or dev
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: INFO #INFO Level suggested, or DEBUG for testing
#baud_rate: 0 #set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
#uart:
# id: uart_output
# tx_pin: GPIO12
# rx_pin: GPIO13
# baud_rate: 9600
#############################################
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
encryption:
key: ${api_key}
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: esphome
password: ${ota_pass}
#############################################
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################
safe_mode:
#############################################
# Wifi Settings
# https://esphome.io/components/wifi.html
#
# Power Save mode (can reduce wifi reliability)
# NONE (least power saving, Default for ESP8266)
# LIGHT (Default for ESP32)
# HIGH (most power saving)
#############################################
wifi:
ssid: ${wifi_ssid}
password: ${wifi_password}
#power_save_mode: LIGHT #https://esphome.io/components/wifi.html#wifi-power-save-mode
manual_ip: #optional static IP address
static_ip: ${static_ip_address}
gateway: ${static_ip_gateway}
subnet: ${static_ip_subnet}
ap: #Details for fallback hotspot (captive portal) in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${devicename} fallback AP
password: ${fallback_ap_password}
ap_timeout: 30min #Time until it brings up fallback AP. default is 1min
#############################################
# Web Portal for display and monitoring
#############################################
web_server:
port: 80
#version: 2
#include_internal: true
#ota: false
#auth:
#username: ${web_server_username}
#password: ${web_server_password}
#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
mqtt:
broker: ${mqtt_server}
topic_prefix: ${mqtt_topic}/${devicename}
username: ${mqtt_username}
password: ${mqtt_password}
discovery: False # enable entity discovery (true is default, we don't want two HA Instances)
# Availability Topic
birth_message:
topic: ${mqtt_topic}/${devicename}/availability
payload: online
will_message:
topic: ${mqtt_topic}/${devicename}/availability
payload: offline
# A way to prevent deep sleep using MQTT command
on_message:
- topic: ${mqtt_commandstopic}/${devicename}/deepsleep
payload: 'OFF'
then:
- deep_sleep.prevent: deep_sleep_1
- topic: ${mqtt_commandstopic}/${devicename}/deepsleep
payload: 'ON'
then:
- deep_sleep.enter: deep_sleep_1
########################################
# Deep Sleep
# https://esphome.io/components/deep_sleep.html
########################################
deep_sleep:
run_duration: 20s
sleep_duration: 5min
id: deep_sleep_1
#time:
# - platform: homeassistant
# id: ha_time
# timezone: Pacific/Auckland
#text_sensor:
# - platform: template
# name: "Current time"
# id: current_time
# lambda: return id(ha_time).now().strftime("%H%M");
# lambda: |-
# char str[17];
# time_t currTime = id(ha_time).now().timestamp;
# strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
# return { str };
# update_interval: ${update_time}
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
sensor:
#Quality of Wifi in dBm
- platform: wifi_signal
name: "WiFi Signal"
id: wifi_strength
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
#Analog sensor for voltage reading (A0)
- platform: uptime
name: "Uptime"
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
- platform: adc
pin: A0
name: "Battery Voltage"
id: battery_voltage
#mqtt_id: battery_voltage
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
# This set used a voltage divider with a 100k and a 4.7k R value
filters:
- multiply: 3.3 #D1 mini V divider, 3.3V -> 1V on esp8266
- calibrate_linear: #Read values with voltmeter and bench supply
- 0.72 -> 16.00
- 0.68 -> 15.00
- 0.63 -> 14.00
- 0.59 -> 13.00
- 0.55 -> 12.00
- 0.51 -> 11.00
- 0.45 -> 10.00
- 0.40 -> 09.00
- 0.36 -> 08.00
- 0.32 -> 07.00
- 0.28 -> 06.00
- 0.23 -> 05.00
# on_value:
# - logger.log:
# level: INFO
# format: "time,%s,battery,%.1f,wifi,%.1f"
# args: [ 'id(ha_time).now()', 'id(battery_voltage).state', 'id(wifi_strength).state' ]
# - uart.write:
# id: uart_output
# data: !lambda char buf[128];
# sprintf(buf, "%s", id(battery_voltage).state);
# std::string s = buf;
# return std::vector<unsigned char>( s.begin(), s.end() );
# - logger.log:
# level: INFO
# format: "time,%.1f,battery,%.1f,wifi,%.1f"
# args: [ 'id(sntp_time).now()', 'id(battery_voltage).state', 'id(wifi_strength).state' ]