Compare commits

...

55 Commits

Author SHA1 Message Date
root
540fe186ff ESPHome new device, DIN power mon PMB 2025-08-27 00:24:54 +12:00
root
c547bd8cb9 ESPHome new device, DIN power mon PMB 2025-08-27 00:11:56 +12:00
root
8adb0a63ba stair light control + pther fixes 2025-08-25 23:31:54 +12:00
root
40e289e99a improve power loss/on actions in V1.4 2025-08-22 09:05:15 +12:00
root
7aa8badcae max on time for LED in V1.3 2025-08-22 09:02:26 +12:00
root
ab1240e720 max on time for LED in V1.3 2025-08-22 00:34:28 +12:00
root
0dfa19b235 max on time for LED in V1.3 2025-08-22 00:33:30 +12:00
root
1b9ebd08d5 max on time for LED in V1.3 2025-08-22 00:32:42 +12:00
root
e3d0224364 max on time for LED in V1.3 2025-08-22 00:30:47 +12:00
root
77f2af3d74 max on time for LED in V1.3 2025-08-22 00:28:23 +12:00
root
a0588bf6c2 minor additions in V1.2 2025-08-22 00:08:51 +12:00
root
c73e41496d defaules in substitutions now V1.2 2025-08-21 20:01:39 +12:00
root
c76ca83932 minor changes on led controller V1.2 2025-08-20 00:13:29 +12:00
root
f7ad26c288 minor changes on led controller V1.2 2025-08-20 00:10:46 +12:00
root
27b26d165f minor changes on led controller V1.2 2025-08-20 00:06:33 +12:00
root
40ff88e0f5 esphome led controller V1.2 2025-08-18 18:47:23 +12:00
root
c286a11f8d Generic LED esphome controller V1.1 2025-08-18 17:07:49 +12:00
root
b5375b2219 pmb 6 button switch now V1.1 2025-08-06 16:36:08 +12:00
root
a6ddc60ec9 pmb 6 button switch now V1.1 2025-08-06 16:24:59 +12:00
root
f7ac6f7165 pmb 6 button switch now V1.1 2025-08-06 16:17:14 +12:00
root
4c8df42b89 more esphome pmb 6 button switch updates 2025-08-05 13:56:00 +12:00
root
ba4f7a19cc esphome pmb 6 button switch - all in 1 yaml 2025-08-05 13:31:08 +12:00
root
e96336dbc4 esphome pmb 6 button switch update 2025-08-05 00:21:37 +12:00
root
42796bc46e esphome updates 2025-08-04 23:26:44 +12:00
root
ac887724a2 multiple esphome device updates and new devices 2025-07-25 13:14:37 +12:00
root
d9ca44a9ee various esphome devices updates 2025-06-23 18:56:01 +12:00
root
6034bc0320 various esphome devices updates 2025-06-23 18:54:24 +12:00
root
23abe209a5 finally got sonoff dual r1 code esphome working as I need 2025-06-18 23:12:06 +12:00
root
4cd88a44a7 esphome pool light and downs dishwasher device swaps 2025-06-15 17:37:27 +12:00
root
4e0986a73f esphome minor additions and HA pool controls 2025-06-14 23:17:56 +12:00
root
5ef9d0df7d esphome sntp offline fixes 2025-06-14 16:39:38 +12:00
root
362b8119b4 a bunch of esphome improvements 2025-06-12 17:40:40 +12:00
root
dbd1342020 laundry esphome env gpio fixes 2025-05-31 00:41:23 +12:00
root
8bb951dc8f laundry esphome migration 2025-05-30 23:45:16 +12:00
root
b09fb0625b climate automation fix 2025-05-18 16:41:50 +12:00
root
0c3ba9cad8 various esphome changes 2025-05-18 16:35:38 +12:00
root
e60f404d05 esphome downstairs bath lightswitch 2025-03-03 22:30:15 +13:00
root
b152961ae1 esphome downstairs bath lightswitch 2025-03-03 20:59:59 +13:00
root
92a54f7ae5 various incl esphome devices 2025-02-15 17:16:41 +13:00
root
70ed59fef8 various incl zha quirks 2024-12-13 14:33:44 +13:00
root
20044d73ea esphome mi 1s lamp 2024-09-04 22:51:11 +12:00
root
83549c6c1e esphome mi 1s lamp 2024-09-04 22:46:30 +12:00
root
74d1c18615 esphome weather staion v5 2024-06-14 23:44:42 +12:00
root
efaaf8c678 esphome weather staion v5 2024-06-14 23:38:45 +12:00
root
6ff9e38e5f esphome weather staion v5 2024-06-14 23:03:05 +12:00
root
8851f8da5e various esphome yaml tidyups 2024-06-06 11:08:40 +12:00
root
29c092232b weather station esphome updates 2024-06-02 18:49:57 +12:00
root
9467d25271 weather station esphome updates 2024-06-02 18:46:17 +12:00
root
3d05e58e9b weather station esphome 2024-05-27 22:24:25 +12:00
root
d920212d3a weather station esphome and others 2024-05-27 22:22:08 +12:00
root
155725ba3a weather station esphome and others 2024-05-27 22:20:04 +12:00
root
945177d535 weather station esphome and others 2024-05-27 22:17:11 +12:00
root
2296a4eef0 ESPresence comments 2023-10-24 11:35:44 +13:00
root
9c9502685f ESPresence, OBK, and other updates 2023-10-20 13:30:32 +13:00
root
910a908880 ESPresence, OBK, and other updates 2023-10-20 13:27:48 +13:00
138 changed files with 23131 additions and 544 deletions

View File

@@ -1,48 +1,35 @@
- id: '1671746148141'
alias: Turn Off Xmas Decorations
- id: '1693027703299'
alias: Downstairs Kitchen turn ON LEDs on with main lights
description: ''
trigger:
- platform: state
entity_id:
- input_boolean.xmas_decorations
from: 'on'
to: 'off'
condition: []
action:
- type: turn_off
device_id: bbfa51e4e883fafa1d38824f11d83ba1
entity_id: switch.xmas_decoration_power_plug_zpp02
domain: switch
- type: turn_off
device_id: 506b3518661f7b2854e42ba5a26e0610
entity_id: switch.kogan_smarterhometm_smart_plug_new_socket_1
domain: switch
- type: turn_off
device_id: a9865914a7b7de1e81d1df47784282a8
entity_id: switch.tasmota_5
triggers:
- type: turned_on
device_id: 1a7e2bfdfaa08e2b78bcbd5d5d12d2a7
entity_id: ef02e93de5fdda30bea18dac1316927a
domain: switch
trigger: device
conditions: []
actions:
- data:
transition: 20
brightness_pct: 95
target:
entity_id: light.controller_dimmable_3c317c
action: light.turn_on
mode: single
- id: '1671746242166'
alias: Turn On Xmas Decorations
- id: '1693027792035'
alias: Downstairs Kitchen turn OFF LEDs with main lights
description: ''
trigger:
- platform: state
entity_id:
- input_boolean.xmas_decorations
from: 'off'
to: 'on'
condition: []
action:
- type: turn_on
device_id: bbfa51e4e883fafa1d38824f11d83ba1
entity_id: switch.xmas_decoration_power_plug_zpp02
domain: switch
- type: turn_on
device_id: 506b3518661f7b2854e42ba5a26e0610
entity_id: switch.kogan_smarterhometm_smart_plug_new_socket_1
domain: switch
- type: turn_on
device_id: a9865914a7b7de1e81d1df47784282a8
entity_id: switch.tasmota_5
triggers:
- type: turned_off
device_id: 1a7e2bfdfaa08e2b78bcbd5d5d12d2a7
entity_id: ef02e93de5fdda30bea18dac1316927a
domain: switch
trigger: device
conditions: []
actions:
- data:
transition: 20
target:
entity_id: light.controller_dimmable_3c317c
action: light.turn_off
mode: single

View File

@@ -9,6 +9,6 @@
action:
- type: turn_off
device_id: 4a9f71fc64e158f1c9286d6e43ce782e
entity_id: remote.sony_kd_55x85k
entity_id: remote.lounge_tv
domain: remote
mode: single

View File

@@ -0,0 +1,69 @@
- id: "Two_Way_Lights_-_Garage_Corridor_-_1"
alias: "Two Way Lights - Garage Corridor - 1"
trigger:
- platform: state
entity_id: switch.garage_entry_switch_1
action:
- choose:
- conditions:
- condition: state
entity_id: switch.garage_entry_switch_1
state: "on"
sequence:
- service: switch.turn_on
entity_id: switch.tasmo_ks811d_5613_lounge_4b
default:
- service: switch.turn_off
entity_id: switch.tasmo_ks811d_5613_lounge_4b
- id: "Two_Way_Lights_-_Garage_Corridor_-_2"
alias: "Two Way Lights - Garage Corridor - 2"
trigger:
- platform: state
entity_id: switch.tasmo_ks811d_5613_lounge_4b
action:
- choose:
- conditions:
- condition: state
entity_id: switch.tasmo_ks811d_5613_lounge_4b
state: "on"
sequence:
- service: switch.turn_on
entity_id: switch.garage_entry_switch_1
default:
- service: switch.turn_off
entity_id: switch.garage_entry_switch_1
- id: "Two_Way_Lights_-_Garage_Main_-_1"
alias: "Two Way Lights - Garage Main - 1"
trigger:
- platform: state
entity_id: switch.garage_entry_switch_3
action:
- choose:
- conditions:
- condition: state
entity_id: switch.garage_entry_switch_3
state: "on"
sequence:
- service: switch.turn_on
entity_id: switch.tasmo_sbas_0407_garagelight
default:
- service: switch.turn_off
entity_id: switch.tasmo_sbas_0407_garagelight
- id: "Two_Way_Lights_-_Garage_Main_-_2"
alias: "Two Way Lights - Garage Main - 2"
trigger:
- platform: state
entity_id: switch.tasmo_sbas_0407_garagelight
action:
- choose:
- conditions:
- condition: state
entity_id: switch.tasmo_sbas_0407_garagelight
state: "on"
sequence:
- service: switch.turn_on
entity_id: switch.garage_entry_switch_3
default:
- service: switch.turn_off
entity_id: switch.garage_entry_switch_3

View File

@@ -7,7 +7,7 @@
day: 1
time: "21:00:00"
actions:
- service: notify.pushover_notifications
- service: notify.pushover_2
data:
message: >
Weekly Battery Check. Low Devices:

View File

@@ -19,7 +19,7 @@
{% endfor -%}
{{ ns.found }}
action:
- service: notify.pushover_notifications
- service: notify.pushover_2
data:
title: View Road Sensors
message: >

View File

@@ -8,6 +8,7 @@ homeassistant:
customize: !include customize.yaml
packages: !include_dir_named packages
template: !include_dir_merge_list templates
#group: !include groups.yaml
group: !include_dir_merge_named group/
automation: !include_dir_merge_list automations/
@@ -19,23 +20,32 @@ scene: !include scenes.yaml
#sensor merged: !include_dir_merge_list sensor/
sensor: !include_dir_merge_list sensor/
# Example base configuration.yaml entry
conversation:
logger:
default: info
logs:
custom_components.hacs: debug
custom_components.evnex: debug
evnex: debug
# logs:
#custom_components.hacs: debug
#custom_components.evnex: debug
#evnex: debug
zha:
enable_quirks: true
custom_quirks_path: /config/custom_zha_quirks/
device_config: # https://community.home-assistant.io/t/turning-a-light-entity-to-switch-entity/589599/7
a4:c1:38:02:58:65:86:ed-1: # format: {ieee}-{endpoint_id}
type: “switch” # corrected device type
a4:c1:38:a1:db:1d:38:b7-1: # format: {ieee}-{endpoint_id}
type: “switch” # corrected device type
zigpy_config:
ota:
otau_directory: /config/zigpy_ota
ikea_provider: true
ledvance_provider: true
salus_provider: true
inovelli_provider: true
thirdreality_provider: true
device_tracker:
- platform: bluetooth_le_tracker
input_boolean:
foxhole_occupied:
name: Foxhole Guest Occupied
icon: mdi:briefcase-plus-outline
quiet_time:
name: Quiet time for no notifications
icon: mdi:shield-moon
away_occupied_routine:
name: Automation for Lights etc when away
icon: mdi:shield-lock

View File

@@ -0,0 +1,137 @@
"""Quirk for TS0207 rain sensors."""
import zigpy.types as t
from typing import Any, Type
from zigpy.profiles import zha
from zigpy.quirks import CustomDevice, CustomCluster
from zigpy.zcl.clusters.general import (
Basic,
Groups,
Identify,
OnOff,
Ota,
PowerConfiguration,
Scenes,
Time,
)
from zigpy.zcl.clusters.lightlink import LightLink
from zigpy.zcl.clusters.security import IasZone
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.tuya.mcu import TuyaMCUCluster
from zhaquirks.tuya import (
TuyaManufCluster,
DPToAttributeMapping,
EnchantedDevice,
TuyaNoBindPowerConfigurationCluster,
)
ZONE_TYPE = 0x0001
class TuyaSolarRainSensorCluster(TuyaMCUCluster):
"""Tuya manufacturer cluster."""
attributes = TuyaMCUCluster.attributes.copy()
attributes.update(
{
0xEF65: ("light_intensity", t.uint32_t, True),
0xEF66: ("average_light_intensity_20mins", t.uint32_t, True),
0xEF67: ("todays_max_light_intensity", t.uint32_t, True),
0xEF68: ("cleaning_reminder", t.Bool, True),
0xEF69: ("rain_sensor_voltage", t.uint32_t, True),
}
)
dp_to_attribute: dict[int, DPToAttributeMapping] = {
101: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"light_intensity",
),
102: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"average_light_intensity_20mins",
),
103: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"todays_max_light_intensity",
),
104: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"cleaning_reminder",
),
105: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"rain_sensor_voltage",
),
}
data_point_handlers = {
101: "_dp_2_attr_update",
102: "_dp_2_attr_update",
103: "_dp_2_attr_update",
104: "_dp_2_attr_update",
105: "_dp_2_attr_update",
}
class TuyaIasZone(CustomCluster, IasZone):
"""IAS Zone for rain sensors."""
_CONSTANT_ATTRIBUTES = {ZONE_TYPE: IasZone.ZoneType.Water_Sensor}
class TuyaSolarRainSensor(EnchantedDevice):
"""TS0207 Rain sensor quirk."""
signature = {
MODELS_INFO: [("_TZ3210_tgvtvdoc", "TS0207")],
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.IAS_ZONE,
INPUT_CLUSTERS: [
Basic.cluster_id,
PowerConfiguration.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
IasZone.cluster_id,
TuyaMCUCluster.cluster_id,
],
OUTPUT_CLUSTERS: [
Identify.cluster_id,
Groups.cluster_id,
OnOff.cluster_id,
Time.cluster_id,
Ota.cluster_id,
LightLink.cluster_id,
],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.IAS_ZONE,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaNoBindPowerConfigurationCluster,
TuyaIasZone,
TuyaSolarRainSensorCluster,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}

View File

@@ -0,0 +1,358 @@
# rivsc https://blog.rivsc.ovh
import logging
from typing import Final
from zigpy.quirks.v2 import QuirkBuilder, BinarySensorDeviceClass
import zigpy.types as t
from zigpy.zcl.foundation import ZCLAttributeDef
from zigpy.zcl.clusters.measurement import (
IlluminanceMeasurement,
OccupancySensing,
)
from zigpy.zcl.clusters.security import IasZone
from zigpy.quirks.v2.homeassistant import EntityPlatform, EntityType
from zhaquirks.tuya import (
TuyaLocalCluster,
TuyaPowerConfigurationCluster2AAA,
)
from zhaquirks.tuya.mcu import TuyaMCUCluster, DPToAttributeMapping
class PresenceState(t.enum8):
"""Presence State values"""
none = 0x00
presence = 0x01
peaceful = 0x02
small_movement = 0x03
large_movement = 0x04
class TuyaOccupancySensing(OccupancySensing, TuyaLocalCluster):
"""Tuya local OccupancySensing cluster."""
class TuyaIlluminanceMeasurement(IlluminanceMeasurement, TuyaLocalCluster):
"""Tuya local IlluminanceMeasurement cluster."""
class HumanPresenceSensorManufCluster(TuyaMCUCluster):
"""Human Presence Sensor ZG-205Z (5.8GHz)"""
class AttributeDefs(TuyaMCUCluster.AttributeDefs):
"""Tuya DataPoints attributes"""
# Presence state
presence_state: Final = ZCLAttributeDef(
id=0x0001, # DP 1
type=t.uint16_t,
access="rp",
is_manufacturer_specific=True,
)
# Target distance
target_distance: Final = ZCLAttributeDef(
id=0x0101, # DP 101
type=t.uint16_t,
access="rp",
is_manufacturer_specific=True,
)
# Illuminance value
illuminance_lux: Final = ZCLAttributeDef(
id=0x0102, # DP 102
type=t.uint16_t,
access="rp",
is_manufacturer_specific=True,
)
# None delay time (presence keep time)
none_delay_time: Final = ZCLAttributeDef(
id=0x0103, # DP 103
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Indicator
indicator: Final = ZCLAttributeDef(
id=0x0104, # DP 104
type=t.Bool,
is_manufacturer_specific=True,
)
# Move detection max distance
move_detection_max: Final = ZCLAttributeDef(
id=0x0107, # DP 107
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Move detection min distance
move_detection_min: Final = ZCLAttributeDef(
id=0x0108, # DP 108
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Breath detection max distance
breath_detection_max: Final = ZCLAttributeDef(
id=0x0109, # DP 109
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Breath detection min distance
breath_detection_min: Final = ZCLAttributeDef(
id=0x0110, # DP 110
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Small move detection max distance
small_move_detection_max: Final = ZCLAttributeDef(
id=0x0114, # DP 114
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Small move detection min distance
small_move_detection_min: Final = ZCLAttributeDef(
id=0x0115, # DP 115
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Move sensitivity
move_sensitivity: Final = ZCLAttributeDef(
id=0x0116, # DP 116
type=t.uint8_t,
is_manufacturer_specific=True,
)
# Small move sensitivity
small_move_sensitivity: Final = ZCLAttributeDef(
id=0x0117, # DP 117
type=t.uint8_t,
is_manufacturer_specific=True,
)
# Breath sensitivity
breath_sensitivity: Final = ZCLAttributeDef(
id=0x0118, # DP 118
type=t.uint8_t,
is_manufacturer_specific=True,
)
dp_to_attribute: dict[int, DPToAttributeMapping] = {
1: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"presence_state",
converter=PresenceState
),
101: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"target_distance",
converter=lambda x: x / 100 if x is not None else 0
),
102: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"illuminance_lux",
),
103: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"none_delay_time",
converter=lambda x: x if x is not None else 30
),
104: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"indicator",
),
107: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"move_detection_max",
converter=lambda x: x / 100 if x is not None else 10
),
108: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"move_detection_min",
converter=lambda x: x / 100 if x is not None else 0
),
109: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"breath_detection_max",
converter=lambda x: x / 100 if x is not None else 6
),
110: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"breath_detection_min",
converter=lambda x: x / 100 if x is not None else 0
),
114: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"small_move_detection_max",
converter=lambda x: x / 100 if x is not None else 6
),
115: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"small_move_detection_min",
converter=lambda x: x / 100 if x is not None else 0
),
116: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"move_sensitivity",
converter=lambda x: x if x is not None else 5
),
117: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"small_move_sensitivity",
converter=lambda x: x if x is not None else 5
),
118: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"breath_sensitivity",
converter=lambda x: x if x is not None else 5
),
}
data_point_handlers = {
1: "_dp_2_attr_update",
101: "_dp_2_attr_update",
102: "_dp_2_attr_update",
103: "_dp_2_attr_update",
104: "_dp_2_attr_update",
107: "_dp_2_attr_update",
108: "_dp_2_attr_update",
109: "_dp_2_attr_update",
110: "_dp_2_attr_update",
114: "_dp_2_attr_update",
115: "_dp_2_attr_update",
116: "_dp_2_attr_update",
117: "_dp_2_attr_update",
118: "_dp_2_attr_update",
}
(
QuirkBuilder("_TZE204_dapwryy7", "TS0601")
.skip_configuration()
.removes(IasZone.cluster_id)
.adds(HumanPresenceSensorManufCluster)
#.adds(TuyaOccupancySensing)
.replaces(TuyaPowerConfigurationCluster2AAA)
.replaces(TuyaIlluminanceMeasurement)
.binary_sensor(
"presence_state",
HumanPresenceSensorManufCluster.cluster_id,
endpoint_id=1,
#entity_type=EntityType.STANDARD, # very soon (zigpy channel #dev on github
device_class=BinarySensorDeviceClass.OCCUPANCY,
fallback_name="Presence"
)
.enum(
HumanPresenceSensorManufCluster.AttributeDefs.presence_state.name,
PresenceState,
HumanPresenceSensorManufCluster.cluster_id,
entity_platform=EntityPlatform.SENSOR,
entity_type=EntityType.STANDARD,
fallback_name="Presence State",
translation_key="presence_state"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.target_distance.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=10,
#unit="m", # fail :/
fallback_name="Target Distance (m)",
translation_key="target_distance",
#entity_type=EntityType.STANDARD # not yet :/
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.none_delay_time.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=28800,
unit="s",
fallback_name="Hold Delay Time",
translation_key="none_delay_time"
)
.switch(
HumanPresenceSensorManufCluster.AttributeDefs.indicator.name,
HumanPresenceSensorManufCluster.cluster_id,
fallback_name="LED Indicator",
translation_key="indicator"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.move_detection_max.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=10,
#unit="m",
fallback_name="Move Detection Max Distance (m)",
translation_key="move_detection_max"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.move_detection_min.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=10,
#unit="m",
fallback_name="Move Detection Min Distance (m)",
translation_key="move_detection_min"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.small_move_detection_max.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
#unit="m",
fallback_name="Small Move Detection Max Distance (m)",
translation_key="small_move_detection_max"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.small_move_detection_min.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
#unit="m",
fallback_name="Small Move Detection Min Distance (m)",
translation_key="small_move_detection_min"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.breath_detection_max.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
#unit="m",
fallback_name="Breath Detection Max Distance (m)",
translation_key="breath_detection_max"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.breath_detection_min.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
#unit="m",
fallback_name="Breath Detection Min Distance (m)",
translation_key="breath_detection_min"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.move_sensitivity.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=10,
fallback_name="Move Sensitivity",
translation_key="move_sensitivity"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.small_move_sensitivity.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=10,
fallback_name="Small Move Sensitivity",
translation_key="small_move_sensitivity"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.breath_sensitivity.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=10,
fallback_name="Breath Sensitivity",
translation_key="breath_sensitivity"
)
.add_to_registry()
)

View File

@@ -0,0 +1,361 @@
import logging
from typing import Final
from zigpy.quirks.v2 import QuirkBuilder, BinarySensorDeviceClass
import zigpy.types as t
from zigpy.zcl.foundation import ZCLAttributeDef
from zigpy.zcl.clusters.measurement import (
IlluminanceMeasurement,
OccupancySensing,
)
from zigpy.zcl.clusters.security import IasZone
from zigpy.quirks.v2.homeassistant import EntityPlatform, EntityType
from zhaquirks.tuya import (
TuyaLocalCluster,
TuyaPowerConfigurationCluster2AAA,
)
from zhaquirks.tuya.mcu import TuyaMCUCluster, DPToAttributeMapping
class PresenceState(t.enum8):
"""Presence State values"""
none = 0x00
presence = 0x01
peaceful = 0x02
small_movement = 0x03
large_movement = 0x04
class TuyaOccupancySensing(OccupancySensing, TuyaLocalCluster):
"""Tuya local OccupancySensing cluster."""
class TuyaIlluminanceMeasurement(IlluminanceMeasurement, TuyaLocalCluster):
"""Tuya local IlluminanceMeasurement cluster."""
class HumanPresenceSensorManufCluster(TuyaMCUCluster):
"""Human Presence Sensor ZG-205Z (5.8GHz)"""
class AttributeDefs(TuyaMCUCluster.AttributeDefs):
"""Tuya DataPoints attributes"""
# Presence state
presence_state: Final = ZCLAttributeDef(
id=0x0001, # DP 1
type=t.uint16_t,
access="rp",
is_manufacturer_specific=True,
)
# Target distance
target_distance: Final = ZCLAttributeDef(
id=0x0101, # DP 101
type=t.uint16_t,
access="rp",
is_manufacturer_specific=True,
)
# Illuminance value
illuminance_lux: Final = ZCLAttributeDef(
id=0x0102, # DP 102
type=t.uint16_t,
access="rp",
is_manufacturer_specific=True,
)
# None delay time (presence keep time)
none_delay_time: Final = ZCLAttributeDef(
id=0x0103, # DP 103
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Indicator
indicator: Final = ZCLAttributeDef(
id=0x0104, # DP 104
type=t.Bool,
is_manufacturer_specific=True,
)
# Move detection max distance
move_detection_max: Final = ZCLAttributeDef(
id=0x0107, # DP 107
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Move detection min distance
move_detection_min: Final = ZCLAttributeDef(
id=0x0108, # DP 108
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Breath detection max distance
breath_detection_max: Final = ZCLAttributeDef(
id=0x0109, # DP 109
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Breath detection min distance
breath_detection_min: Final = ZCLAttributeDef(
id=0x0110, # DP 110
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Small move detection max distance
small_move_detection_max: Final = ZCLAttributeDef(
id=0x0114, # DP 114
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Small move detection min distance
small_move_detection_min: Final = ZCLAttributeDef(
id=0x0115, # DP 115
type=t.uint16_t,
is_manufacturer_specific=True,
)
# Move sensitivity (1 is higher, 9 is lower)
move_sensitivity: Final = ZCLAttributeDef(
id=0x0116, # DP 116
type=t.uint8_t,
is_manufacturer_specific=True,
)
# Small move sensitivity
small_move_sensitivity: Final = ZCLAttributeDef(
id=0x0117, # DP 117
type=t.uint8_t,
is_manufacturer_specific=True,
)
# Breath sensitivity
breath_sensitivity: Final = ZCLAttributeDef(
id=0x0118, # DP 118
type=t.uint8_t,
is_manufacturer_specific=True,
)
dp_to_attribute: dict[int, DPToAttributeMapping] = {
1: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"presence_state",
converter=PresenceState
),
101: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"target_distance",
converter=lambda x: x / 100 if x is not None else 0
),
102: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"illuminance_lux",
),
103: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"none_delay_time",
# Value in Tuya App is 30 after Factory reset
#converter=lambda x: x if x is not None else 30
),
104: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"indicator",
),
107: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"move_detection_max",
converter=lambda x: x / 100 if x is not None else 10
),
108: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"move_detection_min",
converter=lambda x: x / 100 if x is not None else 0
),
109: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"breath_detection_max",
converter=lambda x: x / 100 if x is not None else 6
),
110: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"breath_detection_min",
converter=lambda x: x / 100 if x is not None else 0
),
114: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"small_move_detection_max",
converter=lambda x: x / 100 if x is not None else 6
),
115: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"small_move_detection_min",
converter=lambda x: x / 100 if x is not None else 0
),
116: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"move_sensitivity",
converter=lambda x: x if x is not None else 5
),
117: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"small_move_sensitivity",
converter=lambda x: x if x is not None else 5
),
118: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"breath_sensitivity",
converter=lambda x: x if x is not None else 5
),
}
data_point_handlers = {
1: "_dp_2_attr_update",
101: "_dp_2_attr_update",
102: "_dp_2_attr_update",
103: "_dp_2_attr_update",
104: "_dp_2_attr_update",
107: "_dp_2_attr_update",
108: "_dp_2_attr_update",
109: "_dp_2_attr_update",
110: "_dp_2_attr_update",
114: "_dp_2_attr_update",
115: "_dp_2_attr_update",
116: "_dp_2_attr_update",
117: "_dp_2_attr_update",
118: "_dp_2_attr_update",
}
(
QuirkBuilder("_TZE204_dapwryy7", "TS0601")
.skip_configuration()
.removes(IasZone.cluster_id)
.adds(HumanPresenceSensorManufCluster)
#.adds(TuyaOccupancySensing)
.replaces(TuyaPowerConfigurationCluster2AAA)
.replaces(TuyaIlluminanceMeasurement)
.binary_sensor(
"presence_state",
HumanPresenceSensorManufCluster.cluster_id,
endpoint_id=1,
#entity_type=EntityType.STANDARD, # very soon (zigpy channel #dev on github
device_class=BinarySensorDeviceClass.OCCUPANCY,
fallback_name="Presence"
)
.enum(
HumanPresenceSensorManufCluster.AttributeDefs.presence_state.name,
PresenceState,
HumanPresenceSensorManufCluster.cluster_id,
entity_platform=EntityPlatform.SENSOR,
entity_type=EntityType.STANDARD,
fallback_name="Presence State",
translation_key="presence_state"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.illuminance_lux.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=2500,
fallback_name="Illuminance (lx)",
translation_key="illuminance_lux",
#entity_type=EntityType.STANDARD # not yet :/
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.target_distance.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=10,
fallback_name="Target Distance (m)",
translation_key="target_distance",
#entity_type=EntityType.STANDARD # not yet :/
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.none_delay_time.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=28800,
#unit="s",
fallback_name="Hold Delay Time",
translation_key="none_delay_time"
)
.switch(
HumanPresenceSensorManufCluster.AttributeDefs.indicator.name,
HumanPresenceSensorManufCluster.cluster_id,
fallback_name="LED Indicator",
translation_key="indicator"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.move_detection_max.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=10,
fallback_name="Move Detection Max Distance (m)",
translation_key="move_detection_max"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.move_detection_min.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=10,
fallback_name="Move Detection Min Distance (m)",
translation_key="move_detection_min"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.small_move_detection_max.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
fallback_name="Small Move Detection Max Distance (m)",
translation_key="small_move_detection_max"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.small_move_detection_min.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
fallback_name="Small Move Detection Min Distance (m)",
translation_key="small_move_detection_min"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.breath_detection_max.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
fallback_name="Breath Detection Max Distance (m)",
translation_key="breath_detection_max"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.breath_detection_min.name,
HumanPresenceSensorManufCluster.cluster_id,
step=0.01,
min_value=0,
max_value=6,
fallback_name="Breath Detection Min Distance (m)",
translation_key="breath_detection_min"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.move_sensitivity.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=10,
fallback_name="Move Sensitivity",
translation_key="move_sensitivity"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.small_move_sensitivity.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=10,
fallback_name="Small Move Sensitivity",
translation_key="small_move_sensitivity"
)
.number(
HumanPresenceSensorManufCluster.AttributeDefs.breath_sensitivity.name,
HumanPresenceSensorManufCluster.cluster_id,
step=1,
min_value=0,
max_value=10,
fallback_name="Breath Sensitivity",
translation_key="breath_sensitivity"
)
.add_to_registry()
)

View File

@@ -0,0 +1,471 @@
"""Tuya Din Power Meter."""
import logging
from zigpy.profiles import zha
import zigpy.types as t
from zigpy.zcl.clusters.general import Basic, GreenPowerProxy, Groups, Ota, Scenes, Time
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
from zigpy.zcl.clusters.smartenergy import Metering
from zhaquirks import Bus, LocalDataCluster
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.tuya import TuyaManufClusterAttributes, TuyaOnOff, TuyaSwitch
TUYA_TOTAL_ENERGY_ATTR = 0x0211 #energy
TUYA_CURRENT_ATTR = 0x0212 #current
TUYA_POWER_ATTR = 0x0213 #power
TUYA_VOLTAGE_ATTR = 0x0214 #voltaje
TUYA_DIN_SWITCH_ATTR = 0x0101 #switch
SWITCH_EVENT = "switch_event"
"""Hiking Power Meter Attributes"""
HIKING_DIN_SWITCH_ATTR = 0x0110
HIKING_TOTAL_ENERGY_DELIVERED_ATTR = 0x0201
HIKING_TOTAL_ENERGY_RECEIVED_ATTR = 0x0266
HIKING_VOLTAGE_CURRENT_ATTR = 0x0006
HIKING_POWER_ATTR = 0x0267
HIKING_FREQUENCY_ATTR = 0x0269
HIKING_POWER_FACTOR_ATTR = 0x026F
HIKING_TOTAL_REACTIVE_ATTR = 0x026D
HIKING_REACTIVE_POWER_ATTR = 0x026E
"""MatSee Power Meter Attributes"""
MATSEE_DPID_POWER_TOTAL_ID_ATTR = 0x0273
MATSEE_DPID_POWER_ID_A_ATTR = 0x0265
MATSEE_DPID_POWER_ID_B_ATTR = 0x0269
MATSEE_DPID_POWER_DIRECTION_ID_A_ATTR = 0x0266
MATSEE_DPID_POWER_DIRECTION_ID_B_ATTR = 0x0268
MATSEE_DPID_FORWARD_ENERGY_TOTAL_A_ATTR = 0x026A
MATSEE_DPID_REVERSE_ENERGY_TOTAL_A_ATTR = 0x026B
MATSEE_DPID_FORWARD_ENERGY_TOTAL_B_ATTR = 0x026C
MATSEE_DPID_REVERSE_ENERGY_TOTAL_B_ATTR = 0x026D
MATSEE_DPID_POWER_FACTOR_A_ATTR = 0x026E
MATSEE_DPID_POWER_FACTOR_B_ATTR = 0x0279
MATSEE_DPID_POWER_FREQ_ATTR = 0x026F
MATSEE_DPID_VOLTAGE_A_ATTR = 0x0270
MATSEE_DPID_CURRENT_A_ATTR = 0x0271
MATSEE_DPID_CURRENT_B_ATTR = 0x0272
MATSEE_DPID_UPDATE_RATE_ATTR = 0x0281
MATSEE_DPID_VOLTAGE_A_COEF_ATTR = 0x0274
MATSEE_DPID_CURRENT_A_COEF_ATTR = 0x0275
MATSEE_DPID_POWER_A_COEF_ATTR = 0x0276
MATSEE_DPID_ENERGY_A_COEF_ATTR = 0x0277
MATSEE_DPID_ENERGY_A_COEF_REV_ATTR = 0x027F
MATSEE_DPID_FREQ_COEF_ATTR = 0x027A
MATSEE_DPID_CURRENT_B_COEF_ATTR = 0x027B
MATSEE_DPID_POWER_B_COEF_ATTR = 0x027C
MATSEE_DPID_ENERGY_B_COEF_ATTR = 0x027D
MATSEE_DPID_ENERGY_B_COEF_REV_ATTR = 0x0280
_LOGGER = logging.getLogger(__name__)
class TuyaManufClusterDinPower(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the Tuya Power Meter device."""
attributes = {
TUYA_TOTAL_ENERGY_ATTR: ("energy", t.uint32_t, True),
TUYA_CURRENT_ATTR: ("current", t.int16s, True),
TUYA_POWER_ATTR: ("power", t.uint16_t, True),
TUYA_VOLTAGE_ATTR: ("voltage", t.uint16_t, True),
TUYA_DIN_SWITCH_ATTR: ("switch", t.uint8_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == TUYA_TOTAL_ENERGY_ATTR:
self.endpoint.smartenergy_metering.energy_deliver_reported(value / 100)
elif attrid == TUYA_CURRENT_ATTR:
self.endpoint.electrical_measurement.current_reported(value)
elif attrid == TUYA_POWER_ATTR:
self.endpoint.electrical_measurement.power_reported(value / 10)
elif attrid == TUYA_VOLTAGE_ATTR:
self.endpoint.electrical_measurement.voltage_reported(value / 10)
elif attrid == TUYA_DIN_SWITCH_ATTR:
self.endpoint.device.switch_bus.listener_event(
SWITCH_EVENT, self.endpoint.endpoint_id, value
)
class TuyaPowerMeasurement(LocalDataCluster, ElectricalMeasurement):
"""Custom class for power, voltage and current measurement."""
cluster_id = ElectricalMeasurement.cluster_id
POWER_ID = 0x050B
VOLTAGE_ID = 0x0505
CURRENT_ID = 0x0508
REACTIVE_POWER_ID = 0x050E
AC_FREQUENCY_ID = 0x0300
TOTAL_REACTIVE_POWER_ID = 0x0305
POWER_FACTOR_ID = 0x0510
AC_VOLTAGE_MULTIPLIER = 0x0600
AC_VOLTAGE_DIVISOR = 0x0601
AC_CURRENT_MULTIPLIER = 0x0602
AC_CURRENT_DIVISOR = 0x0603
AC_FREQUENCY_MULTIPLIER = 0x0400
AC_FREQUENCY_DIVISOR = 0x0401
_CONSTANT_ATTRIBUTES = {
AC_VOLTAGE_MULTIPLIER: 1,
AC_VOLTAGE_DIVISOR: 10,
AC_CURRENT_MULTIPLIER: 1,
AC_CURRENT_DIVISOR: 1000,
AC_FREQUENCY_MULTIPLIER: 1,
AC_FREQUENCY_DIVISOR: 100,
}
def voltage_reported(self, value):
"""Voltage reported."""
self._update_attribute(self.VOLTAGE_ID, value)
def power_reported(self, value):
"""Power reported."""
self._update_attribute(self.POWER_ID, value)
def power_factor_reported(self, value):
"""Power Factor reported."""
self._update_attribute(self.POWER_FACTOR_ID, value)
def reactive_power_reported(self, value):
"""Reactive Power reported."""
self._update_attribute(self.REACTIVE_POWER_ID, value)
def current_reported(self, value):
"""Ampers reported."""
self._update_attribute(self.CURRENT_ID, value)
def frequency_reported(self, value):
"""AC Frequency reported."""
self._update_attribute(self.AC_FREQUENCY_ID, value)
def reactive_energy_reported(self, value):
"""Summation Reactive Energy reported."""
self._update_attribute(self.TOTAL_REACTIVE_POWER_ID, value)
class TuyaElectricalMeasurement(LocalDataCluster, Metering):
"""Custom class for total energy measurement."""
cluster_id = Metering.cluster_id
CURRENT_DELIVERED_ID = 0x0000
CURRENT_RECEIVED_ID = 0x0001
#CURRENT_TIER1_DELIVERED_ID = 0x0100
POWER_WATT = 0x0000
"""Setting unit of measurement."""
_CONSTANT_ATTRIBUTES = {0x0300: POWER_WATT}
def energy_deliver_reported(self, value):
"""Summation Energy Deliver reported."""
self._update_attribute(self.CURRENT_DELIVERED_ID, value)
def energy_receive_reported(self, value):
"""Summation Energy Receive reported."""
self._update_attribute(self.CURRENT_RECEIVED_ID, value)
#def energy_tier1_deliver_reported(self, value):
# """Summation Energy Tier1 Receive reported."""
# self._update_attribute(self.CURRENT_TIER1_DELIVERED_ID, value)
class HikingManufClusterDinPower(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the Hiking Power Meter device."""
attributes = {
HIKING_DIN_SWITCH_ATTR: ("switch", t.uint8_t, True),
HIKING_TOTAL_ENERGY_DELIVERED_ATTR: ("energy_delivered", t.uint32_t, True),
HIKING_TOTAL_ENERGY_RECEIVED_ATTR: ("energy_received", t.uint16_t, True),
HIKING_VOLTAGE_CURRENT_ATTR: ("voltage_current", t.uint32_t, True),
HIKING_POWER_ATTR: ("power", t.uint16_t, True),
HIKING_FREQUENCY_ATTR: ("frequency", t.uint16_t, True),
HIKING_TOTAL_REACTIVE_ATTR: ("total_reactive_energy", t.int32s, True),
HIKING_REACTIVE_POWER_ATTR: ("reactive_power", t.int16s, True),
HIKING_POWER_FACTOR_ATTR: ("power_factor", t.uint16_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == HIKING_DIN_SWITCH_ATTR:
self.endpoint.device.switch_bus.listener_event(SWITCH_EVENT, 16, value)
elif attrid == HIKING_TOTAL_ENERGY_DELIVERED_ATTR:
self.endpoint.smartenergy_metering.energy_deliver_reported(value / 100)
elif attrid == HIKING_TOTAL_ENERGY_RECEIVED_ATTR:
self.endpoint.smartenergy_metering.energy_receive_reported(value / 100)
elif attrid == HIKING_VOLTAGE_CURRENT_ATTR:
self.endpoint.electrical_measurement.current_reported(value >> 16)
self.endpoint.electrical_measurement.voltage_reported(
(value & 0x0000FFFF) / 10
)
elif attrid == HIKING_POWER_ATTR:
self.endpoint.electrical_measurement.power_reported(value)
elif attrid == HIKING_FREQUENCY_ATTR:
self.endpoint.electrical_measurement.frequency_reported(value)
elif attrid == HIKING_TOTAL_REACTIVE_ATTR:
self.endpoint.electrical_measurement.reactive_energy_reported(value)
elif attrid == HIKING_REACTIVE_POWER_ATTR:
self.endpoint.electrical_measurement.reactive_power_reported(value)
elif attrid == HIKING_POWER_FACTOR_ATTR:
self.endpoint.electrical_measurement.power_factor_reported(value / 10)
class MatSeeManufClusterDinPower(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the Hiking Power Meter device."""
_LOGGER.debug("PRUEBA MatSeeManufClusterDinPower")
attributes = {
HIKING_DIN_SWITCH_ATTR: ("switch", t.uint8_t, True),
MATSEE_DPID_REVERSE_ENERGY_TOTAL_A_ATTR: ("energy_delivered", t.uint32_t, True),
MATSEE_DPID_FORWARD_ENERGY_TOTAL_A_ATTR: ("energy_received", t.uint32_t, True),
MATSEE_DPID_REVERSE_ENERGY_TOTAL_B_ATTR: ("energy_delivered_b", t.uint32_t, True),
MATSEE_DPID_FORWARD_ENERGY_TOTAL_B_ATTR: ("energy_received_b", t.uint32_t, True),
MATSEE_DPID_VOLTAGE_A_ATTR: ("voltage_current", t.uint32_t, True),
MATSEE_DPID_POWER_ID_A_ATTR: ("power", t.uint32_t, True),
MATSEE_DPID_POWER_ID_B_ATTR: ("power_b", t.uint32_t, True),
MATSEE_DPID_POWER_FREQ_ATTR: ("frequency", t.uint32_t, True),
MATSEE_DPID_POWER_TOTAL_ID_ATTR: ("total_reactive_energy", t.int32s, True),
MATSEE_DPID_POWER_A_COEF_ATTR: ("reactive_power", t.uint32_t, True),
MATSEE_DPID_POWER_B_COEF_ATTR: ("reactive_power_b", t.uint32_t, True),
MATSEE_DPID_POWER_FACTOR_A_ATTR: ("power_factor", t.uint32_t, True),
MATSEE_DPID_POWER_FACTOR_B_ATTR: ("power_factor_b", t.uint32_t, True),
MATSEE_DPID_CURRENT_A_ATTR: ("current", t.uint32_t, True),
MATSEE_DPID_CURRENT_B_ATTR: ("current_b", t.uint32_t, True),
MATSEE_DPID_POWER_DIRECTION_ID_A_ATTR: ("power_direction", t.uint8_t, True),
MATSEE_DPID_POWER_DIRECTION_ID_B_ATTR: ("power_direction_b", t.uint8_t, True),
MATSEE_DPID_UPDATE_RATE_ATTR: ("update_rate", t.uint32_t, True),
MATSEE_DPID_VOLTAGE_A_COEF_ATTR: ("voltage_coef", t.uint32_t, True),
MATSEE_DPID_CURRENT_A_COEF_ATTR: ("current_coef", t.uint32_t, True),
MATSEE_DPID_CURRENT_B_COEF_ATTR: ("current_coef_b", t.uint32_t, True),
MATSEE_DPID_ENERGY_A_COEF_ATTR: ("energy_coef", t.uint32_t, True),
MATSEE_DPID_ENERGY_B_COEF_ATTR: ("energy_coef_b", t.uint32_t, True),
MATSEE_DPID_ENERGY_A_COEF_REV_ATTR: ("energy_coef_rev", t.uint32_t, True),
MATSEE_DPID_ENERGY_B_COEF_REV_ATTR: ("energy_coef_rev_b", t.uint32_t, True),
MATSEE_DPID_FREQ_COEF_ATTR: ("frequency_coef", t.uint32_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == HIKING_DIN_SWITCH_ATTR:
self.endpoint.device.switch_bus.listener_event(SWITCH_EVENT, 16, value)
elif attrid == MATSEE_DPID_REVERSE_ENERGY_TOTAL_A_ATTR:
self.endpoint.smartenergy_metering.energy_deliver_reported(value / 100)
elif attrid == MATSEE_DPID_FORWARD_ENERGY_TOTAL_A_ATTR:
self.endpoint.smartenergy_metering.energy_receive_reported(value / 100)
#elif attrid == MATSEE_DPID_REVERSE_ENERGY_TOTAL_B_ATTR:
# self.endpoint.smartenergy_metering.energy_tier1_deliver_reported(value / 100)
elif attrid == MATSEE_DPID_CURRENT_A_ATTR:
self.endpoint.electrical_measurement.current_reported(value)
#elif attrid == MATSEE_DPID_CURRENT_B_ATTR:
# self.endpoint.electrical_measurement.current_reported((value & 0x0000FFFF)/1000)
elif attrid == MATSEE_DPID_VOLTAGE_A_ATTR:
#self.endpoint.electrical_measurement.current_reported(value >> 16)
#self.endpoint.electrical_measurement.current_reported((value & 0x0000FFFF) / 10)
self.endpoint.electrical_measurement.voltage_reported(
value
)
elif attrid == MATSEE_DPID_POWER_ID_A_ATTR:
self.endpoint.electrical_measurement.power_reported(value)
elif attrid == MATSEE_DPID_POWER_FREQ_ATTR:
self.endpoint.electrical_measurement.frequency_reported(value)
elif attrid == MATSEE_DPID_POWER_TOTAL_ID_ATTR:
self.endpoint.electrical_measurement.reactive_energy_reported(value)
elif attrid == MATSEE_DPID_POWER_A_COEF_ATTR:
self.endpoint.electrical_measurement.reactive_power_reported(value)
elif attrid == MATSEE_DPID_POWER_FACTOR_A_ATTR:
self.endpoint.electrical_measurement.power_factor_reported(value / 100)
class TuyaPowerMeter(TuyaSwitch):
"""Tuya power meter device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.switch_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
# "node_descriptor": "<NodeDescriptor byte1=1 byte2=64 mac_capability_flags=142 manufacturer_code=4098
# maximum_buffer_size=82 maximum_incoming_transfer_size=82 server_mask=11264
# maximum_outgoing_transfer_size=82 descriptor_capability_field=0>",
# device_version=1
# input_clusters=[0x0000, 0x0004, 0x0005, 0xef00]
# output_clusters=[0x000a, 0x0019]
MODELS_INFO: [
("_TZE200_byzdayie", "TS0601"),
("_TZE200_ewxhg6o9", "TS0601"),
("_TZE204_81yrt3lo", "TS0601"),
],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=51
# device_version=1
# input_clusters=[0, 4, 5, 61184]
# output_clusters=[10, 25]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterAttributes.cluster_id,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterDinPower,
TuyaPowerMeasurement,
TuyaElectricalMeasurement,
TuyaOnOff,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
}
}
class HikingPowerMeter(TuyaSwitch):
"""Hiking Power Meter Device - DDS238-2."""
signature = {
# "node_descriptor": "<NodeDescriptor byte1=1 byte2=64 mac_capability_flags=142 manufacturer_code=4098
# maximum_buffer_size=82 maximum_incoming_transfer_size=82 server_mask=11264
# maximum_outgoing_transfer_size=82 descriptor_capability_field=0>",
# device_version=1
# input_clusters=[0x0000, 0x0004, 0x0005, 0xef00]
# output_clusters=[0x000a, 0x0019]
MODELS_INFO: [("_TZE200_bkkmqmyo", "TS0601")],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=51
# device_version=1
# input_clusters=[0, 4, 5, 61184]
# output_clusters=[10, 25]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterAttributes.cluster_id,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
HikingManufClusterDinPower,
TuyaElectricalMeasurement,
TuyaPowerMeasurement,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
},
16: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
TuyaOnOff,
],
OUTPUT_CLUSTERS: [],
},
}
}
class TuyaPowerMeter_GPP(TuyaSwitch):
"""Tuya power meter device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.switch_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
# "node_descriptor": "<NodeDescriptor byte1=1 byte2=64 mac_capability_flags=142 manufacturer_code=4098
# maximum_buffer_size=82 maximum_incoming_transfer_size=82 server_mask=11264
# maximum_outgoing_transfer_size=82 descriptor_capability_field=0>",
# device_version=1
# input_clusters=[0x0000, 0x0004, 0x0005, 0xef00]
# output_clusters=[0x000a, 0x0019]
MODELS_INFO: [
("_TZE200_lsanae15", "TS0601"),
("_TZE204_81yrt3lo", "TS0601"),
],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=51
# device_version=1
# input_clusters=[0, 4, 5, 61184]
# output_clusters=[10, 25]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterAttributes.cluster_id,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
},
242: {
# <SimpleDescriptor endpoint=242 profile=41440 device_type=97
# input_clusters=[]
# output_clusters=[33]
PROFILE_ID: 41440,
DEVICE_TYPE: 97,
INPUT_CLUSTERS: [],
OUTPUT_CLUSTERS: [GreenPowerProxy.cluster_id],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
MatSeeManufClusterDinPower,
TuyaPowerMeasurement,
TuyaElectricalMeasurement,
TuyaOnOff,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
},
242: {
# <SimpleDescriptor endpoint=242 profile=41440 device_type=97
# input_clusters=[]
# output_clusters=[33]
PROFILE_ID: 41440,
DEVICE_TYPE: 97,
INPUT_CLUSTERS: [],
OUTPUT_CLUSTERS: [GreenPowerProxy.cluster_id],
},
}
}

View File

@@ -0,0 +1,293 @@
"""Tuya Din Power Meter."""
from zigpy.profiles import zha
import zigpy.types as t
from zigpy.zcl.clusters.general import Basic, Groups, Ota, Scenes, Time
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
from zigpy.zcl.clusters.smartenergy import Metering
from zhaquirks import Bus, LocalDataCluster
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.tuya import TuyaManufClusterAttributes, TuyaOnOff, TuyaSwitch
TUYA_TOTAL_ENERGY_ATTR = 0x0211
TUYA_CURRENT_ATTR = 0x0212
TUYA_POWER_ATTR = 0x0213
TUYA_VOLTAGE_ATTR = 0x0214
TUYA_DIN_SWITCH_ATTR = 0x0101
SWITCH_EVENT = "switch_event"
"""Hiking Power Meter Attributes"""
HIKING_DIN_SWITCH_ATTR = 0x0110
HIKING_TOTAL_ENERGY_DELIVERED_ATTR = 0x0201
HIKING_TOTAL_ENERGY_RECEIVED_ATTR = 0x0266
HIKING_VOLTAGE_CURRENT_ATTR = 0x0006
HIKING_POWER_ATTR = 0x0267
HIKING_FREQUENCY_ATTR = 0x0269
HIKING_POWER_FACTOR_ATTR = 0x026F
HIKING_TOTAL_REACTIVE_ATTR = 0x026D
HIKING_REACTIVE_POWER_ATTR = 0x026E
class TuyaManufClusterDinPower(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the Tuya Power Meter device."""
attributes = {
TUYA_TOTAL_ENERGY_ATTR: ("energy", t.uint32_t, True),
TUYA_CURRENT_ATTR: ("current", t.int16s, True),
TUYA_POWER_ATTR: ("power", t.uint16_t, True),
TUYA_VOLTAGE_ATTR: ("voltage", t.uint16_t, True),
TUYA_DIN_SWITCH_ATTR: ("switch", t.uint8_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == TUYA_TOTAL_ENERGY_ATTR:
self.endpoint.smartenergy_metering.energy_deliver_reported(value / 100)
elif attrid == TUYA_CURRENT_ATTR:
self.endpoint.electrical_measurement.current_reported(value)
elif attrid == TUYA_POWER_ATTR:
self.endpoint.electrical_measurement.power_reported(value / 10)
elif attrid == TUYA_VOLTAGE_ATTR:
self.endpoint.electrical_measurement.voltage_reported(value / 10)
elif attrid == TUYA_DIN_SWITCH_ATTR:
self.endpoint.device.switch_bus.listener_event(
SWITCH_EVENT, self.endpoint.endpoint_id, value
)
class TuyaPowerMeasurement(LocalDataCluster, ElectricalMeasurement):
"""Custom class for power, voltage and current measurement."""
POWER_ID = 0x050B
VOLTAGE_ID = 0x0505
CURRENT_ID = 0x0508
REACTIVE_POWER_ID = 0x050E
AC_FREQUENCY_ID = 0x0300
TOTAL_REACTIVE_POWER_ID = 0x0305
POWER_FACTOR_ID = 0x0510
AC_CURRENT_MULTIPLIER = 0x0602
AC_CURRENT_DIVISOR = 0x0603
AC_FREQUENCY_MULTIPLIER = 0x0400
AC_FREQUENCY_DIVISOR = 0x0401
_CONSTANT_ATTRIBUTES = {
AC_CURRENT_MULTIPLIER: 1,
AC_CURRENT_DIVISOR: 1000,
AC_FREQUENCY_MULTIPLIER: 1,
AC_FREQUENCY_DIVISOR: 100,
}
def voltage_reported(self, value):
"""Voltage reported."""
self._update_attribute(self.VOLTAGE_ID, value)
def power_reported(self, value):
"""Power reported."""
self._update_attribute(self.POWER_ID, value)
def power_factor_reported(self, value):
"""Power Factor reported."""
self._update_attribute(self.POWER_FACTOR_ID, value)
def reactive_power_reported(self, value):
"""Reactive Power reported."""
self._update_attribute(self.REACTIVE_POWER_ID, value)
def current_reported(self, value):
"""Ampers reported."""
self._update_attribute(self.CURRENT_ID, value)
def frequency_reported(self, value):
"""AC Frequency reported."""
self._update_attribute(self.AC_FREQUENCY_ID, value)
def reactive_energy_reported(self, value):
"""Summation Reactive Energy reported."""
self._update_attribute(self.TOTAL_REACTIVE_POWER_ID, value)
class TuyaElectricalMeasurement(LocalDataCluster, Metering):
"""Custom class for total energy measurement."""
CURRENT_DELIVERED_ID = 0x0000
CURRENT_RECEIVED_ID = 0x0001
POWER_WATT = 0x0000
"""Setting unit of measurement."""
_CONSTANT_ATTRIBUTES = {0x0300: POWER_WATT}
def energy_deliver_reported(self, value):
"""Summation Energy Deliver reported."""
self._update_attribute(self.CURRENT_DELIVERED_ID, value)
def energy_receive_reported(self, value):
"""Summation Energy Receive reported."""
self._update_attribute(self.CURRENT_RECEIVED_ID, value)
class HikingManufClusterDinPower(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the Hiking Power Meter device."""
attributes = {
HIKING_DIN_SWITCH_ATTR: ("switch", t.uint8_t, True),
HIKING_TOTAL_ENERGY_DELIVERED_ATTR: ("energy_delivered", t.uint32_t, True),
HIKING_TOTAL_ENERGY_RECEIVED_ATTR: ("energy_received", t.uint16_t, True),
HIKING_VOLTAGE_CURRENT_ATTR: ("voltage_current", t.uint32_t, True),
HIKING_POWER_ATTR: ("power", t.uint16_t, True),
HIKING_FREQUENCY_ATTR: ("frequency", t.uint16_t, True),
HIKING_TOTAL_REACTIVE_ATTR: ("total_reactive_energy", t.int32s, True),
HIKING_REACTIVE_POWER_ATTR: ("reactive_power", t.int16s, True),
HIKING_POWER_FACTOR_ATTR: ("power_factor", t.uint16_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == HIKING_DIN_SWITCH_ATTR:
self.endpoint.device.switch_bus.listener_event(SWITCH_EVENT, 16, value)
elif attrid == HIKING_TOTAL_ENERGY_DELIVERED_ATTR:
self.endpoint.smartenergy_metering.energy_deliver_reported(value / 100)
elif attrid == HIKING_TOTAL_ENERGY_RECEIVED_ATTR:
self.endpoint.smartenergy_metering.energy_receive_reported(value / 100)
elif attrid == HIKING_VOLTAGE_CURRENT_ATTR:
self.endpoint.electrical_measurement.current_reported(value >> 16)
self.endpoint.electrical_measurement.voltage_reported(
(value & 0x0000FFFF) / 10
)
elif attrid == HIKING_POWER_ATTR:
self.endpoint.electrical_measurement.power_reported(value)
elif attrid == HIKING_FREQUENCY_ATTR:
self.endpoint.electrical_measurement.frequency_reported(value)
elif attrid == HIKING_TOTAL_REACTIVE_ATTR:
self.endpoint.electrical_measurement.reactive_energy_reported(value)
elif attrid == HIKING_REACTIVE_POWER_ATTR:
self.endpoint.electrical_measurement.reactive_power_reported(value)
elif attrid == HIKING_POWER_FACTOR_ATTR:
self.endpoint.electrical_measurement.power_factor_reported(value / 10)
class TuyaPowerMeter(TuyaSwitch):
"""Tuya power meter device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.switch_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
# "node_descriptor": "<NodeDescriptor byte1=1 byte2=64 mac_capability_flags=142 manufacturer_code=4098
# maximum_buffer_size=82 maximum_incoming_transfer_size=82 server_mask=11264
# maximum_outgoing_transfer_size=82 descriptor_capability_field=0>",
# device_version=1
# input_clusters=[0x0000, 0x0004, 0x0005, 0xef00]
# output_clusters=[0x000a, 0x0019]
MODELS_INFO: [
("_TZE200_byzdayie", "TS0601"),
("_TZE200_ewxhg6o9", "TS0601"),
],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=51
# device_version=1
# input_clusters=[0, 4, 5, 61184]
# output_clusters=[10, 25]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterAttributes.cluster_id,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterDinPower,
TuyaPowerMeasurement,
TuyaElectricalMeasurement,
TuyaOnOff,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
}
}
class HikingPowerMeter(TuyaSwitch):
"""Hiking Power Meter Device - DDS238-2."""
signature = {
# "node_descriptor": "<NodeDescriptor byte1=1 byte2=64 mac_capability_flags=142 manufacturer_code=4098
# maximum_buffer_size=82 maximum_incoming_transfer_size=82 server_mask=11264
# maximum_outgoing_transfer_size=82 descriptor_capability_field=0>",
# device_version=1
# input_clusters=[0x0000, 0x0004, 0x0005, 0xef00]
# output_clusters=[0x000a, 0x0019]
MODELS_INFO: [
("_TZE200_bkkmqmyo", "TS0601"),
("_TZE204_81yrt3lo", "TS0601"),
],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=51
# device_version=1
# input_clusters=[0, 4, 5, 61184]
# output_clusters=[10, 25]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufClusterAttributes.cluster_id,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
HikingManufClusterDinPower,
TuyaElectricalMeasurement,
TuyaPowerMeasurement,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
},
16: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
TuyaOnOff,
],
OUTPUT_CLUSTERS: [],
},
}
}

View File

@@ -0,0 +1,175 @@
"""
DY-YG400A Smoke Sensor, TS0601 from _TZE204_ntcy3xu1
https://www.aliexpress.com/item/1005005863519099.html
https://www.aliexpress.com/item/1005005854203557.html
"""
import logging
import zigpy.profiles.zha
from zigpy.quirks import CustomDevice
import zigpy.types as t
from zigpy.zcl.clusters.general import (
Basic,
Groups,
Ota,
Scenes,
Time,
BinaryInput,
BatterySize,
)
from zigpy.zcl.clusters.security import IasZone
from zhaquirks import Bus
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
ZONE_TYPE,
)
from zhaquirks.tuya import (
TuyaLocalCluster,
TuyaManufCluster,
TuyaManufClusterAttributes,
TuyaPowerConfigurationCluster,
)
_LOGGER = logging.getLogger(__name__)
TUYA_SMOKE_DETECTED_ATTR = 0x0401 # [0]/[1] [Detected]/[Clear]!
TUYA_SMOKE_TAMPERED_ATTR = 0x0104 # [0]/[1] [Clear]/[Tampered]!
TUYA_SMOKE_BATTERY_ATTR = 0x040e # [0]/[1]/[2] [Low]/[Med]]/[Full]!
class TuyaSmokeDetectorCluster(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the TS0601 smoke detector."""
attributes = {
TUYA_SMOKE_DETECTED_ATTR: ("smoke_detected", t.uint8_t, True),
TUYA_SMOKE_TAMPERED_ATTR: ("tampered_device", t.uint8_t, True),
TUYA_SMOKE_BATTERY_ATTR: ("battery_status", t.uint8_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == TUYA_SMOKE_DETECTED_ATTR:
status = IasZone.ZoneStatus.Alarm_1 if value == 0 else 0
self.endpoint.device.ias_bus.listener_event("update_attribute", "zone_status", status)
elif attrid == TUYA_SMOKE_TAMPERED_ATTR:
self.endpoint.device.tamper_detection_bus.listener_event(
"update_attribute", "present_value", bool(value)
)
elif attrid == TUYA_SMOKE_BATTERY_ATTR:
batt = 5 if value == 0 else 40 if value == 1 else 100
self.endpoint.device.battery_bus.listener_event("battery_change", batt)
else:
_LOGGER.warning(
"[0x%04x:%s:0x%04x] unhandled attribute: 0x%04x",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
attrid,
)
class TuyaIasZone(TuyaLocalCluster, IasZone):
"""
IAS Zone: this generates the "Smoke" entity for HA.
Receives updates from TuyaSmokeDetectorCluster.
"""
_CONSTANT_ATTRIBUTES = {ZONE_TYPE: IasZone.ZoneType.Fire_Sensor}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.ias_bus.add_listener(self)
class TuyaPowerConfigCluster(TuyaPowerConfigurationCluster):
"""
Power Config Cluster: this generates the "Battery" entity for HA.
Receives updates from TuyaSmokeDetectorCluster.
"""
_CONSTANT_ATTRIBUTES = {
TuyaPowerConfigurationCluster.attributes_by_name["battery_size"].id: BatterySize.AAA,
TuyaPowerConfigurationCluster.attributes_by_name["battery_quantity"].id: 2,
TuyaPowerConfigurationCluster.attributes_by_name["battery_rated_voltage"].id: 15,
}
class TuyaTamperDetection(TuyaLocalCluster, BinaryInput):
"""
Tamper Detection Cluster: this generates the "Binary input" entity for HA, which is updated
with the Tampered state. Receives updates from TuyaSmokeDetectorCluster.
"""
_CONSTANT_ATTRIBUTES = {
BinaryInput.attributes_by_name["description"].id: "Tamper Detected",
BinaryInput.attributes_by_name["active_text"].id: "Tampered",
BinaryInput.attributes_by_name["inactive_text"].id: "Clear",
}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.tamper_detection_bus.add_listener(self)
class TuyaSmokeDetector0601(CustomDevice):
"""TS0601 Smoke detector quirk."""
def __init__(self, *args, **kwargs):
"""Init."""
self.ias_bus = Bus()
self.battery_bus = Bus()
self.tamper_detection_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
MODELS_INFO: [
"""("_TZE200_uebojraa", "TS0601"),"""
("_TZE200_uebojraa"),
],
ENDPOINTS: {
1: {
PROFILE_ID: zigpy.profiles.zha.PROFILE_ID,
DEVICE_TYPE: zigpy.profiles.zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufCluster.cluster_id,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zigpy.profiles.zha.PROFILE_ID,
DEVICE_TYPE: zigpy.profiles.zha.DeviceType.IAS_ZONE,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaIasZone,
TuyaSmokeDetectorCluster,
TuyaPowerConfigCluster,
TuyaTamperDetection,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}

View File

@@ -0,0 +1,120 @@
"""Smoke Sensor."""
import logging
import zigpy.profiles.zha
from zigpy.quirks import CustomCluster, CustomDevice
import zigpy.types as t
from zigpy.zcl.clusters.general import Basic, Groups, Ota, Scenes, Time
from zigpy.zcl.clusters.security import IasZone
from zhaquirks import Bus
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
ZONE_STATUS,
ZONE_TYPE,
)
from zhaquirks.tuya import TuyaManufCluster, TuyaManufClusterAttributes
_LOGGER = logging.getLogger(__name__)
TUYA_SMOKE_DETECTED_ATTR = 0x0401 # [0]/[1] [Detected]/[Clear]!
class TuyaSmokeDetectorCluster(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the TS0601 smoke detector."""
attributes = {
TUYA_SMOKE_DETECTED_ATTR: ("smoke_detected", t.uint8_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == TUYA_SMOKE_DETECTED_ATTR:
if value == 0:
self.endpoint.device.ias_bus.listener_event(
"update_zone_status", IasZone.ZoneStatus.Alarm_1
)
else:
self.endpoint.device.ias_bus.listener_event("update_zone_status", 0)
else:
_LOGGER.warning(
"[0x%04x:%s:0x%04x] unhandled attribute: 0x%04x",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
attrid,
)
class TuyaIasZone(CustomCluster, IasZone):
"""IAS Zone."""
_CONSTANT_ATTRIBUTES = {ZONE_TYPE: IasZone.ZoneType.Fire_Sensor}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.ias_bus.add_listener(self)
def update_zone_status(self, value):
"""Update IAS status."""
super()._update_attribute(ZONE_STATUS, value)
class TuyaSmokeDetector0601(CustomDevice):
"""TS0601 Smoke detector quirk."""
def __init__(self, *args, **kwargs):
"""Init."""
self.ias_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
MODELS_INFO: [
("_TZE200_aycxwiau", "TS0601"),
("_TZE200_ntcy3xu1", "TS0601"),
("_TZE200_vzekyi4c", "TS0601"),
("_TZE200_uebojraa", "TS0601"),
],
ENDPOINTS: {
1: {
PROFILE_ID: zigpy.profiles.zha.PROFILE_ID,
DEVICE_TYPE: zigpy.profiles.zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufCluster.cluster_id,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zigpy.profiles.zha.PROFILE_ID,
DEVICE_TYPE: zigpy.profiles.zha.DeviceType.IAS_ZONE,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaIasZone,
TuyaSmokeDetectorCluster,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}

View File

@@ -0,0 +1,151 @@
"""Tuya valve devices."""
import logging
from typing import Dict
from zigpy.profiles import zha
from zigpy.quirks import CustomDevice
import zigpy.types as t
from zigpy.zcl import foundation
from zigpy.zcl.clusters.general import (
Basic,
Groups,
Ota,
PowerConfiguration,
Scenes,
Time,
)
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.tuya import NoManufacturerCluster
from zhaquirks.tuya.mcu import (
DPToAttributeMapping,
TuyaAttributesCluster,
TuyaDPType,
TuyaLevelControl,
TuyaMCUCluster,
TuyaOnOff,
TuyaOnOffNM,
TuyaPowerConfigurationCluster,
)
_LOGGER = logging.getLogger(__name__)
class TuyaLevelControlNM(NoManufacturerCluster, TuyaLevelControl):
"""Tuya LevelControl cluster with NoManufacturerID."""
class TuyaValveManufCluster(TuyaMCUCluster):
"""Tuya valve manufacturer cluster."""
attributes = TuyaMCUCluster.attributes.copy()
attributes.update(
{
0xEF03: ("dp_3", t.uint32_t, True),
0xEF65: ("dp_101", t.uint32_t, True),
0xEF66: ("dp_102", t.uint32_t, True), # <-- new cluster attribute with fake IDs (102=0x66).Format= ID: ("name", type, True)
}
)
dp_to_attribute: Dict[int, DPToAttributeMapping] = {
2: DPToAttributeMapping(
TuyaOnOffNM.ep_attribute,
"on_off",
dp_type=TuyaDPType.BOOL,
),
1: DPToAttributeMapping(
TuyaLevelControlNM.ep_attribute,
"current_level",
dp_type=TuyaDPType.VALUE,
converter=lambda x: (x * 255) // 100,
dp_converter=lambda x: (x * 100) // 255,
),
3: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"dp_3",
dp_type=TuyaDPType.VALUE,
),
101: DPToAttributeMapping(
TuyaMCUCluster.ep_attribute,
"dp_101",
dp_type=TuyaDPType.VALUE,
),
102: DPToAttributeMapping( # <-- device DP102
TuyaMCUCluster.ep_attribute, # <-- reference to the cluster which has the attribute
"dp_102", # <-- attribute "name"
dp_type=TuyaDPType.VALUE, # <-- DP Type it is related to the attribute type
),
108: DPToAttributeMapping(
TuyaPowerConfigurationCluster.ep_attribute,
"battery_percentage_remaining",
dp_type=TuyaDPType.VALUE,
),
}
data_point_handlers = {
2: "_dp_2_attr_update",
1: "_dp_2_attr_update",
3: "_dp_2_attr_update",
101: "_dp_2_attr_update",
102: "_dp_2_attr_update",
108: "_dp_2_attr_update",
}
async def write_attributes(self, attributes, manufacturer=None):
"""Overwrite to force manufacturer code."""
return await super().write_attributes(
attributes, manufacturer=foundation.ZCLHeader.NO_MANUFACTURER_ID
)
class TuyaValve(CustomDevice):
"""Tuya valve device."""
signature = {
"""MODELS_INFO: [("_TZE200_sh1btabb", "TS0601")],"""
MODELS_INFO: [("_TZE200_sh1btabb")],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=0x0051
# input_clusters=[0x0000, 0x0004, 0x0005, 0xef00]
# output_clusters=[0x000a, 0x0019]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaValveManufCluster.cluster_id,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
},
}
replacement = {
ENDPOINTS: {
1: {
DEVICE_TYPE: zha.DeviceType.ON_OFF_LIGHT,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaValveManufCluster,
TuyaOnOffNM,
TuyaLevelControlNM,
TuyaPowerConfigurationCluster,
],
OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
}
}
}

View File

@@ -0,0 +1,170 @@
##########################################################################################
##########################################################################################
# MAIN DISHWASHER POWER
# Controlled by a Athom Smart Plug V1
# package_import_url: github://athom-tech/athom-configs/athom-smart-plug.yaml
#
# V1.1 2025-06-12 package added for energy entities
# V1.0 2025-06-10 YAML Tidyups
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-maindishwasherspower"
friendly_name: "Main Dishwashers Power"
description_comment: "Main Dishwashers Power Monitor :: Athom Smart Plug Power Monitor V1"
device_area: "Kitchen" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V1" # Project Details
project_version: "v1.1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-maindishwasherspower_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:dishwasher"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
# Device Specific included packages
common_athompowermonV1: !include
file: common/athompowermonv1_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_current_limit: "${current_limit}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: "relay"
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp8285
restore_from_flash: True # mainly for calculating cumulative energy, but not that important here
preferences:
flash_write_interval: 5min
mdns:
disabled: false
#dashboard_import:
# package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml
##########################################################################################
# ESPHome LOGGING
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
status_led:
pin:
number: GPIO13
inverted: True
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: 3
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- switch.toggle:
id: relay
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
switch:
- platform: gpio
name: "Power Output"
pin: GPIO14
id: relay
restore_mode: RESTORE_DEFAULT_ON # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"

View File

@@ -0,0 +1,188 @@
#############################################
#############################################
# OFFICE MULTI USB CONTROL
# Controlled by a Sonoff Basic R1
#
# V1.0 2025-06-18 Initial Version
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-officemultiusb"
friendly_name: "Office Multi USB Control"
mqtt_device_name: "office-multiusb-control"
description_comment: "Multi USB control in the Office (behind monitors) :: Sonoff Basic"
device_area: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-officemultiusb_ip
mqtt_main_command_topic: !secret mqtt_main_command_topic
# Device Settings
relay_icon: "mdi:generator-portable"
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT Controls
mqtt_main_topic: "${mqtt_main_command_topic}/${mqtt_device_name}/operation" # Topic we will use to command stuff from external withougt HA
#mqtt_status_topic: "${mqtt_main_status_topic}/esphome/${device_name}" # Topic we will use to read stuff from external withougt HA
#mqtt_main_status_topic: !secret mqtt_main_status_topic
#mqtt_remote_device1_command_topic: "${mqtt_main_command_topic}/masterbath-towelrail/operation"
#mqtt_remote_device1_command1: "BOOST"
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/esphome.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
# platformio_options:
# build_flags:
# - "-Os" # optimize for size
# - "-Wl,--gc-sections" # drop unused code/data
# - "-fno-exceptions" # strip C++ exceptions
# - "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: False
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff Basic R1 Relay Switch is GPIO12
#############################################
switch:
- platform: gpio
name: "ELV Power Supply"
pin: GPIO12
id: relay1
restore_mode: RESTORE_DEFAULT_ON
icon: "${relay_icon}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Sonoff Basic R1 Button is GPIO03
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO03
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay1).state) {
// Relay is ON: turn it OFF
id(relay1).turn_off();
} else {
// Relay is OFF: turn it ON
id(relay1).turn_on();
}
# Mimics actual relay status (but not controllable)
- platform: template
name: "Relay Status"
lambda: |-
return id(relay1).state;
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Sonoff Basic R1 LED is GPIO13
#############################################
status_led:
pin:
number: GPIO13
inverted: yes
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Relay 1 control
- topic: "${mqtt_main_topic}/relay1/set"
payload: "ON"
then:
- switch.turn_on: relay1
- topic: "${mqtt_main_topic}/relay1/set"
payload: "OFF"
then:
- switch.turn_off: relay1

View File

@@ -0,0 +1,702 @@
##########################################################################################
##########################################################################################
# POOL LIGHTS POWER AND TIMER
# Controlled by a Athom Smart Plug V3
#
# dashboard_import:
# package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml
#
# V2.3 2025-06-15 Changed to an Athom V3 (esp32)
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
#
# INSTRUCTIONS
# - It allows the device to work in a standalone timer style operation
# - The timer has a morning and evening time (but no weekday/weekend settings)
# - Default values are set, but changed values are remembered in flash
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
# - If on a network and there is a MQTT server, you can set the on/off times via MQTT (See below commands)
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
# - Any new timer times set via MQTT will be remembered though a reboot
# - On startup, or a reboot, the device will always turn on for the BOOST Duration (BOOST mode, default 2 hours)
# - TIMER mode will always be switched on after BOOST mode is complete
# - Home Assistant entities are set so that BOOST mode can be pressed with a button and other modes selectable with a dropdown
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. (Note there is no weekday/weekend setting)
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
# mqtt_timer_topic/operation/ON : Device permanently on
# mqtt_timer_topic/operation/OFF : Device permanently off
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
#
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-poollightspower"
friendly_name: "Pool Lights Power"
description_comment: "Pool Lights Power :: Athom Smart Plug Power V3"
device_area: "Outside" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V3" # Project Details
project_version: "v2.3" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-poollightspower_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-poollightspower_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-poollightspower_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "10s" # update time for for general sensors etc
# Device and Timer Settings
relay_icon: "mdi:light-flood-up"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
mqtt_timer_topic: "viewroad-commands/poollights-timer" # Topics you will use to change stuff
boost_duration_default: "180" # Minutes to stay ON in BOOST mode before reverting to TIMER
morning_on_default: "450" # Default in minutes from midnight. Default 07:30 => 450
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450 (same as ON as no need for morning schedule)
evening_on_default: "1140" # Default in minutes from midnight. Default 19:00 => 1140
evening_off_default: "1350" # Default in minutes from midnight. Default 22:30 => 1350 => 1440 is midnight
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
common_webportal: !include
file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
# Device Specific included packages
common_athompowermonV3: !include
file: common/athompowermonv3_common.yaml
vars:
local_current_limit: "${current_limit}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
name: "${project_name}"
version: "${project_version}"
platformio_options:
board_build.mcu: esp32c3
board_build.variant: esp32c3
board_build.flash_mode: dio
on_boot:
then:
- script.execute: evaluate_relay_state
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp32:
board: esp32-c3-devkitm-1
flash_size: 4MB
variant: ESP32C3
framework:
type: esp-idf # "esp-idf" OR "arduino". Suggested ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended # recommended, latest or dev
preferences:
flash_write_interval: 5min
esp32_improv:
authorizer: none
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
# Tracks the time (in seconds from midnight) at the previous boot
- id: last_boot_time_s
type: int
restore_value: true
initial_value: "0"
# Counts how many consecutive boots have occurred (within X seconds)
- id: boot_count
type: int
restore_value: true
initial_value: "0"
# Morning On time (minutes from midnight),
- id: morning_on
type: int
restore_value: true
initial_value: "${morning_on_default}"
# Morning Off time (minutes from midnight),
- id: morning_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening On time (minutes from midnight),
- id: evening_on
type: int
restore_value: true
initial_value: "${evening_on_default}"
# Evening Off time (minutes from midnight),
- id: evening_off
type: int
restore_value: true
initial_value: "${evening_off_default}"
# Boost Duration (minutes),
- id: boost_duration
type: int
restore_value: true
initial_value: "${boost_duration_default}"
####################################################
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
####################################################
- id: operation_mode
type: int
restore_value: true
initial_value: "2"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
####################################################
- id: current_mins
type: int
restore_value: true
initial_value: "720" # 720 is 12:00 Noon
####################################################
# boost_timer: counts minutes in BOOST mode
# After 'boost_duration' minutes, revert to TIMER.
####################################################
- id: boost_timer
type: int
restore_value: true
initial_value: "0"
##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
############################
# MQTT Subscriptions
############################
####################################################
# Subscribe to the Morning On time, format "HH:MM"
# We check x.size() == 5 and x[2] == ':',
# then parse x.substr(0,2) and x.substr(3,2)
# std::string uses 'substr', not 'substring'.
####################################################
- platform: mqtt_subscribe
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
internal: True
on_value:
then:
- lambda: |-
// Expect "HH:MM" => total length = 5, with ':'
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str()); // "HH"
int minute = atoi(x.substr(3, 2).c_str()); // "MM"
id(morning_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
####################################################
# Morning Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(morning_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
####################################################
# Evening On time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
####################################################
# Evening Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
####################################################
# Boost duration => 1 - 1439
####################################################
- platform: mqtt_subscribe
name: "Boost Duration"
id: boost_time_topic
topic: "${mqtt_timer_topic}/boost-time" # Stored as an integer from 1-1439
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
// parse as integer
char *endptr;
long v = strtol(x.c_str(), &endptr, 10);
// invalid if nothing parsed, trailing chars, or out of 01439
if (endptr == x.c_str() || *endptr != '\0' || v < 0 || v > 1439) {
ESP_LOGE("boost_time", "Invalid boost_time '%s'", x.c_str());
} else {
id(boost_duration) = static_cast<int>(v);
}
####################################################
# Subscribe to operation mode:
# OFF, ON, TIMER, BOOST
# We do case-insensitive compare using strcasecmp
# (Requires <strings.h> typically included in ESPHome)
####################################################
# MQTT subscription: set mode, then immediately re-evaluate relay
- platform: mqtt_subscribe
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation"
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (strcasecmp(x.c_str(), "TIMER") == 0) {
id(operation_mode) = 2;
ESP_LOGI("timer","Operation mode set to TIMER");
} else if (strcasecmp(x.c_str(), "ON") == 0) {
id(operation_mode) = 1;
ESP_LOGI("timer","Operation mode set to ON");
} else if (strcasecmp(x.c_str(), "OFF") == 0) {
id(operation_mode) = 0;
ESP_LOGI("timer","Operation mode set to OFF");
} else if (strcasecmp(x.c_str(), "BOOST") == 0) {
id(operation_mode) = 3;
id(boost_timer) = 0;
ESP_LOGI("timer","Operation mode set to BOOST");
} else {
ESP_LOGW("timer","Invalid operation mode: %s", x.c_str());
}
- script.execute: evaluate_relay_state
######################################################
# Expose the current operation mode (OFF, ON, TIMER, BOOST)
######################################################
- platform: template
name: "Operation Mode State"
lambda: |-
// 0=OFF, 1=ON, 2=TIMER, 3=BOOST
switch (id(operation_mode)) {
case 0: return {"OFF"};
case 1: return {"ON"};
case 2: return {"TIMER"};
case 3: return {"BOOST"};
default: return {"UNKNOWN"};
}
update_interval: 5s
######################################################
# Expose the "Morning On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning On Time"
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Morning Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning Off Time"
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening On Time"
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening Off Time"
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay).state) {
// Relay is ON: turn it OFF and set mode to 0 (TIMER)
id(relay).turn_off();
id(operation_mode) = 2;
} else {
// Relay is OFF: turn it ON and set mode to 3 (BOOST)
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
##########################################################################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
sensor:
- platform: template
name: "Timeclock: Boost Duration"
id: boost_duration_time
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
internal: True # No need to show this in Home Assistant
lambda: |-
return id(current_mins);
# A value in mins if a timer is running showing how many mins left
- platform: template
name: "Timer Minutes Remaining"
id: timer_minutes_remaining
unit_of_measurement: "Mins"
update_interval: 5s
accuracy_decimals: "0"
lambda: |-
// always zero if relay is off
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (BOOST)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
rem = id(boost_duration) - id(boost_timer);
}
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
switch:
- platform: gpio
name: "Power Output"
pin: GPIO5
id: relay
restore_mode: RESTORE_DEFAULT_OFF # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
- platform: template
name: "Boost now"
id: boost_button
icon: "mdi:play-circle-outline"
on_press:
# 1) reset BOOST timer and set mode
- lambda: |-
id(boost_timer) = 0;
id(operation_mode) = 3;
# 2) immediately re-evaluate relay state
- script.execute: evaluate_relay_state
- platform: template
name: "Default timer settings"
id: default_timer_settings_button
icon: "mdi:restore"
on_press:
- lambda: |-
// Restore all timing globals to their YAML defaults
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration)= ${boost_duration_default};
// Reset mode to TIMER and clear any running boost
id(operation_mode)= 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
- script.execute: evaluate_relay_state
#################################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#################################################################################################
select:
- platform: template
name: "Operation Mode"
id: operation_mode_select
update_interval: 5s
options:
- "OFF"
- "ON"
- "TIMER"
- "BOOST"
# show the current mode
lambda: |-
switch (id(operation_mode)) {
case 1: return std::string("ON");
case 2: return std::string("TIMER");
case 3: return std::string("BOOST");
default: return std::string("OFF");
}
# when changed in HA, set mode & re-evaluate
set_action:
- lambda: |-
if (x == "OFF") { id(operation_mode) = 0; }
else if (x == "ON") { id(operation_mode) = 1; }
else if (x == "TIMER") { id(operation_mode) = 2; }
else { // BOOST
id(boost_timer) = 0;
id(operation_mode) = 3;
}
- script.execute: evaluate_relay_state
#################################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
#################################################################################################
# Script: evaluate and drive the relay
script:
- id: evaluate_relay_state
then:
- lambda: |-
int mode = id(operation_mode);
// BOOST just forces the relay on
if (mode == 3) {
id(relay).turn_on();
return;
}
// OFF → always off
if (mode == 0) {
id(relay).turn_off();
return;
}
// ON → always on
if (mode == 1) {
id(relay).turn_on();
return;
}
// TIMER → follow schedule windows
{
bool should_on = false;
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off))
should_on = true;
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off))
should_on = true;
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
#################################################################################################
# INTERVAL COMPONENT
# https://esphome.io/components/interval.html
#################################################################################################
# Interval: bumps time (even if no SNTP), then calls the script to evaluate relay state
interval:
- interval: "1min"
then:
- lambda: |-
// — update current_mins via SNTP or fallback
if (!id(sntp_time).now().is_valid()) {
id(current_mins)++;
if (id(current_mins) >= 1440) id(current_mins) = 0;
} else {
auto now = id(sntp_time).now();
id(current_mins) = now.hour * 60 + now.minute;
}
// — if in BOOST, advance boost_timer and expire when done
if (id(operation_mode) == 3) {
id(boost_timer)++;
if (id(boost_timer) >= id(boost_duration)) {
id(operation_mode) = 2;
//id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
}
}
- script.execute: evaluate_relay_state

View File

@@ -80,6 +80,8 @@ bluetooth_proxy:
esp32_ble_tracker:
#xiaomi_ble:
###################################################################################
## Help with Mi Bluetooth Temp Sensors
## Youtube Howto https://www.youtube.com/watch?v=l5ea7lQWpMk&t=0s

View File

@@ -0,0 +1,9 @@
#############################################
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
encryption:
key: ${local_api_key}
reboot_timeout: 0s # disables watchdog reboot on API failure

View File

@@ -0,0 +1,9 @@
#############################################
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
#encryption:
#key: ${local_api_key}
reboot_timeout: 0s # disables watchdog reboot on API failure

View File

@@ -0,0 +1,142 @@
##########################################################################################
# GLOBAL VARIABLES
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
####################################################
# total_energy: cumulative power
# Not restored, so each boot starts fresh at 0.
####################################################
- id: total_energy
type: float
restore_value: yes
initial_value: '0.0'
##########################################################################################
# GENERAL COMMON SENSORS
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
#############################################
# CSE7766 POWER SENSOR
# https://esphome.io/components/sensor/cse7766.html
#############################################
- platform: hlw8012
id: athom_hlw8012
sel_pin:
number: GPIO12
inverted: True
cf_pin: GPIO4
cf1_pin: GPIO5
voltage_divider: 780
current:
name: "Current"
id: current
unit_of_measurement: A
accuracy_decimals: 2
icon: mdi:current-ac
filters:
- calibrate_linear:
- 0.0000 -> 0.0110 # Relay off no load
- 0.0097 -> 0.0260 # Relay on no load
- 0.9270 -> 0.7570
- 2.0133 -> 1.6330
- 2.9307 -> 2.3750
- 5.4848 -> 4.4210
- 8.4308 -> 6.8330
- 9.9171 -> 7.9830
# Normalize for plug load
- lambda: if (x < 0.0260) return 0; else return (x - 0.0260);
on_value_range:
- above: ${local_current_limit}
then:
- switch.turn_off: relay
voltage:
name: "Voltage"
id: voltage
unit_of_measurement: V
accuracy_decimals: 1
icon: mdi:sine-wave
filters:
- skip_initial: 2
power:
name: "Power"
id: power_sensor
unit_of_measurement: W
accuracy_decimals: 1
icon: mdi:power
filters:
- calibrate_linear:
- 0.0000 -> 0.5900 # Relay off no load
- 0.0000 -> 1.5600 # Relay on no load
- 198.5129 -> 87.8300
- 434.2469 -> 189.5000
- 628.6241 -> 273.9000
- 1067.0067 -> 460.1000
- 1619.8098 -> 699.2000
- 2043.0282 -> 885.0000
# Normalize for plug load
- lambda: if (x < 1.5600) return 0; else return (x - 1.5600);
change_mode_every: 1
update_interval: 5s
# Shows the Energy kWh since the device was last started
energy:
name: "Energy (Since Restart)"
id: energy
icon: mdi:lightning-bolt
unit_of_measurement: kWh
accuracy_decimals: 3
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
on_value:
then:
- lambda: |-
static float previous_energy_value = 0.0;
float current_energy_value = id(energy).state;
id(total_energy) += current_energy_value - previous_energy_value;
previous_energy_value = current_energy_value;
id(total_energy_sensor).update();
# internal: ${hide_energy_sensor}
#############################################
# Total Energy (All Time)
#############################################
- platform: template
name: "Total Energy"
id: total_energy_sensor
unit_of_measurement: kWh
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
accuracy_decimals: 3
lambda: |-
return id(total_energy);
update_interval: "60s"
#############################################
# Total Daily Energy
# https://esphome.io/components/sensor/total_daily_energy.html
#############################################
- platform: total_daily_energy
name: "Total Daily Energy"
restore: true
power_id: power_sensor
unit_of_measurement: kWh
icon: mdi:hours-24
accuracy_decimals: 3
filters:
- multiply: 0.001
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
status_led:
pin:
number: GPIO13
inverted: True

View File

@@ -0,0 +1,127 @@
##########################################################################################
# GLOBAL VARIABLES
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
####################################################
# total_energy: cumulative power
# Not restored, so each boot starts fresh at 0.
####################################################
- id: total_energy
type: float
restore_value: yes
initial_value: '0.0'
##########################################################################################
# GENERAL COMMON SENSORS
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
#############################################
# CSE7766 POWER SENSOR
# https://esphome.io/components/sensor/cse7766.html
#############################################
- platform: cse7766
id: athom_cse7766
current:
name: "Current"
icon: mdi:current-ac
filters:
- throttle_average: "10s"
- lambda: if (x < 0.060) return 0.0; else return x; #For the chip will report less than 3w power when no load is connected
on_value_range:
- above: "${local_current_limit}"
then:
- switch.turn_off: relay
voltage:
name: "Voltage"
icon: mdi:sine-wave
filters:
- throttle_average: "10s"
power:
name: "Power"
id: power_sensor
icon: mdi:power
filters:
- throttle_average: "10s"
- lambda: if (x < 3.0) return 0.0; else return x; #For the chip will report less than 3w power when no load is connected
energy:
name: "Energy"
id: energy
icon: mdi:lightning-bolt
unit_of_measurement: kWh
filters:
- throttle: "10s"
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
on_value:
then:
- lambda: |-
static float previous_energy_value = 0.0;
float current_energy_value = id(energy).state;
id(total_energy) += current_energy_value - previous_energy_value;
previous_energy_value = current_energy_value;
id(total_energy_sensor).update();
apparent_power:
name: "Apparent Power"
icon: mdi:power
filters:
- throttle_average: "10s"
reactive_power:
name: "Reactive Power"
icon: mdi:flash
filters:
- throttle_average: "10s"
power_factor:
name: "Power Factor"
icon: mdi:percent-outline
filters:
- throttle_average: "10s"
#############################################
# Total Energy (All Time)
#############################################
- platform: template
name: "Total Energy"
id: total_energy_sensor
unit_of_measurement: kWh
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
accuracy_decimals: 3
lambda: |-
return id(total_energy);
update_interval: "60s"
#############################################
# Total Daily Energy
# https://esphome.io/components/sensor/total_daily_energy.html
#############################################
- platform: total_daily_energy
name: "Total Daily Energy"
restore: true
power_id: power_sensor
unit_of_measurement: kWh
icon: mdi:hours-24
accuracy_decimals: 3
filters:
- multiply: 0.001
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
status_led:
pin:
number: GPIO06
inverted: False
##########################################################################################
# UART Bus
# https://esphome.io/components/uart.html
##########################################################################################
uart:
rx_pin: GPIO20
baud_rate: 4800
data_bits: 8
stop_bits: 1
parity: EVEN

View File

@@ -0,0 +1,24 @@
#############################################
# SYSTEM SPECIFIC VARIABLE SUBSTITUTIONS
#############################################
substitutions:
mqtt_server: !secret ha_mqtt_server
mqtt_username: !secret ha_mqtt_username
mqtt_password: !secret ha_mqtt_password
mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
#############################################
# 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}/${local_device_name}
username: ${mqtt_username}
password: ${mqtt_password}
discovery: False # enable entity discovery (true is default)
discover_ip: False # enable device discovery (true is default)
id: mqtt_client
reboot_timeout: 0s # same for MQTT
log_topic: null # <— stops MQTT log streaming (big JSON payloads)

View File

@@ -0,0 +1,77 @@
substitutions:
local_gpioinvert: "True"
local_singleclick_duration: "300000" # 5 minutes in ms
# local_doubleclick_duration: "1800000" # 30 minutes in ms
# local_tripleclick_duration: "7200000" # 2 hours in ms
#globals:
# - id: single_click_delay
# type: long
# restore_value: no
# initial_value: ${local_singleclick_duration}
# - id: double_click_delay
# type: long
# restore_value: no
# initial_value: ${local_doubleclick_duration}
# - id: triple_click_delay
# type: long
# restore_value: no
# initial_value: ${local_tripleclick_duration}
binary_sensor:
- platform: gpio
pin:
number: ${local_gpio}
mode: INPUT
inverted: ${local_gpioinvert}
name: ${local_name}
on_multi_click:
# Single Click: one press (short press/release)
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: |-
if (id(Relay_3).state) {
ESP_LOGD("button3", "Single click: Relay_3 is on, turning it off.");
id(Relay_3).turn_off();
} else {
ESP_LOGD("button3", "Single click: Relay_3 is off, turning it on for %d ms.", id(single_click_delay));
id(Relay_3).turn_on();
delay(id(single_click_delay));
id(Relay_3).turn_off();
}
# # Double Click: two quick press/release cycles
# - timing:
# - ON for at most 1s
## - OFF for at most 1s
# - ON for at most 1s
# - OFF for at least 0.5s
# then:
# - lambda: |-
# ESP_LOGD(${local_gpio}, "Double click detected: turning ",${local_relay_id}," on for %d ms.", ${local_doubleclick_duration});
## ${local_relay_id}->turn_on();
# delay(${local_doubleclick_duration});
# ${local_relay_id}->turn_off();
# # Triple Click: three quick press/release cycles
## - timing:
# - ON for at most 1s
## - OFF for at most 1s
# - ON for at most 1s
# - OFF for at most 1s
# - ON for at most 1s
# - OFF for at least 0.5s
# then:
# - lambda: |-
# ESP_LOGD(${local_gpio}, "Triple click detected: turning ",${local_relay_id}," on for %d ms.", ${local_tripleclick_duration});
# ${local_relay_id}->turn_on();
# delay(${local_tripleclick_duration});
# ${local_relay_id}->turn_off();
# # Hold: button held for at least 4 seconds
# - timing:
# - ON for at least 4s
## then:
# - lambda: |-
# ESP_LOGD(${local_gpio}, "Hold detected: turning ",${local_relay_id}," on indefinitely.");
# ${local_relay_id}->turn_on();

View File

@@ -0,0 +1,122 @@
substitutions:
##############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
wifi_ssid: !secret ha_wifi_ssid
wifi_password: !secret ha_wifi_password
fallback_ap_password: !secret fallback_ap_password
# Enables faster network connections, with last connected SSID being connected to and no full scan for SSID being undertaken
wifi_fast_connect: "false"
# Define a domain for this device to use. i.e. iot.home.lan (so device will appear as athom-smart-plug-v2.iot.home.lan in DNS/DHCP logs)
dns_domain: ".local"
# Automatically add the mac address to the name
# eg so you can use a single firmware for all devices
add_mac_suffix: "false"
# Enable or disable the use of IPv6 networking on the device
ipv6_enable: "false"
# Add these if we are giving it a static ip, or remove them in the Wifi section
static_ip_subnet: !secret ha_wifi_subnet
static_ip_gateway: !secret ha_wifi_gateway
static_ip_dns1: !secret ha_wifi_dns1
static_ip_dns2: !secret ha_wifi_dns2
# Network reconnect every x hours to ensure best access point
base_interval_hours: "23" # Base interval in hours
random_offset_max_minutes: "59" # Max random offset in minutes
#############################################
# Common 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}
#enable_rrm: true # (ESP32 only) enable 802.11k Radio Resource Management
#enable_btm: true # (ESP32 only) enable 802.11v BSS Transition Management
#power_save_mode: LIGHT # https://esphome.io/components/wifi.html#wifi-power-save-mode
manual_ip: # optional static IP address
static_ip: ${local_static_ip_address}
gateway: ${static_ip_gateway}
subnet: ${static_ip_subnet}
dns1: ${static_ip_dns1}
dns2: ${static_ip_dns2}
use_address: ${local_static_ip_address}
ap: # Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${local_device_name} AP
password: ${fallback_ap_password}
ap_timeout: 10min # Time until it brings up fallback AP. default is 1min
# Allow rapid re-connection to previously connect WiFi SSID, skipping scan of all SSID
fast_connect: "${wifi_fast_connect}"
# Define dns domain / suffix to add to hostname
domain: "${dns_domain}"
#captive_portal: # extra fallback mechanism for when connecting if the configured WiFi fails
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: esphome
password: ${local_ota_pass}
version: 2
#- platform: web_server
#############################################
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################
safe_mode:
#############################################
# Network
# global configuration for all types of networks
# https://esphome.io/components/network.html
#############################################
network:
enable_ipv6: ${ipv6_enable}
#############################################
# Interval
# Restart Networking every x hours + rand mins
# This ensure that the device is connected to the best AP
#############################################
script:
- id: random_reconnect
mode: restart
then:
- lambda: |-
// Compute total delay: base hours + random offset minutes
uint32_t extra;
#if defined(ESP32)
// ESP32 (both Arduino & IDF builds) uses esp_random()
extra = esp_random() % (${random_offset_max_minutes} + 1);
#elif defined(ESP8266)
// ESP8266 Arduino core
extra = os_random() % (${random_offset_max_minutes} + 1);
#else
// Fallback to esp_random() on other platforms
extra = esp_random() % (${random_offset_max_minutes} + 1);
#endif
uint32_t total_s = ${base_interval_hours} * 3600 + extra * 60;
ESP_LOGI("random_reconnect", "Next reconnect in %u seconds", total_s);
// Delay inside lambda (blocks script execution but OK for reconnect timing)
delay(total_s * 1000);
- logger.log: "network_check: performing reconnect"
- wifi.disable: {}
- delay: 1s
- wifi.enable: {}
- script.execute: random_reconnect

View File

@@ -0,0 +1,126 @@
substitutions:
##############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
wifi_ssid: !secret ha_wifi_ssid
wifi_password: !secret ha_wifi_password
fallback_ap_password: !secret fallback_ap_password
# Enables faster network connections, with last connected SSID being connected to and no full scan for SSID being undertaken
wifi_fast_connect: "false"
# Define a domain for this device to use. i.e. iot.home.lan (so device will appear as athom-smart-plug-v2.iot.home.lan in DNS/DHCP logs)
dns_domain: ".local"
# Automatically add the mac address to the name
# eg so you can use a single firmware for all devices
add_mac_suffix: "false"
# Enable or disable the use of IPv6 networking on the device
ipv6_enable: "false"
# Add these if we are giving it a static ip, or remove them in the Wifi section
static_ip_subnet: !secret ha_wifi_subnet
static_ip_gateway: !secret ha_wifi_gateway
static_ip_dns1: !secret ha_wifi_dns1
static_ip_dns2: !secret ha_wifi_dns2
# Network reconnect every x hours to ensure best access point
base_interval_hours: "6" # Base interval in hours
random_offset_max_minutes: "59" # Max random offset in minutes
#############################################
# Common 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:
networks:
- ssid: "vega_control_1"
password: "3Kd#vQBP8RDc"
priority: 1.0
- ssid: ${wifi_ssid}
password: ${wifi_password}
#enable_rrm: true # (ESP32 only) enable 802.11k Radio Resource Management
#enable_btm: true # (ESP32 only) enable 802.11v BSS Transition Management
#power_save_mode: LIGHT # https://esphome.io/components/wifi.html#wifi-power-save-mode
priority: 0.5
manual_ip: # optional static IP address
static_ip: ${local_static_ip_address}
gateway: ${static_ip_gateway}
subnet: ${static_ip_subnet}
dns1: ${static_ip_dns1}
dns2: ${static_ip_dns2}
use_address: ${local_static_ip_address} # required when any network uses manual_ip
ap: # Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${device_name} AP
password: ${fallback_ap_password}
ap_timeout: 10min # Time until it brings up fallback AP. default is 1min
# Allow rapid re-connection to previously connect WiFi SSID, skipping scan of all SSID
fast_connect: "${wifi_fast_connect}"
# Define dns domain / suffix to add to hostname
domain: "${dns_domain}"
#captive_portal: # extra fallback mechanism for when connecting if the configured WiFi fails
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: esphome
password: ${local_ota_pass}
version: 2
#############################################
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################
safe_mode:
#############################################
# Network
# global configuration for all types of networks
# https://esphome.io/components/network.html
#############################################
network:
enable_ipv6: ${ipv6_enable}
#############################################
# Interval
# Restart Networking every x hours + rand mins
#############################################
script:
- id: random_reconnect
mode: restart
then:
- lambda: |-
// Compute total delay: base hours + random offset minutes
uint32_t extra;
#if defined(ESP32)
// ESP32 (both Arduino & IDF builds) uses esp_random()
extra = esp_random() % (${random_offset_max_minutes} + 1);
#elif defined(ESP8266)
// ESP8266 Arduino core
extra = os_random() % (${random_offset_max_minutes} + 1);
#else
// Fallback to esp_random() on other platforms
extra = esp_random() % (${random_offset_max_minutes} + 1);
#endif
uint32_t total_s = ${base_interval_hours} * 3600 + extra * 60;
ESP_LOGI("random_reconnect", "Next reconnect in %u seconds", total_s);
// Delay inside lambda (blocks script execution but OK for reconnect timing)
delay(total_s * 1000);
- logger.log: "network_check: performing reconnect"
- wifi.disable: {}
- delay: 1s
- wifi.enable: {}
- script.execute: random_reconnect

View File

@@ -0,0 +1,81 @@
substitutions:
##############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
wifi_ssid: !secret ha_wifi_ssid
wifi_password: !secret ha_wifi_password
fallback_ap_password: !secret fallback_ap_password
# Enables faster network connections, with last connected SSID being connected to and no full scan for SSID being undertaken
wifi_fast_connect: "false"
# Define a domain for this device to use. i.e. iot.home.lan (so device will appear as athom-smart-plug-v2.iot.home.lan in DNS/DHCP logs)
dns_domain: ".local"
# Automatically add the mac address to the name
# eg so you can use a single firmware for all devices
add_mac_suffix: "false"
# Enable or disable the use of IPv6 networking on the device
ipv6_enable: "false"
# Add these if we are giving it a static ip, or remove them in the Wifi section
static_ip_subnet: !secret ha_wifi_subnet
static_ip_gateway: !secret ha_wifi_gateway
static_ip_dns1: !secret ha_wifi_gateway
#############################################
# Common 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}
#enable_rrm: true # (ESP32 only) enable 802.11k Radio Resource Management
#enable_btm: true # (ESP32 only) enable 802.11v BSS Transition Management
#power_save_mode: LIGHT # https://esphome.io/components/wifi.html#wifi-power-save-mode
manual_ip: # optional static IP address
static_ip: ${local_static_ip_address}
gateway: ${static_ip_gateway}
subnet: ${static_ip_subnet}
dns1: ${static_ip_dns1}
ap: # Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${device_name} AP
password: ${fallback_ap_password}
ap_timeout: 10min # Time until it brings up fallback AP. default is 1min
# Allow rapid re-connection to previously connect WiFi SSID, skipping scan of all SSID
fast_connect: "${wifi_fast_connect}"
# Define dns domain / suffix to add to hostname
domain: "${dns_domain}"
#captive_portal: # extra fallback mechanism for when connecting if the configured WiFi fails
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: esphome
password: ${local_ota_pass}
version: 2
#############################################
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################
safe_mode:
#############################################
# Network
# global configuration for all types of networks
# https://esphome.io/components/network.html
#############################################
network:
enable_ipv6: ${ipv6_enable}

View File

@@ -0,0 +1 @@
<<: !include ../secrets.yaml

View File

@@ -0,0 +1,161 @@
##########################################################################################
# GENERAL COMMON SENSORS
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
- platform: uptime # Uptime for this device in seconds
name: "Uptime (s):"
update_interval: ${local_update_interval}
id: uptime_sensor
entity_category: "diagnostic"
- platform: wifi_signal # Wifi Strength
name: "Wifi (dB):"
id: wifi_signal_db
update_interval: ${local_update_interval}
entity_category: "diagnostic"
- platform: copy # Reports the WiFi signal strength in %
source_id: wifi_signal_db
name: "WiFi (%):"
filters:
- lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
unit_of_measurement: "% Max"
entity_category: "diagnostic"
device_class: ""
# Only works with esp8266
#- platform: adc
# pin: VCC
# name: "VCC Voltage"
# id: vcc_voltage_sensor
# entity_category: diagnostic
binary_sensor:
- platform: status
name: "Network Status"
icon: mdi:check-network-outline
entity_category: diagnostic
##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
######################################################
# General ESPHome Info
######################################################
- platform: version
name: "Version:"
entity_category: "diagnostic"
- platform: wifi_info
ip_address:
icon: mdi:ip-network
entity_category: diagnostic
name: "IP Address:"
ssid:
name: "Connected SSID"
icon: mdi:wifi-strength-2
entity_category: diagnostic
mac_address:
name: "MAC Address:"
icon: mdi:network-pos
entity_category: diagnostic
- platform: uptime # Uptime for this device human readable
name: "Uptime:"
icon: mdi:clock-start
update_interval: ${local_update_interval}
entity_category: "diagnostic"
- platform: template
name: "Last Reset Reason"
update_interval: 30s
entity_category: diagnostic
lambda: |-
#if defined(USE_ESP8266)
return { ESP.getResetReason().c_str() };
#elif defined(USE_ESP32)
auto r = esp_reset_reason();
switch (r) {
case ESP_RST_POWERON: return { "Power-on" };
case ESP_RST_EXT: return { "External pin" };
case ESP_RST_SW: return { "Software reset" };
case ESP_RST_PANIC: return { "Exception/Panic" };
case ESP_RST_INT_WDT: return { "Interrupt WDT" };
case ESP_RST_TASK_WDT: return { "Task WDT" };
case ESP_RST_WDT: return { "Other WDT" };
case ESP_RST_DEEPSLEEP: return { "Deep sleep wake" };
case ESP_RST_BROWNOUT: return { "Brownout" };
case ESP_RST_SDIO: return { "SDIO" };
default: return { "Unknown" };
}
#else
return { "Unknown" };
#endif
###################################################################################################
# Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds
###################################################################################################
# - platform: template
# name: "Uptime (Days)"
# entity_category: diagnostic
# lambda: |-
# int seconds = (id(uptime_sensor).state);
# int days = seconds / (24 * 3600);
# seconds = seconds % (24 * 3600);
# int hours = seconds / 3600;
# seconds = seconds % 3600;
# int minutes = seconds / 60;
# seconds = seconds % 60;
# if ( days > 3650 ) {
# return "Starting up" ;
# } else if ( days ) {
# return (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() ;
# } else if ( hours ) {
# return (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() ;
# } else if ( minutes ) {
# return (String(minutes) +"m "+ String(seconds) +"s").c_str() ;
# } else {
# return (String(seconds) +"s").c_str() ;
# }
# icon: mdi:clock-start
# - platform: template
# name: "Uptime (Days)"
# entity_category: diagnostic
# lambda: |-
# int seconds = (id(uptime_sensor).state);
# int days = seconds / (24 * 3600);
# seconds = seconds % (24 * 3600);
# int hours = seconds / 3600;
# seconds = seconds % 3600;
# int minutes = seconds / 60;
# seconds = seconds % 60;
# if ( days > 3650 ) {
# return { "Starting up" };
# } else if ( days ) {
# return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() };
# } else if ( hours ) {
# return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() };
# } else if ( minutes ) {
# return { (String(minutes) +"m "+ String(seconds) +"s").c_str() };
# } else {
# return { (String(seconds) +"s").c_str() };
# }
# icon: mdi:clock-start
##########################################################################################
# BUTTONS
# Diagnostic buttons, activated if needed in HA
##########################################################################################
button:
- platform: safe_mode
name: "Safe Mode Restart:"
entity_category: "diagnostic"
disabled_by_default: true
- platform: restart
name: "Restart:"
entity_category: "diagnostic"
disabled_by_default: true
- platform: factory_reset
name: "FACTORY RESET:"
entity_category: "diagnostic"
disabled_by_default: true

View File

@@ -0,0 +1,41 @@
##########################################################################################
# GENERAL COMMON SENSORS
# https://esphome.io/components/sensor/
# LITE (1MB-friendly)
##########################################################################################
sensor:
- platform: uptime
name: "Uptime (s):"
update_interval: ${local_update_interval}
id: uptime_sensor
entity_category: diagnostic
- platform: wifi_signal
name: "Wifi (dB):"
id: wifi_signal_db
update_interval: ${local_update_interval}
entity_category: diagnostic
binary_sensor:
- platform: status
name: "Network Status"
icon: mdi:check-network-outline
entity_category: diagnostic
##########################################################################################
# BUTTONS
# Diagnostic buttons, activated if needed in HA
##########################################################################################
button:
- platform: safe_mode
name: "Safe Mode Restart:"
entity_category: "diagnostic"
disabled_by_default: true
- platform: restart
name: "Restart:"
entity_category: "diagnostic"
disabled_by_default: true
- platform: factory_reset
name: "FACTORY RESET:"
entity_category: "diagnostic"
disabled_by_default: true

View File

@@ -0,0 +1,75 @@
##############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace
# these with the values (in quotes)
#############################################
substitutions:
timezone: "Pacific/Auckland"
sntp_update_interval: 6h # Set the duration between the sntp service polling
# Network time servers https://www.ntppool.org/zone/@
sntp_server_1: !secret ntp_server_1
sntp_server_2: !secret ntp_server_2
sntp_server_3: !secret ntp_server_3
#############################################
# Real time clock time source for ESPHome
# If it's invalid, we fall back to an internal clock
# https://esphome.io/components/time/index.html
# https://esphome.io/components/time/sntp
#############################################
time:
- platform: sntp
id: sntp_time
# Define the timezone of the device
timezone: "${timezone}"
# Change sync interval from default 5min to 6 hours (or as set in substitutions)
update_interval: ${sntp_update_interval}
# Set specific sntp servers to use
servers:
- "${sntp_server_1}"
- "${sntp_server_2}"
- "${sntp_server_3}"
# Publish the time the device was last restarted
on_time_sync:
then:
- logger.log: "Synchronised SNTP clock"
- text_sensor.template.publish:
id: time_sync
state: "SNTP clock Syncd"
# Update last restart time, but only once.
- if:
condition:
lambda: 'return id(device_last_restart).state == "";'
then:
- text_sensor.template.publish:
id: device_last_restart
#state: !lambda 'return id(sntp_time).now().strftime("%a %d %b %Y - %I:%M:%S %p");'
state: !lambda |-
// grab the current SNTP time
auto now = id(sntp_time).now();
// format it as e.g. "Fri 23 May 2025 - 03:45:12 PM"
return now.strftime("%a %d %b %Y - %I:%M:%S %p");
text_sensor:
# Creates a sensor showing when the device was last restarted
- platform: template
name: "Last Restart"
id: device_last_restart
icon: mdi:clock
entity_category: diagnostic
#device_class: timestamp
- platform: template
name: "Internal Time"
id: time_text
update_interval: "1min"
entity_category: diagnostic
lambda: |-
auto time_text = id(sntp_time).now().strftime("%H:%M:%S - %d-%m-%Y");
return { time_text };
- platform: template
name: "Time Sync Status"
id: time_sync
update_interval: "1min"
entity_category: diagnostic

View File

@@ -0,0 +1,20 @@
##############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace
# these with the values (in quotes)
#############################################
substitutions:
web_server_username: !secret web_server_username
web_server_password: !secret web_server_password
#############################################
# Web Portal for display and monitoring
# Turning this off is maybe a good idea to save resources,
# especially on an esp8266.
# https://esphome.io/components/web_server.html
#############################################
web_server:
port: 80
auth:
username: ${web_server_username} # probably a good idea to secure it
password: ${web_server_password}

View File

@@ -0,0 +1,183 @@
#############################################
#############################################
# 3D PRINTER POWER AND SWITCH
# V1.0 2025-06-29 Initial Version
#############################################
# Sonoff POW R1
# https://devices.esphome.io/devices/Sonoff-POW-R1
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-3dprinterpow" # Reference name for the device in the system.
project_name: "Sonoff Technologies.POW R1" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Load" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "3D Printer Power"
description_comment: "3D Printer inline monitor and switch override using a Sonoff Pow R1."
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-3dprinterpow_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
#priority: 200
#then:
# - switch.turn_on: "virtual_button"
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
#status_led:
# pin:
# number: GPIO2
# inverted: yes
#############################################
# SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO00
mode: INPUT_PULLUP
inverted: True
name: Button
on_press:
- switch.toggle: virtual_button
- platform: template
name: ${entity_prefix} Running
filters:
- delayed_off: 15s
lambda: |-
if (isnan(id(power).state)) {
return {};
} else if (id(power).state > 4) {
// Running
return true;
} else {
// Not running
return false;
}
sensor:
- platform: hlw8012
sel_pin: 5
cf_pin: 14
cf1_pin: 13
update_interval: 2s
current:
name: ${entity_prefix} Current
voltage:
name: ${entity_prefix} Voltage
power:
name: ${entity_prefix} Power
id: power
#on_value_range:
# - above: 4.0
# then:
# - light.turn_on: led
# - below: 3.0
# then:
# - light.turn_off: led
switch:
- platform: template
name: "Relay"
optimistic: true
id: virtual_button
turn_on_action:
- switch.turn_on: relay
- light.turn_on: led
turn_off_action:
- switch.turn_off: relay
- light.turn_off: led
- platform: gpio
id: relay
pin: GPIO12
#restore_mode: ALWAYS_ON # Or RESTORE_DEFAULT_ON/RESTORE_DEFAULT_OFF
output:
- platform: esp8266_pwm
id: pow_blue_led
pin:
number: GPIO15
inverted: True
light:
- platform: monochromatic
name: Status LED
output: pow_blue_led
id: led

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,244 @@
############################################
#############################################
# ATHOM 15W RGBWW Bulb Flasher
#
# # V1.0 2025-02-15 Initial Version
#
# Device https://www.athom.tech/blank-1/15w-color-bulb
#
# DESCRIPTION
# Starts up and flashes a RGBWW lightbulb a white colour
# On and off time (duty cycle) is configurable
# Colour Temp (in K) is configurable
# There is no fading between transitions
#
#############################################
#############################################
#############################################
# USER VARIABLE SUBSTITUTIONS
# Give the device a useful name & description here
# and change values accordingly.
#############################################
substitutions:
devicename: esp-athombulbflasher
friendly_name: "Flasher for Athom 15W Bulb"
description_comment: "Athom RGBWW light bulb that will flash on boot and allow colour temp & duty cycle adjustment"
# Adjust the color temperature here (200 mired is approx 5000k
white_temp: "3000K"
# Adjust the ON/OFF times for flashing.
# 600ms on, 400ms off => 60% ON, 40% OFF duty cycle
on_time_ms: "500ms"
off_time_ms: "500ms"
#############################################
# OTHER VARIABLE SUBSTITUTIONS
# Give the device a useful name & description here
# and change values accordingly.
#############################################
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
api_key: !secret esp-athombulbflasher_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-athombulbflasher_ota_pass # unfortunately you can't use substitutions inside 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
#static_ip_address: !secret esp-athombulbflasher_static_ip
#static_ip_gateway: !secret esp-athombulbflasher_gateway
#static_ip_subnet: !secret esp-athombulbflasher_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
# Add these if we are using the internal web server (this is pretty processor intensive)
#web_server_username: !secret web_server_username
#web_server_password: !secret web_server_password
#update_interval: 60s # update time for for general sensors etc
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esphome:
name: ${devicename}
friendly_name: ${friendly_name}
comment: ${description_comment} # appears on the esphome page in HA
on_boot:
priority: 800
then:
- light.turn_on:
id: my_light
effect: "Flashing White"
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
#############################################
# 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
#############################################
# 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 in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${devicename} AP
password: ${fallback_ap_password}
ap_timeout: 30min # Time until it brings up fallback AP. default is 1min
captive_portal: # extra fallback mechanism for when connecting if the configured WiFi fails
#############################################
# 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: True # enable entity discovery (true is default)
##discover_ip: True # enable device discovery (true is default)
#############################################
# Web Portal for display and monitoring
# Turning this off is probably a good idea to save resources.
# https://esphome.io/components/web_server.html
#############################################
#web_server:
# port: 80
# auth:
# username: ${web_server_username} # probably a good idea to secure it
# password: ${web_server_password}
# ---------------------------------------------------------
# OUTPUTS: 5 x PWM channels for the Athom 15W RGBWW bulb
# ---------------------------------------------------------
output:
- platform: esp8266_pwm
id: output_red
pin: GPIO4
frequency: 1000 Hz
- platform: esp8266_pwm
id: output_green
pin: GPIO5
frequency: 1000 Hz
- platform: esp8266_pwm
id: output_blue
pin: GPIO12
frequency: 1000 Hz
# Typically one channel is cold white...
- platform: esp8266_pwm
id: output_cold_white
pin: GPIO14
frequency: 1000 Hz
# ...and one channel is warm white (inverted on GPIO13).
- platform: esp8266_pwm
id: output_warm_white
pin: GPIO13
frequency: 1000 Hz
inverted: true
# ---------------------------------------------------------
# LIGHT COMPONENT: RGBWW + a strobe effect for flashing
# ---------------------------------------------------------
light:
- platform: rgbww
name: ${friendly_name}
id: my_light
red: output_red
green: output_green
blue: output_blue
cold_white: output_cold_white
warm_white: output_warm_white
# Typical white temperature mapping for RGBWW
cold_white_color_temperature: 6500 K
warm_white_color_temperature: 2700 K
# Ensure it always powers on (and with our desired effect).
restore_mode: ALWAYS_ON
# You can omit this or set it to zero for no fade on effect changes.
default_transition_length: 0s
# The custom strobe effect for flashing white
effects:
- strobe:
name: "Flashing White"
colors:
# Note the Kelvin value is replaced at compile time:
- state: True
brightness: 100%
color_temperature: "${white_temp}"
duration: ${on_time_ms}
- state: False
brightness: 0%
color_temperature: "${white_temp}"
duration: ${off_time_ms}

216
esphome/esp-attobat.yaml Normal file
View File

@@ -0,0 +1,216 @@
#############################################
#############################################
# BYD ATTO3 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-attobat"
friendly_name: "Atto3 12V Battery Monitor"
description_comment: "Atto3 12V Battery Monitor (when home)"
api_key: !secret esp-attobat_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-attobat_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-attobat_static_ip
static_ip_gateway: !secret esp-attobat_gateway
static_ip_subnet: !secret esp-attobat_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
#############################################
# 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
# on_message:
# - topic: ${mqtt_commandstopic}/${devicename}/deepsleepon
# payload: 'ON'
# then:
# - deep_sleep.prevent: deep_sleep_1
# - topic: ${mqtt_commandstopic}/${devicename}/deepsleepoff
# payload: 'OFF'
# 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
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
sensor:
#Quality of Wifi in dBm
- platform: wifi_signal
name: "WiFi Signal"
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"
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
filters:
- multiply: 3.3 #D1 mini V divider, 3.3V -> 1V on esp8266
- calibrate_linear: #Read values with voltmeter and bench supply
- 3.11 -> 14.00
- 3.00 -> 13.50
- 2.89 -> 13.00
- 2.77 -> 12.50
- 2.67 -> 12.00
- 2.55 -> 11.50
- 2.45 -> 11.00
- 2.34 -> 10.50
- 2.22 -> 10.00
- 2.11 -> 09.50
- 2.00 -> 09.00

View File

@@ -0,0 +1,381 @@
##########################################################################################
##########################################################################################
# CEILING FAN - BEDROOM 2
# - 3 speed fan using Sonoff IFan02
# - Also, single Light Output
#
# Controlled by a Sonoff IFan 02
#
# V1.0 - 2025-07-21 First Setup (and replacement of Tasmota)
#
# NOTES:
# Command the fan with MQTT
# ${mqtt_local_command_full_topic}/speed/set 1,2,3,0,+,-
# ${mqtt_local_command_full_topic}/light/set ON,OFF
# Status of the fan is in MQTT
# ${mqtt_local_command_full_topic}/speed/state 1,2,3,0
# ${mqtt_local_command_full_topic}/light/state ON,OFF
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-bedrm2ceilingfan"
friendly_name: "Bedroom 2 Ceiling Fan"
description_comment: "3 Speed Overhead Ceiling Fan Bedroom 2 :: Sonoff ifan02"
device_area: "Bedroom 2" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff ifan02" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-bedrm2ceilingfan_ip # unfortunately you can't use substitutions inside secrets names
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
mqtt_device_name: "bedroom2-ceilingfan"
mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
# Button Naming & Icons
# Switch/Relay Naming & Icons
#relay_icon: "mdi:lightbulb-group"
light_1_name: "Fan Light"
switch_2_name: "Fan 2 Relay"
switch_3_name: "Fan 3 Relay"
switch_4_name: "Fan 4 Relay"
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
on_boot: # This weird toggle thing makes sure the states get restored on reboot
priority : -100
then:
- delay: 200ms
#- fan.toggle: ifan02_fan
#- fan.toggle: ifan02_fan
#- delay: 200ms
#- light.toggle: ifan02_light
#- delay: 1ms
#- light.toggle: ifan02_light
#- delay: 1s
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: !lambda 'return to_string(id(speed_value));'
# platformio_options:
# build_flags:
# - "-Os" # optimize for size
# - "-Wl,--gc-sections" # drop unused code/data
# - "-fno-exceptions" # strip C++ exceptaions
# - "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: True # Disabling will make the build file smaller (and it is still available via static IP)
##########################################################################################
# GLOBAL VARIABLES
##########################################################################################
globals:
- id: speed_value
type: int
restore_value: yes
initial_value: '0'
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" # 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, Serial control)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Light control
- topic: "${mqtt_local_command_topic}/light/set"
payload: "ON"
then:
- light.turn_on: ifan02_light
- topic: "${mqtt_local_command_topic}/light/set"
payload: "OFF"
then:
- light.turn_off: ifan02_light
# Fan speed control (03) + ramp up/down (+ / -)
- topic: "${mqtt_local_command_topic}/speed/set"
then:
# 1) Compute new speed_value based on payload
- lambda: |-
std::string s = x;
int val = id(speed_value);
if (s == "+") {
val++;
} else if (s == "-") {
val--;
} else if (s.size() && isdigit(s[0])) {
val = atoi(s.c_str());
} else {
ESP_LOGE("ifan02", "Invalid speed payload: '%s'", x.c_str());
return;
}
// Clamp between 0 and 3
if (val < 0) val = 0;
if (val > 3) val = 3;
id(speed_value) = val;
# 2) Drive the fan based on the new value
- if:
condition:
lambda: 'return id(speed_value) == 0;'
then:
- fan.turn_off: ifan02_fan
else:
- fan.turn_on:
id: ifan02_fan
speed: !lambda 'return id(speed_value);'
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff ifan02 has relays, but they don't need to be shown in HA because
# the speed of the fan is the important part (and combinations of the relays give that)
##########################################################################################
switch:
- platform: gpio
name: "${switch_3_name}"
pin: GPIO4
id: fan3sw
restore_mode: RESTORE_DEFAULT_OFF
internal: true
- platform: gpio
name: "${switch_2_name}"
pin: GPIO5
id: fan2sw
restore_mode: RESTORE_DEFAULT_OFF
internal: true
- platform: gpio
name: "${switch_4_name}"
pin: GPIO15
id: fan4sw
restore_mode: RESTORE_DEFAULT_OFF
internal: true
##########################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
##########################################################################################
select:
- platform: template
name: "Bedroom 2 Fan Speed"
id: bedroom2_fan_speed_select
options:
- "Off"
- "Low"
- "Medium"
- "High"
update_interval: 1s
lambda: |-
switch (id(speed_value)) {
case 1: return esphome::optional<std::string>("Low");
case 2: return esphome::optional<std::string>("Medium");
case 3: return esphome::optional<std::string>("High");
default: return esphome::optional<std::string>("Off");
}
set_action:
# OFF
- if:
condition:
lambda: 'return x == "Off";'
then:
- lambda: |-
id(speed_value) = 0;
- fan.turn_off: ifan02_fan
# LOW (1)
- if:
condition:
lambda: 'return x == "Low";'
then:
- lambda: |-
id(speed_value) = 1;
- fan.turn_on:
id: ifan02_fan
speed: 1
# MEDIUM (2)
- if:
condition:
lambda: 'return x == "Medium";'
then:
- lambda: |-
id(speed_value) = 2;
- fan.turn_on:
id: ifan02_fan
speed: 2
# HIGH (3)
- if:
condition:
lambda: 'return x == "High";'
then:
- lambda: |-
id(speed_value) = 3;
- fan.turn_on:
id: ifan02_fan
speed: 3
################################################################################
# TEMPLATE OUTPUTS: drive the real relays when the states change
################################################################################
output:
- platform: gpio
pin: GPIO12
id: lightrelay
- platform: template
type: float
id: fan_decode
write_action:
- lambda: |-
if (state < 0.25) {
id(fan2sw).turn_off();
id(fan3sw).turn_off();
id(fan4sw).turn_off();
}
else if (state < 0.5) {
id(fan2sw).turn_on();
id(fan3sw).turn_off();
id(fan4sw).turn_off();
}
else if (state < 0.75) {
id(fan2sw).turn_on();
id(fan3sw).turn_on();
id(fan4sw).turn_off();
}
else {
id(fan2sw).turn_on();
id(fan3sw).turn_off();
id(fan4sw).turn_on();
}
##########################################################################################
# LIGHT COMPONENT
# https://esphome.io/components/light/
##########################################################################################
light:
- platform: binary
name: "${light_1_name}"
output: lightrelay
restore_mode: RESTORE_DEFAULT_OFF
id: ifan02_light
# publish status updates when the light turns on/off:
on_turn_on:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "ON"
on_turn_off:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "OFF"
##########################################################################################
# FAN COMPONENT
# https://esphome.io/components/fan/index.html
##########################################################################################
fan:
- platform: speed
output: fan_decode
name: "Fan"
id: ifan02_fan
speed_count: 3
restore_mode: RESTORE_DEFAULT_OFF
# whenever you explicitly set a speed (13):
on_speed_set:
- lambda: |-
id(speed_value) = x;
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: !lambda 'return to_string(id(speed_value));'
# whenever the fan goes fully off:
on_turn_off:
- lambda: |-
id(speed_value) = 0;
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: "0"
# whenever the fan goes off→on (e.g. via HAs Fan switch):
on_turn_on:
- lambda: |-
// bump 0→1 if we were off
if (id(speed_value) == 0) id(speed_value) = 1;
- fan.turn_on:
id: ifan02_fan
speed: !lambda 'return id(speed_value);'
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: !lambda 'return to_string(id(speed_value));'

View File

@@ -0,0 +1,265 @@
##########################################################################################
##########################################################################################
# BEDROOM 2 FAN SWITCH
#
# Controlled by a Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# V1.0 - 2025-07-23 First Setup (and replacement of Tasmota)
#
# NOTES
# - Switch for Ceiling Fan
# - 3 Switches, Up, Down and Off
# - Remote commands to the fan ${mqtt_remote_command_full_topic}/speed/set 1,2,3,0,+,-
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-bedrm2fanswitch"
friendly_name: "Bedroom 2 Fan Wall Switch (3)"
description_comment: "Switch for Bedroom 2 Ceiling Fan using Zemismart KS-811 Triple Push Button. Speed Up (1), Speed Down (2), Fan Off (3)"
device_area: "Bedroom 2" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811-3 (Triple)" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Main Bathroom" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-bedrm2fanswitch_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
#relay_icon: "mdi:heating-coil"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "bedroom2-ceilingfan-switch"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
mqtt_remote_device_name: "bedroom2-ceilingfan"
mqtt_remote_device_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device_name}/speed/set"
mqtt_remote_device_command1: "+"
mqtt_remote_device_command2: "-"
mqtt_remote_device_command3: "0"
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device_name}/speed/state" # Topic we will use to view status locally without HA
# Button Naming & Icons
# Switch/Relay Naming & Icons
#relay_icon: "mdi:heating-coil"
switch_1_name: "Fan Speed Up" # This is virtual only, no power connected to 1st relay
switch_2_name: "Fan Speed Down" # This is virtual only, no power connected to 2nd relay
switch_3_name: "Fan Off" # This is virtual only, no power connected to 3rd relay
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} #Appears on the esphome page in HA
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptions
- "-fno-rtti" # strip C++ RTTI
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Status LED for KS-811 is GPIO02
##########################################################################################
status_led:
pin:
number: GPIO02
inverted: yes
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Buttons for KS-811-3 are GPIO16, GPIO05, GPIO04
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: "${mqtt_remote_device_command1}"
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: "${mqtt_remote_device_command2}"
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: ${switch_3_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: "${mqtt_remote_device_command3}"
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Relays for KS-811-3 are GPIO13, GPIO12, GPIO14
##########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: ${switch_3_name}"
pin: GPIO14
id: Relay_3
mqtt:
on_message:
- topic: "${mqtt_local_status_topic}"
then:
- lambda: |-
int val = atoi(x.c_str());
ESP_LOGI("fan_switch", "Received requested speed: %d", val);
// Desired states
bool r1 = false;
bool r2 = false;
bool r3 = false;
switch (val) {
case 1:
r3 = true;
break;
case 2:
r2 = true;
r3 = true;
break;
case 3:
r1 = true;
r2 = true;
r3 = true;
break;
case 0:
default:
// all remain false
break;
}
// Only change relays if necessary
if (id(Relay_1).state != r1) {
if (r1) {
id(Relay_1).turn_on();
} else {
id(Relay_1).turn_off();
}
}
if (id(Relay_2).state != r2) {
if (r2) {
id(Relay_2).turn_on();
} else {
id(Relay_2).turn_off();
}
}
if (id(Relay_3).state != r3) {
if (r3) {
id(Relay_3).turn_on();
} else {
id(Relay_3).turn_off();
}
}

View File

@@ -0,0 +1,180 @@
##########################################################################################
##########################################################################################
# BEDROOM 2 LIGHTSWITCH
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-bedrm2lights"
friendly_name: "Bedroom 2 Lightswitch (2)"
description_comment: "Bedroom 2 Main Lightswitch using a Zemismart KS-811 Double Push Button. Main Lights (1), Desk Lights (2)"
device_area: "Bedroom 2" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-bedrm2lights_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "bedroom2-lights"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Switch Naming
switch_1_name: "Main Lights"
switch_2_name: "Desk Lights"
#switch_3_name: "Nil"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,381 @@
##########################################################################################
##########################################################################################
# CEILING FAN - BEDROOM 3
# - 3 speed fan using Sonoff IFan02
# - Also, single Light Output
#
# Controlled by a Sonoff IFan 02
#
# V1.0 - 2025-07-21 First Setup (and replacement of Tasmota)
#
# NOTES:
# Command the fan with MQTT
# ${mqtt_local_command_full_topic}/speed/set 1,2,3,0,+,-
# ${mqtt_local_command_full_topic}/light/set ON,OFF
# Status of the fan is in MQTT
# ${mqtt_local_command_full_topic}/speed/state 1,2,3,0
# ${mqtt_local_command_full_topic}/light/state ON,OFF
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-bedrm3ceilingfan"
friendly_name: "Bedroom 3 Ceiling Fan"
description_comment: "3 Speed Overhead Ceiling Fan Bedroom 3 :: Sonoff ifan02"
device_area: "Bedroom 3" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff ifan02" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-bedrm3ceilingfan_ip # unfortunately you can't use substitutions inside secrets names
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
mqtt_device_name: "bedroom3-ceilingfan"
mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
# Button Naming & Icons
# Switch/Relay Naming & Icons
#relay_icon: "mdi:lightbulb-group"
light_1_name: "Fan Light"
switch_2_name: "Fan 2 Relay"
switch_3_name: "Fan 3 Relay"
switch_4_name: "Fan 4 Relay"
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
on_boot: # This weird toggle thing makes sure the states get restored on reboot
priority : -100
then:
- delay: 200ms
#- fan.toggle: ifan02_fan
#- fan.toggle: ifan02_fan
#- delay: 200ms
#- light.toggle: ifan02_light
#- delay: 1ms
#- light.toggle: ifan02_light
#- delay: 1s
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: !lambda 'return to_string(id(speed_value));'
# platformio_options:
# build_flags:
# - "-Os" # optimize for size
# - "-Wl,--gc-sections" # drop unused code/data
# - "-fno-exceptions" # strip C++ exceptaions
# - "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: True # Disabling will make the build file smaller (and it is still available via static IP)
##########################################################################################
# GLOBAL VARIABLES
##########################################################################################
globals:
- id: speed_value
type: int
restore_value: yes
initial_value: '0'
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" # 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, Serial control)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Light control
- topic: "${mqtt_local_command_topic}/light/set"
payload: "ON"
then:
- light.turn_on: ifan02_light
- topic: "${mqtt_local_command_topic}/light/set"
payload: "OFF"
then:
- light.turn_off: ifan02_light
# Fan speed control (03) + ramp up/down (+ / -)
- topic: "${mqtt_local_command_topic}/speed/set"
then:
# 1) Compute new speed_value based on payload
- lambda: |-
std::string s = x;
int val = id(speed_value);
if (s == "+") {
val++;
} else if (s == "-") {
val--;
} else if (s.size() && isdigit(s[0])) {
val = atoi(s.c_str());
} else {
ESP_LOGE("ifan02", "Invalid speed payload: '%s'", x.c_str());
return;
}
// Clamp between 0 and 3
if (val < 0) val = 0;
if (val > 3) val = 3;
id(speed_value) = val;
# 2) Drive the fan based on the new value
- if:
condition:
lambda: 'return id(speed_value) == 0;'
then:
- fan.turn_off: ifan02_fan
else:
- fan.turn_on:
id: ifan02_fan
speed: !lambda 'return id(speed_value);'
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff ifan02 has relays, but they don't need to be shown in HA because
# the speed of the fan is the important part (and combinations of the relays give that)
##########################################################################################
switch:
- platform: gpio
name: "${switch_3_name}"
pin: GPIO4
id: fan3sw
restore_mode: RESTORE_DEFAULT_OFF
internal: true
- platform: gpio
name: "${switch_2_name}"
pin: GPIO5
id: fan2sw
restore_mode: RESTORE_DEFAULT_OFF
internal: true
- platform: gpio
name: "${switch_4_name}"
pin: GPIO15
id: fan4sw
restore_mode: RESTORE_DEFAULT_OFF
internal: true
##########################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
##########################################################################################
select:
- platform: template
name: "Bedroom 2 Fan Speed"
id: bedroom2_fan_speed_select
options:
- "Off"
- "Low"
- "Medium"
- "High"
update_interval: 1s
lambda: |-
switch (id(speed_value)) {
case 1: return esphome::optional<std::string>("Low");
case 2: return esphome::optional<std::string>("Medium");
case 3: return esphome::optional<std::string>("High");
default: return esphome::optional<std::string>("Off");
}
set_action:
# OFF
- if:
condition:
lambda: 'return x == "Off";'
then:
- lambda: |-
id(speed_value) = 0;
- fan.turn_off: ifan02_fan
# LOW (1)
- if:
condition:
lambda: 'return x == "Low";'
then:
- lambda: |-
id(speed_value) = 1;
- fan.turn_on:
id: ifan02_fan
speed: 1
# MEDIUM (2)
- if:
condition:
lambda: 'return x == "Medium";'
then:
- lambda: |-
id(speed_value) = 2;
- fan.turn_on:
id: ifan02_fan
speed: 2
# HIGH (3)
- if:
condition:
lambda: 'return x == "High";'
then:
- lambda: |-
id(speed_value) = 3;
- fan.turn_on:
id: ifan02_fan
speed: 3
################################################################################
# TEMPLATE OUTPUTS: drive the real relays when the states change
################################################################################
output:
- platform: gpio
pin: GPIO12
id: lightrelay
- platform: template
type: float
id: fan_decode
write_action:
- lambda: |-
if (state < 0.25) {
id(fan2sw).turn_off();
id(fan3sw).turn_off();
id(fan4sw).turn_off();
}
else if (state < 0.5) {
id(fan2sw).turn_on();
id(fan3sw).turn_off();
id(fan4sw).turn_off();
}
else if (state < 0.75) {
id(fan2sw).turn_on();
id(fan3sw).turn_on();
id(fan4sw).turn_off();
}
else {
id(fan2sw).turn_on();
id(fan3sw).turn_off();
id(fan4sw).turn_on();
}
##########################################################################################
# LIGHT COMPONENT
# https://esphome.io/components/light/
##########################################################################################
light:
- platform: binary
name: "${light_1_name}"
output: lightrelay
restore_mode: RESTORE_DEFAULT_OFF
id: ifan02_light
# publish status updates when the light turns on/off:
on_turn_on:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "ON"
on_turn_off:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "OFF"
##########################################################################################
# FAN COMPONENT
# https://esphome.io/components/fan/index.html
##########################################################################################
fan:
- platform: speed
output: fan_decode
name: "Fan"
id: ifan02_fan
speed_count: 3
restore_mode: RESTORE_DEFAULT_OFF
# whenever you explicitly set a speed (13):
on_speed_set:
- lambda: |-
id(speed_value) = x;
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: !lambda 'return to_string(id(speed_value));'
# whenever the fan goes fully off:
on_turn_off:
- lambda: |-
id(speed_value) = 0;
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: "0"
# whenever the fan goes off→on (e.g. via HAs Fan switch):
on_turn_on:
- lambda: |-
// bump 0→1 if we were off
if (id(speed_value) == 0) id(speed_value) = 1;
- fan.turn_on:
id: ifan02_fan
speed: !lambda 'return id(speed_value);'
- mqtt.publish:
topic: "${mqtt_local_status_topic}/speed/state"
payload: !lambda 'return to_string(id(speed_value));'

View File

@@ -0,0 +1,265 @@
##########################################################################################
##########################################################################################
# BEDROOM 3 FAN SWITCH
#
# Controlled by a Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# V1.0 - 2025-07-23 First Setup (and replacement of Tasmota)
#
# NOTES
# - Switch for Ceiling Fan
# - 3 Switches, Up, Down and Off
# - Remote commands to the fan ${mqtt_remote_command_full_topic}/speed/set 1,2,3,0,+,-
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-bedrm3fanswitch"
friendly_name: "Bedroom 3 Fan Wall Switch (3)"
description_comment: "Switch for Bedroom 3 Ceiling Fan using Zemismart KS-811 Triple Push Button. Speed Up (1), Speed Down (2), Fan Off (3)"
device_area: "Bedroom 3" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811-3 (Triple)" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
#entity_prefix: "Main Bathroom" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-bedrm3fanswitch_ip
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
#relay_icon: "mdi:heating-coil"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "bedroom2-ceilingfan-switch"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
mqtt_remote_device_name: "bedroom3-ceilingfan"
mqtt_remote_device_command_topic: "${mqtt_local_command_main_topic}/${mqtt_remote_device_name}/speed/set"
mqtt_remote_device_command1: "+"
mqtt_remote_device_command2: "-"
mqtt_remote_device_command3: "0"
mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_remote_device_name}/speed/state" # Topic we will use to view status locally without HA
# Button Naming & Icons
# Switch/Relay Naming & Icons
#relay_icon: "mdi:heating-coil"
switch_1_name: "Fan Speed Up" # This is virtual only, no power connected to 1st relay
switch_2_name: "Fan Speed Down" # This is virtual only, no power connected to 2nd relay
switch_3_name: "Fan Off" # This is virtual only, no power connected to 3rd relay
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} #Appears on the esphome page in HA
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptions
- "-fno-rtti" # strip C++ RTTI
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Status LED for KS-811 is GPIO02
##########################################################################################
status_led:
pin:
number: GPIO02
inverted: yes
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Buttons for KS-811-3 are GPIO16, GPIO05, GPIO04
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: "${mqtt_remote_device_command1}"
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: "${mqtt_remote_device_command2}"
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: ${switch_3_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: "${mqtt_remote_device_command3}"
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Relays for KS-811-3 are GPIO13, GPIO12, GPIO14
##########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: ${switch_3_name}"
pin: GPIO14
id: Relay_3
mqtt:
on_message:
- topic: "${mqtt_local_status_topic}"
then:
- lambda: |-
int val = atoi(x.c_str());
ESP_LOGI("fan_switch", "Received requested speed: %d", val);
// Desired states
bool r1 = false;
bool r2 = false;
bool r3 = false;
switch (val) {
case 1:
r3 = true;
break;
case 2:
r2 = true;
r3 = true;
break;
case 3:
r1 = true;
r2 = true;
r3 = true;
break;
case 0:
default:
// all remain false
break;
}
// Only change relays if necessary
if (id(Relay_1).state != r1) {
if (r1) {
id(Relay_1).turn_on();
} else {
id(Relay_1).turn_off();
}
}
if (id(Relay_2).state != r2) {
if (r2) {
id(Relay_2).turn_on();
} else {
id(Relay_2).turn_off();
}
}
if (id(Relay_3).state != r3) {
if (r3) {
id(Relay_3).turn_on();
} else {
id(Relay_3).turn_off();
}
}

View File

@@ -0,0 +1,238 @@
##########################################################################################
##########################################################################################
# BEDROOM 3 LIGHTSWITCH
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-bedrm3lights"
friendly_name: "Bedroom 3 Lightswitch (2)"
description_comment: "Bedroom 3 Main Lightswitch using a Zemismart KS-811 Double Push Button. Main Lights (1-Virtual), All Lights Off (2-Virtual)"
device_area: "Bedroom 3" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-bedrm3lights_ip
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
mqtt_device_name: "bedroom3-lightswitch"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
mqtt_remote_device_name: "bedroom3-ceilingfan"
mqtt_remote_device_command_topic1: "${mqtt_local_command_main_topic}/${mqtt_remote_device_name}/light/set"
mqtt_remote_status_topic1: "${mqtt_local_status_main_topic}/${mqtt_remote_device_name}/light/state" # Topic we will use to view status of remote without HA
mqtt_remote_device_command_ON: "ON"
mqtt_remote_device_command_OFF: "OFF"
mqtt_remote_device_command_topic2: "${mqtt_local_command_main_topic}/${mqtt_remote_device_name}/speed/set"
#mqtt_remote_device_command1: "+"
#mqtt_remote_device_command2: "-"
mqtt_remote_device_command_0: "0"
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_remote_device_name}/speed/state" # Topic we will use to view status locally without HA
# Button Naming & Icons
# Switch Naming
switch_1_name: "Main Lights" # Nothing physically connected to this output. Lights are physically on IFan02 light output
switch_2_name: "All Lights Off" # Nothing physically connected to this output
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#GLobals
globals:
- id: btn1_last_ms
type: uint32_t
restore_value: no
initial_value: '0'
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
id: button_1
pin:
number: GPIO16
mode:
input: true
pullup: false # set to true if you need an internal pull-up
inverted: true
name: "Button 1: ${switch_1_name}"
filters:
- delayed_on: 30ms
- delayed_off: 30ms
on_press:
# cooldown: 250 ms between publishes
- if:
condition:
lambda: |-
return (millis() - id(btn1_last_ms)) > 250;
then:
- lambda: |-
id(btn1_last_ms) = millis();
- if:
condition:
switch.is_on: Relay_1
then:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic1}"
payload: "${mqtt_remote_device_command_OFF}"
qos: 0
retain: false
else:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic1}"
payload: "${mqtt_remote_device_command_ON}"
qos: 0
retain: false
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.turn_off: Relay_2
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic1}"
payload: "${mqtt_remote_device_command_OFF}"
qos: 0
retain: false
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic2}"
payload: "${mqtt_remote_device_command_0}"
qos: 0
retain: false
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Switch control. Turn on/off relay if remote device switched on/off
- topic: "${mqtt_remote_status_topic1}"
payload: "${mqtt_remote_device_command_ON}"
then:
- switch.turn_on: Relay_1
- topic: "${mqtt_remote_status_topic1}"
payload: "${mqtt_remote_device_command_OFF}"
then:
- switch.turn_off: Relay_1

View File

@@ -0,0 +1,248 @@
##########################################################################################
##########################################################################################
# CENTRAL STAIRS - BOTTOM LIGHTSWITCH
# V3.7 2025-09-24 upload to this device
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# - Light switch at top of the stairs, only the footer lights physically connected.
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-centralstairs-bottom"
friendly_name: "Central Stair Lightswitch - Bottom (2)"
description_comment: "Central Stair Lightswitch - Bottom, using Zemismart KS-811 Double Push Button. Main Stair Lights (1), Stair Footer Lights (2)"
device_area: "" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.7" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets (unfortunately you can't use substitutions inside secrets names)
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-centralstairs-bottom_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
mqtt_local_device_name: "stair-mainlights-lower"
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}/set" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}/state" # Topic we will use to view status locally without HA
mqtt_local_device_command_ON: "ON"
mqtt_local_device_command_OFF: "OFF"
# MQTT REMOTE Controls
mqtt_remote_device1_name: "stair-footerlights"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/set"
mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}/state"
#mqtt_remote_device2_name: "stair-mainlights-upper"
#mqtt_remote_device2_command_topic: "${mqtt_local_command_main_topic}/${mqtt_remote_device2_name}/relay3/set"
#mqtt_remote_device2_status_topic: "${mqtt_local_status_main_topic}/${mqtt_remote_device2_name}/relay3/state"
mqtt_remote_device_command_ON: "ON"
mqtt_remote_device_command_OFF: "OFF"
# Switch Naming
switch_1_name: "Main Stair Lights (Lower)" # Physical, but just the lower lights below the media cabinet
switch_2_name: "Stair Footer Lights" # virtual only, nothing connected to this output
#switch_3_name: "Nil"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_connect:
then:
- if:
condition:
lambda: 'return id(Relay_1).state;'
then:
- mqtt.publish:
topic: "${mqtt_local_status_topic}"
payload: "${mqtt_local_device_command_ON}"
qos: 0
retain: false
else:
- mqtt.publish:
topic: "${mqtt_local_status_topic}"
payload: "${mqtt_local_device_command_OFF}"
qos: 0
retain: false
on_message:
# Light control to turn on relay 1
- topic: "${mqtt_local_command_topic}"
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: Relay_1
# Light control to turn off relay 1
- topic: "${mqtt_local_command_topic}"
payload: "${mqtt_local_device_command_OFF}"
then:
- switch.turn_off: Relay_1
- topic: "${mqtt_remote_device1_status_topic}"
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: Relay_2
# Light control to turn off relay 2
- topic: "${mqtt_remote_device1_status_topic}"
payload: "${mqtt_local_device_command_OFF}"
then:
- switch.turn_off: Relay_2
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
filters:
- delayed_on: 30ms
- delayed_off: 30ms
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
filters:
- delayed_on: 30ms
- delayed_off: 30ms
name: "Button 2: ${switch_2_name}"
on_press:
# Toggle the remote Footer Lights via COMMAND topic, based on our mirrored state
- if:
condition:
lambda: 'return id(Relay_2).state;'
then:
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device_command_OFF}"
retain: false
else:
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device_command_ON}"
retain: false
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
on_turn_on:
- mqtt.publish:
topic: "${mqtt_local_status_topic}"
payload: "${mqtt_local_device_command_ON}"
retain: false
on_turn_off:
- mqtt.publish:
topic: "${mqtt_local_status_topic}"
payload: "${mqtt_local_device_command_OFF}"
retain: false
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# No mqtt.publish here (this is a mirror only)

View File

@@ -0,0 +1,232 @@
##########################################################################################
##########################################################################################
# CENTRAL STAIRS - TOP LIGHTSWITCH
# V3.7 2025-09-24 upload to this device
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# - Light switch at top of the stairs
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-centralstairs-top"
friendly_name: "Central Stair Lightswitch - Top (2)"
description_comment: "Central Stair Lightswitch - Top, using Zemismart KS-811 Double Push Button. Main Stair Lights (1), Stair Footer Lights (2)"
device_area: "" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.7" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets (unfortunately you can't use substitutions inside secrets names)
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-centralstairs-top_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
mqtt_local_device_name: "stair-footerlights"
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}/set" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}/state" # Topic we will use to view status locally without HA
mqtt_local_device_command_ON: "ON"
mqtt_local_device_command_OFF: "OFF"
# MQTT REMOTE Controls
mqtt_remote_device1_name: "stair-mainlights-lower"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/set"
mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}/state"
#mqtt_remote_device2_name: "stair-mainlights-upper"
#mqtt_remote_device2_command_topic: "${mqtt_local_command_main_topic}/${mqtt_remote_device2_name}/relay3/set"
#mqtt_remote_device2_status_topic: "${mqtt_local_status_main_topic}/${mqtt_remote_device2_name}/relay3/state"
mqtt_remote_device_command_ON: "ON"
mqtt_remote_device_command_OFF: "OFF"
# Switch Naming
switch_1_name: "Main Stair Lights" # virtual only, nothing connected to this output
switch_2_name: "Stair Footer Lights"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Light control to turn on relay 2
- topic: "${mqtt_local_command_topic}"
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: Relay_2
# Light control to turn off relay 2
- topic: "${mqtt_local_command_topic}"
payload: "${mqtt_local_device_command_OFF}"
then:
- switch.turn_off: Relay_2
- topic: "${mqtt_remote_device1_status_topic}"
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: Relay_1
# Light control to turn off relay 2
- topic: "${mqtt_remote_device1_status_topic}"
payload: "${mqtt_local_device_command_OFF}"
then:
- switch.turn_off: Relay_1
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
filters:
- delayed_on: 30ms
- delayed_off: 30ms
name: "Button 1: ${switch_1_name}"
on_press:
# Toggle the remote Main Stair Lights (lower) via COMMAND topic, based on our mirrored state
- if:
condition:
lambda: 'return id(Relay_1).state;'
then:
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device_command_OFF}"
retain: false
else:
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device_command_ON}"
retain: false
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
filters:
- delayed_on: 30ms
- delayed_off: 30ms
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
# No mqtt.publish here (this is a mirror only)
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
on_turn_on:
- mqtt.publish:
topic: "${mqtt_local_status_topic}"
payload: "${mqtt_local_device_command_ON}"
retain: false
on_turn_off:
- mqtt.publish:
topic: "${mqtt_local_status_topic}"
payload: "${mqtt_local_device_command_OFF}"
retain: false

View File

@@ -0,0 +1,755 @@
##########################################################################################
##########################################################################################
# SMART DIN POWER MONITOR AND RELAY (Originally CBU)
# V1.0 2025-08-26 Initial Version
##########################################################################################
# Ex Tuya Smart Relay
# Uses this chip for power monitoring BL0942
# Replacement https://templates.blakadder.com/ESP8685-WROOM-06.html
#
# NOTES
# - DIN rail power monitor and relay
# - Relay is a pulse to latch ON (GPIO04) and pulse to latch OFF (GPIO05)
# - Has an "is it running" template sensor with a minimum wattage value of ${min_power_to_state_running_default}
# - The relay opens on over-current at ${max_current_trip_default} (Trip Active sensor set). Trip is latched
# (relay stays OFF) until manually reset via the "Clear Trip" button or you toggle the relay ON again.
# - The status_led flashes with a speed depending on current (faster near limit).
# - The status_led is ON when the relay has tripped
# - Inrush debounce: current must remain above the trip threshold for ${overcurrent_debounce_ms} ms
# before the trip occurs (helps ignore short inrush spikes).
# - Energy totals calculated on-device:
# * Last Hour Energy (kWh) - snapshot of the previous completed hour
# * Today / Yesterday (kWh) - resets at local midnight, survives reboots
# * This Week / Last Week (kWh) - resets at Monday 00:00 local time, survives reboots
# * This Month / Last Month (kWh) - resets at 1st of month 00:00 local time, survives reboots
# - For rolling 24h Min/Max Power, use HA "Statistics" helper instead of on-device buffers.
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "dinpowermonitor-pmb"
friendly_name: "Din Power"
description_comment: "DIN Rail mounted current monitor and relay, with current based (software) trip"
device_area: "Hallway" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Generic ESP32.ESP8685-WROOM-06" # Project details, a dot separates the HA columns
project_version: "v1.0" # Project version allows checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you cannot use substitutions inside secrets names
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-dinpowermonitor-pmb_ip
# Device General Settings
log_level: "ERROR" # NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for general sensors etc
# Device Specific Defaults (used to seed HA-tunable numbers)
relay_1_name: "Relay"
button_1_name: "Toggle Button"
status_led_name: "Power Active"
min_power_to_state_running_default: 4 # Watts, initial value only
max_current_trip_default: 6 # Amps, initial value only
led_flash_slow_ms: 1000 # slow flash period when just running
led_flash_fast_ms: 100 # very fast flash period near trip
overcurrent_debounce_ms: 300 # Over-current inrush debounce (ms)
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
common_webportal: !include
file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the ESPHome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: -100
then:
# Restore "Last Trip" text from persisted epoch (if any)
- lambda: |-
if (id(last_trip_epoch) > 0) {
auto t = time::ESPTime::from_epoch_local(id(last_trip_epoch));
char buf[24];
t.strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S");
id(ts_last_trip).publish_state(buf);
} else {
id(ts_last_trip).publish_state("Never");
}
# Apply Power Restore Mode selector (Always ON / Always OFF / Previous State)
- lambda: |-
std::string mode = id(sel_restore_mode).state;
if (mode == "Always ON") {
id(relay_virtual).turn_on();
} else if (mode == "Always OFF") {
id(relay_virtual).turn_off();
} else {
// "Previous State" -> do nothing (template switch will keep last or default OFF)
}
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp32:
board: esp32-c3-devkitm-1
variant: esp32c3
framework:
type: esp-idf
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: ${log_level}
##########################################################################################
# STATUS LED (BLUE) - low = ON per mapping
# https://esphome.io/components/status_led.html
##########################################################################################
status_led:
pin:
number: GPIO8
inverted: true
##########################################################################################
# UART BUS
# https://esphome.io/components/uart/
##########################################################################################
uart:
# for BL0942
id: bl_uart
rx_pin: GPIO19 # BL0942 TXD -> MCU RX
tx_pin: GPIO18 # BL0942 RXD -> MCU TX
baud_rate: 4800
parity: NONE
stop_bits: 1
##########################################################################################
# GLOBALS
# https://esphome.io/components/globals.html
##########################################################################################
globals:
# Used for LED blink period calculation (in milliseconds)
- id: _blink_period_ms
type: int
restore_value: no
initial_value: "500"
# Last trip time, saved across reboots.
- id: last_trip_epoch
type: uint32_t
restore_value: yes
initial_value: "0"
# Debounce start timestamp (ms since boot) for over-current detection
- id: _oc_start_ms
type: uint32_t
restore_value: no
initial_value: "0"
# ---- ENERGY ACCUMULATION & PERIOD SNAPSHOTS ----
- id: g_last_ms # millis() timestamp of last integration step
type: uint32_t
restore_value: no
initial_value: "0"
# Current-period totals (persisted)
- id: g_hour_kwh
type: float
restore_value: yes
initial_value: "0.0"
- id: g_today_kwh
type: float
restore_value: yes
initial_value: "0.0"
- id: g_week_kwh
type: float
restore_value: yes
initial_value: "0.0"
- id: g_month_kwh
type: float
restore_value: yes
initial_value: "0.0"
# Last-period snapshots (persisted)
- id: g_last_hour_kwh
type: float
restore_value: yes
initial_value: "0.0"
- id: g_yesterday_kwh
type: float
restore_value: yes
initial_value: "0.0"
- id: g_last_week_kwh
type: float
restore_value: yes
initial_value: "0.0"
- id: g_last_month_kwh
type: float
restore_value: yes
initial_value: "0.0"
# Boundary trackers (don't persist)
- id: g_last_hour_seen
type: int
restore_value: no
initial_value: "-1"
- id: g_last_doy_seen
type: int
restore_value: no
initial_value: "-1"
- id: g_last_wday_seen
type: int
restore_value: no
initial_value: "-1"
- id: g_last_month_seen
type: int
restore_value: no
initial_value: "-1"
##########################################################################################
# SENSORS
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
- platform: bl0942
uart_id: bl_uart
update_interval: 250ms
voltage:
name: "${friendly_name} Voltage"
id: pm_voltage
accuracy_decimals: 1
unit_of_measurement: "V"
device_class: voltage
state_class: measurement
current:
name: "${friendly_name} Current"
id: pm_current
accuracy_decimals: 3
unit_of_measurement: "A"
device_class: current
state_class: measurement
on_value:
then:
- lambda: |-
// OVER-CURRENT TRIP HANDLER with inrush debounce
// Trip when current > n_max_trip and relay is ON, but only if it stays above
// for ${overcurrent_debounce_ms} ms (to ignore short inrush spikes).
if (id(bs_tripped).state) {
// Already tripped: reset debounce tracker and do nothing
id(_oc_start_ms) = 0;
} else if (id(relay_virtual).state && x > id(n_max_trip).state) {
// Over threshold while relay is ON
if (id(_oc_start_ms) == 0) {
id(_oc_start_ms) = millis(); // start debounce window
}
uint32_t elapsed = millis() - id(_oc_start_ms);
if (elapsed >= ${overcurrent_debounce_ms}) {
ESP_LOGI("trip",
"Overcurrent trip: %.3f A > %.3f A for %u ms (>= %u ms), opening relay",
x, id(n_max_trip).state, (unsigned) elapsed, (unsigned) ${overcurrent_debounce_ms});
// Mark trip, stop blinker FIRST
id(bs_tripped).publish_state(true);
id(led_blinker).stop();
// Open latching relay (template handles OFF coil pulse)
id(relay_virtual).turn_off();
// Force LED solid ON to indicate trip
auto call = id(led).turn_on();
call.perform();
// Time-stamp "Last Trip" using SNTP time (YYYY-MM-DD HH:MM:SS) and persist
auto now_clk = id(sntp_time).now();
if (now_clk.is_valid()) {
char buf[24];
now_clk.strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S");
id(ts_last_trip).publish_state(buf);
id(last_trip_epoch) = (uint32_t) now_clk.timestamp;
} else {
id(ts_last_trip).publish_state("unknown");
}
// Reset debounce tracker
id(_oc_start_ms) = 0;
}
} else {
// Below threshold or relay OFF: reset debounce tracker
id(_oc_start_ms) = 0;
}
// ------------------------------------------------------------------
// ENERGY ACCUMULATION (kWh) + PERIOD ROLLOVERS (hour/day/week/month)
// ------------------------------------------------------------------
// Integrate W over time using millis() -> kWh
uint32_t now_ms = millis();
if (id(g_last_ms) == 0) {
id(g_last_ms) = now_ms; // first sample since boot
} else {
uint32_t dt_ms = now_ms - id(g_last_ms);
id(g_last_ms) = now_ms;
float p_w = (isnan(x) || x < 0.0f) ? 0.0f : x; // sanitize power
float inc_kwh = p_w * (dt_ms / 3600000.0f); // W * hours -> kWh
id(g_hour_kwh) += inc_kwh;
id(g_today_kwh) += inc_kwh;
id(g_week_kwh) += inc_kwh; // Week rolls at Monday 00:00 local time
id(g_month_kwh) += inc_kwh;
}
// Use SNTP time for clean boundary changes
auto now = id(sntp_time).now();
if (now.is_valid()) {
// Hour rollover: when hour changes
if (id(g_last_hour_seen) != now.hour) {
if (id(g_last_hour_seen) != -1) {
id(g_last_hour_kwh) = id(g_hour_kwh);
id(s_last_hour_kwh).publish_state(id(g_last_hour_kwh));
}
id(g_hour_kwh) = 0.0f;
id(g_last_hour_seen) = now.hour;
}
// Day rollover (midnight local)
if (id(g_last_doy_seen) != now.day_of_year) {
if (id(g_last_doy_seen) != -1) {
id(g_yesterday_kwh) = id(g_today_kwh);
id(s_yesterday_kwh).publish_state(id(g_yesterday_kwh));
}
id(g_today_kwh) = 0.0f;
id(g_last_doy_seen) = now.day_of_year;
}
// Week rollover: Monday 00:00 local time (i.e., after Sunday)
// ESPHome day_of_week: Monday=1 ... Sunday=7
if (now.day_of_week == 1 && now.hour == 0 && id(g_last_wday_seen) != 1) {
id(g_last_week_kwh) = id(g_week_kwh);
id(s_last_week_kwh).publish_state(id(g_last_week_kwh));
id(g_week_kwh) = 0.0f;
}
id(g_last_wday_seen) = now.day_of_week;
// Month rollover: first day of month at 00:00
if (id(g_last_month_seen) != now.month) {
if (id(g_last_month_seen) != -1 && now.day_of_month == 1 && now.hour == 0) {
id(g_last_month_kwh) = id(g_month_kwh);
id(s_last_month_kwh).publish_state(id(g_last_month_kwh));
id(g_month_kwh) = 0.0f;
}
id(g_last_month_seen) = now.month;
}
}
power:
name: "${friendly_name} Power"
id: pm_power
accuracy_decimals: 1
unit_of_measurement: "W"
device_class: power
state_class: measurement
energy:
name: "${friendly_name} Energy"
id: pm_energy
unit_of_measurement: "Wh"
device_class: energy
state_class: total_increasing
frequency:
name: "${friendly_name} Frequency"
id: pm_freq
unit_of_measurement: "Hz"
device_class: frequency
state_class: measurement
# Optional calibration references (uncomment and tune if needed)
# voltage_reference: 15968
# current_reference: 124180
# power_reference: 309.1
# energy_reference: 2653
##########################################################################
# ENERGY TOTALS (kWh) EXPOSED TO HA
# These read the persisted globals; they update periodically.
##########################################################################
- platform: template
name: "${friendly_name} Last Hour Energy"
id: s_last_hour_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_last_hour_kwh);
- platform: template
name: "${friendly_name} Today Energy"
id: s_today_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_today_kwh);
- platform: template
name: "${friendly_name} Yesterday Energy"
id: s_yesterday_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_yesterday_kwh);
- platform: template
name: "${friendly_name} This Week Energy"
id: s_week_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_week_kwh);
- platform: template
name: "${friendly_name} Last Week Energy"
id: s_last_week_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_last_week_kwh);
- platform: template
name: "${friendly_name} This Month Energy"
id: s_month_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_month_kwh);
- platform: template
name: "${friendly_name} Last Month Energy"
id: s_last_month_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total
accuracy_decimals: 3
update_interval: 30s
lambda: |-
return id(g_last_month_kwh);
##########################################################################################
# TEXT SENSOR COMPONENT
# https://esphome.io/components/text_sensor/
##########################################################################################
text_sensor:
# Holds the date/time of last trip (string with seconds)
- platform: template
name: "${friendly_name} Last Trip"
id: ts_last_trip
icon: "mdi:clock-alert"
entity_category: diagnostic
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
# https://esphome.io/components/binary_sensor/template.html
##########################################################################################
binary_sensor:
# "Is Running" template sensor
- platform: template
id: bs_running
name: "${friendly_name} Running"
lambda: |-
if (isnan(id(pm_power).state)) {
return false;
} else if (id(pm_power).state > id(n_min_power).state) {
return true;
} else {
return false;
}
filters:
- delayed_off: 15s
on_press:
then:
- if:
condition:
and:
- switch.is_on: relay_virtual
- lambda: 'return !id(bs_tripped).state;'
then:
- script.execute: led_blinker
on_release:
then:
- script.stop: led_blinker
- if:
condition:
lambda: 'return !id(bs_tripped).state;'
then:
- light.turn_off: led
# Trip flag exposed to HA; set true on over-current. Cleared when relay is turned ON or via Reset button.
- platform: template
id: bs_tripped
name: "${friendly_name} Trip Active"
device_class: problem
# On-board button (P17 -> IO9) pulls low when pressed
- platform: gpio
pin:
number: GPIO9
mode: INPUT_PULLUP
inverted: true
name: "${button_1_name}"
on_press:
- switch.toggle: relay_virtual
##########################################################################################
# NUMBER COMPONENT
# https://esphome.io/components/number/
##########################################################################################
# HA-TUNABLE THRESHOLDS (NUMBER ENTITIES)
# https://esphome.io/components/number/template.html
##########################################################################################
number:
- platform: template
name: "${friendly_name} Min Power Running"
id: n_min_power
optimistic: true
restore_value: true
initial_value: ${min_power_to_state_running_default}
min_value: 1
max_value: 100
step: 1
unit_of_measurement: "W"
icon: "mdi:flash-outline"
- platform: template
name: "${friendly_name} Max Current Trip"
id: n_max_trip
optimistic: true
restore_value: true
initial_value: ${max_current_trip_default}
min_value: 0
max_value: 10
step: 0.2
unit_of_measurement: "A"
icon: "mdi:current-ac"
##########################################################################################
# BUTTON COMPONENT
# Reset the trip latch from HA (does not close the relay automatically)
# https://esphome.io/components/button/
##########################################################################################
button:
- platform: template
name: "${friendly_name} Clear Trip"
id: btn_clear_trip
icon: "mdi:alert-remove-outline"
on_press:
- logger.log: "Trip reset requested (clearing Trip Active and LED state)"
- binary_sensor.template.publish:
id: bs_tripped
state: false
- lambda: |-
// Clear any pending debounce
id(_oc_start_ms) = 0;
- if:
condition:
and:
- switch.is_on: relay_virtual
- binary_sensor.is_on: bs_running
then:
- script.execute: led_blinker
else:
- light.turn_off: led
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# LATCHING RELAY CONTROL (two coils: ON and OFF)
# P24 -> IO5 (close), P26 -> IO4 (open) per mapping.
# Two hidden GPIO pulse switches to drive the coils.
# User-facing switch sends a short pulse to the appropriate coil.
##########################################################################################
switch:
# Hidden coil driver: ON pulse (GPIO5)
- platform: gpio
id: relay_on_coil
pin:
number: GPIO5
inverted: false
restore_mode: ALWAYS_OFF
# Hidden coil driver: OFF pulse (GPIO4)
- platform: gpio
id: relay_off_coil
pin:
number: GPIO4
inverted: false
restore_mode: ALWAYS_OFF
# User-facing virtual switch
- platform: template
id: relay_virtual
name: "${relay_1_name}"
optimistic: true
# Use default restore behavior; "Previous State" is honored by doing nothing in on_boot.
# Set a safe baseline here (default off if no previous state).
restore_mode: RESTORE_DEFAULT_OFF
turn_on_action:
- logger.log: "Relay ON: pulsing ON coil"
- binary_sensor.template.publish:
id: bs_tripped
state: false # clear trip on manual ON
- switch.turn_on: relay_on_coil
- delay: 200ms
- switch.turn_off: relay_on_coil
- if:
condition:
and:
- binary_sensor.is_on: bs_running
- lambda: 'return !id(bs_tripped).state;'
then:
- script.execute: led_blinker
turn_off_action:
- logger.log: "Relay OFF: pulsing OFF coil"
- switch.turn_on: relay_off_coil
- delay: 200ms
- switch.turn_off: relay_off_coil
- script.stop: led_blinker
- if:
condition:
lambda: 'return !id(bs_tripped).state;'
then:
- light.turn_off: led
##########################################################################################
# OUTPUT COMPONENT
# https://esphome.io/components/output/ledc.html
##########################################################################################
# RED LED AS LIGHT (P9 -> IO10, low = ON)
##########################################################################################
output:
- platform: ledc
id: pow_red_led_pwm
pin:
number: GPIO10
inverted: true
##########################################################################################
# LIGHT COMPONENT
# https://esphome.io/components/light/monochromatic.html
##########################################################################################
# Hidden from HA; still used internally by scripts/logic
light:
- platform: monochromatic
id: led
output: pow_red_led_pwm
internal: true
##########################################################################################
# SELECT COMPONENT
# Choose how the relay state should be restored after boot
# https://esphome.io/components/select/template.html
##########################################################################################
select:
- platform: template
name: "${friendly_name} Power Restore Mode"
id: sel_restore_mode
optimistic: true
restore_value: true
options:
- "Always ON"
- "Always OFF"
- "Previous State"
initial_option: "Previous State"
icon: "mdi:power-settings"
##########################################################################################
# SCRIPTS
# https://esphome.io/components/script.html
##########################################################################################
script:
# LED blinker: variable-speed based on current vs n_max_trip; runs only when running, relay ON, not tripped
- id: led_blinker
mode: restart
then:
- while:
condition:
and:
- switch.is_on: relay_virtual
- binary_sensor.is_on: bs_running
- lambda: 'return !id(bs_tripped).state;'
then:
# Compute and store current-period ms in _blink_period_ms
- lambda: |-
float i = isnan(id(pm_current).state) ? 0.0f : id(pm_current).state;
float maxA = id(n_max_trip).state;
if (maxA < 0.1f) maxA = 0.1f;
float f = i / maxA; // 0.0 .. 1.0
if (f < 0.0f) f = 0.0f;
if (f > 1.0f) f = 1.0f;
int slow_ms = ${led_flash_slow_ms};
int fast_ms = ${led_flash_fast_ms};
if (slow_ms < fast_ms) { int t = slow_ms; slow_ms = fast_ms; fast_ms = t; } # ensure slow >= fast
int period = slow_ms - (int)((slow_ms - fast_ms) * f);
id(_blink_period_ms) = period;
# Blink with 50% duty cycle at the computed period
- light.turn_on:
id: led
brightness: 100%
- delay: !lambda 'return (uint32_t)(id(_blink_period_ms) / 2);'
- light.turn_off: led
- delay: !lambda 'return (uint32_t)(id(_blink_period_ms) / 2);'

View File

@@ -0,0 +1,834 @@
##########################################################################################
##########################################################################################
# Title: DOWNSTAIRS KITCHEN - OVER PANTRY LEDS
# Hardware: Sinilink MOSFET Board XY-WFMS (ESP8266) — sometimes listed as “XY-VFMS”
# https://devices.esphome.io/devices/Sinilink-XY-VFMS
# Repo: https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-downstairskitchleds.yaml
#
# v1.4 - 2025-08-22 Improved power loss/on actions
# v1.3 - 2025-08-22 Added a "max on time” setting (1-48 h, 0 = no limit)
# v1.2 - 2025-08-21 Added defaults to “Device Specific Settings” in substitutions & a PWM % view
# v1.1 - 2025-08-18 Full tidy-up as general-purpose LED strip controller
# v1.0 - 2025-08-17 First setup (and replacement of Tasmota)
#
# ------------------------------------------
# DEVICE GPIO (Sinilink XY-WFMS)
# ------------------------------------------
# GPIO02 Blue LED (used for ESPHome status)
# GPIO04 MOSFET output (0 V when switched) and Red LED
# GPIO12 Toggle button
# GPIO13 Green LED (used to display fading status)
#
# ------------------------------------------
# OPERATION (as of v1.4)
# ------------------------------------------
# 1. General-purpose LED controller.
# 2. Designed for the Sinilink XY-WFMS board with a MOSFET output (claimed 5A, 5-36 V DC).
# 3. Global setting for MAX % output to extend LED life.
# 4. Minimum output setting; switches fully OFF at/below the minimum to avoid low-PWM flicker.
# 5. PWM frequency is set to 500 Hz by default. You can increase it, but higher values caused
# resets on this device. On ESP32 you can run much higher (~40 kHz).
# 6. Min/Max output settings are not exposed in Home Assistant/MQTT by default, but can be.
# With a 1 MB flash, space is tight and only minimal optimisation has been done so far.
# 7. PACKAGES include common items: network settings, diagnostic entities, MQTT, and SNTP (optional).
# 8. Default behaviour is to fade slowly up to full at power-up (so it can run with no network).
# 9. The green LED flashes while fading (different patterns for up/down). The red LED follows the
# output (it shares the MOSFET GPIO).
# 10. Fade timing scales with the configured values (proportionally when starting mid-brightness).
# 11. Useful 3D-printed case: https://cults3d.com/en/3d-model/tool/snapfit-enclosure-for-esp8266-sinilink-xy-wfms-5v-36v-mosfet-switch-module
# 12. Exposed in Home Assistant/MQTT:
# - Startup action
# - Fade Up / Fade Down / Fade Stop buttons
# - Fade Up/Down switch
# - Normal On/Off switch (quick ramp up/down)
# - Fade up/down times (0-60s)
# - Output % (pre-gamma) and PWM % (post-gamma)
# - Output Set (1-100, respects min/max)
# - Device diagnostics (from the included package)
# - Maximum 'on' time before automatic fade-down (1-48 h, 0 = no limit)
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstairskitchleds"
friendly_name: "Downstairs Kitchen LEDs"
description_comment: "Downstairs Kitchen Over Pantry LEDs :: Sinilink XY-WFMS"
device_area: "Downstairs Kitchen" # Allows the ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sinilink.XY-WFMS" # Project details
project_version: "v1.4" # Project version denotes release of the YAML file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-downstairskitchleds_ip # Unfortunately, you can't use substitutions inside secret names
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# MQTT LOCAL Controls
mqtt_local_device_name: "downstairskitchen-pantryleds"
mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_local_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_local_device_name}" # Topic we will use to view status locally without HA
mqtt_local_device_command_ON: "ON"
mqtt_local_device_command_OFF: "OFF"
# Device Specific Settings
log_level: "NONE" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (default), VERBOSE, VERY_VERBOSE
update_interval: "20s" # Update time for general sensors, etc.
led_gamma: "1.2" # Gamma from 1.2-3 is sensible to normalise the LED fading vs PWM
minimum_led_output: "1" # % If at this value or below, we'll switch it completely off
maximum_led_output: "90" # % Maximum output; it is sometimes nice to limit the output for longevity or aesthetics
max_on_default_hours: "6" # The maximum time the LEDs will be on, in case they get left on. 0 = no automatic turn-off
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
#file: common/api_common.yaml
file: common/api_common_noencryption.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
#common_sntp: !include
# file: common/sntp_common.yaml
common_general_sensors: !include
#file: common/sensors_common.yaml
file: common/sensors_common_lite.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome CORE CONFIGURATION
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}"
area: "${device_area}"
on_boot:
priority: -200
then:
# Keep the HA dropdown in sync with the stored mode
- lambda: |-
switch (id(restart_mode)) {
case 0: id(restart_action).publish_state("Fade up to full"); break;
case 1: id(restart_action).publish_state("Restore Brightness"); break;
case 2: default: id(restart_action).publish_state("Remain Off"); break;
}
# Mode 0: Fade up to full (obeys fade settings; no on/off flicker)
- if:
condition:
lambda: 'return id(restart_mode) == 0;'
then:
- lambda: 'id(ramp_switch_target_on) = true;'
- script.execute: ramp_on_script
# Mode 1: Restore Brightness using the light's normal defaults
# (uses default_transition_length and on_turn_on handlers)
- if:
condition:
lambda: 'return id(restart_mode) == 1;'
then:
- lambda: |-
float target = id(last_brightness_pct);
if (target < 0.0f) target = 0.0f;
if (target > 100.0f) target = 100.0f;
// Gently clamp to min/max caps to avoid an immediate post-on_state correction.
const float minp = (float) id(min_brightness_pct);
const float maxp = (float) id(max_brightness_pct);
if (target > 0.0f && target < minp) target = minp;
if (target > maxp) target = maxp;
id(suppress_slider_sync) = true;
if (target <= 0.0f) {
id(ramp_switch_target_on) = false;
auto call = id(mosfet_leds).make_call();
call.set_state(false);
call.set_transition_length(0);
call.perform();
} else {
id(ramp_switch_target_on) = true;
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(target / 100.0f);
// No transition_length here: use light.default_transition_length.
call.perform();
}
- delay: 300ms
- lambda: 'id(suppress_slider_sync) = false;'
# Mode 2: Remain Off (no blip; stays off)
- if:
condition:
lambda: 'return id(restart_mode) == 2;'
then:
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- lambda: 'id(ramp_switch_target_on) = false;'
- light.turn_off:
id: mosfet_leds
transition_length: 0s
platformio_options:
build_unflags:
- -flto
build_flags:
- -fno-lto
- -Wl,--gc-sections
- -ffunction-sections
- -fdata-sections
- -DNDEBUG
##########################################################################################
# ESP PLATFORM AND FRAMEWORK
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m
restore_from_flash: true # restore some values on reboot
#preferences:
# flash_write_interval: 5min
mdns:
disabled: false # Disabling will make the build file smaller (and it is still available via static IP)
##########################################################################################
# GLOBAL VARIABLES
# https://esphome.io/components/globals.html
##########################################################################################
globals:
# Minimum Brightness % for LEDs (will switch off if <=)
- id: min_brightness_pct
type: int
restore_value: true
initial_value: "${minimum_led_output}" # start/finish at X%
# Maximum Brightness % for LEDs (should never go beyond this)
- id: max_brightness_pct
type: int
restore_value: false
initial_value: "${maximum_led_output}" # hard cap; never exceed this
# The maximum time the lights will stay on, in hours. Just in case they are left on. 0 = forever
- id: max_on_hours
type: int
restore_value: true
initial_value: '${max_on_default_hours}'
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
type: int
restore_value: true
initial_value: '5000' # 5 s
# Default Fading Down Time (Selectable and will be retained)
- id: ramp_down_ms # fade-out when turned OFF
type: int
restore_value: true
initial_value: '10000' # 10 s
# Action on Restart. (0=Fade full, 1=Restore brightness, 2=Remain off)
- id: restart_mode
type: int
restore_value: true
initial_value: '0' # default = Fade Up to Full (so can be deployed with no other setup)
# Determine last fade direction.
# true when you asked the light to end up ON (Ramp Up)
# false when you asked the light to end up OFF (Ramp Down)
- id: ramp_switch_target_on
type: bool
restore_value: true
initial_value: 'false'
# Prevent jitter when adjusting the slider
- id: suppress_slider_sync
type: bool
restore_value: false
initial_value: 'false'
# actual 0..100 seen last time, for restart
- id: last_brightness_pct
type: float
restore_value: true
initial_value: '0.0'
# last published "Output Set (0-100)" integer
- id: last_set_pos
type: int
restore_value: false
initial_value: '-1'
# helper to keep blink time == transition time
- id: last_ramp_ms
type: int
restore_value: false
initial_value: '0'
##########################################################################################
# LOGGER COMPONENT
# https://esphome.io/components/logger.html
# Logs all log messages through the serial port and through MQTT topics.
##########################################################################################
logger:
level: "${log_level}" # 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, Serial control)
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Light control to ramp up
- topic: "${mqtt_local_command_topic}/light/set"
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: mosfet_ramp_switch
# Light control to ramp up
- topic: "${mqtt_local_command_topic}/light/set"
payload: "${mqtt_local_device_command_OFF}"
then:
- switch.turn_off: mosfet_ramp_switch
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
# SINILINK: Status LED Blue LED on GPIO2, active-low
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: true
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
switch:
# Ramp-aware ON/OFF for HA (asymmetric, eased; no bounce)
- platform: template
id: mosfet_ramp_switch
name: "${friendly_name} Fade Up/Down"
icon: mdi:led-strip-variant
lambda: |-
return id(ramp_switch_target_on);
turn_on_action:
- lambda: 'id(ramp_switch_target_on) = true;'
- script.stop: ramp_off_script
- script.execute: ramp_on_script
turn_off_action:
- lambda: 'id(ramp_switch_target_on) = false;'
- script.stop: ramp_on_script
- script.execute: ramp_off_script
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
# Start ramping UP (from current level)
- platform: template
id: fade_up_button
name: "${friendly_name} Fade Up"
icon: mdi:arrow-up-bold
on_press:
- lambda: |-
id(ramp_switch_target_on) = true;
id(mosfet_ramp_switch).publish_state(true); // reflect in HA immediately
- script.stop: ramp_off_script
- script.execute: ramp_on_script
# Start ramping DOWN (from current level)
- platform: template
id: fade_down_button
name: "${friendly_name} Fade Down"
icon: mdi:arrow-down-bold
on_press:
- lambda: |-
id(ramp_switch_target_on) = false;
id(mosfet_ramp_switch).publish_state(false); // reflect in HA immediately
- script.stop: ramp_on_script
- script.execute: ramp_off_script
# STOP any ramping (hold current brightness)
- platform: template
id: fade_stop_button
name: "${friendly_name} Fade Stop"
icon: mdi:pause
on_press:
# Stop any pending scripts (and their delayed turn_off)
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- script.stop: led_flash_up
- script.stop: led_flash_down
- output.turn_off: green_led_out
# Cancel the light's transition by commanding the current level with 0 ms,
# but DO NOT change the ramp switch state/flag.
- lambda: |-
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on()) {
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cv.get_brightness());
call.set_transition_length(0);
call.perform();
}
#########################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#########################################################################################
select:
- platform: template
id: restart_action
name: "${friendly_name} Restart Action"
icon: mdi:restart
optimistic: true
options:
- "Fade up to full"
- "Restore Brightness"
- "Remain Off"
initial_option: "Restore Brightness"
set_action:
- lambda: |-
if (x == "Fade up to full") {
id(restart_mode) = 0;
} else if (x == "Restore Brightness") {
id(restart_mode) = 1;
} else {
id(restart_mode) = 2;
}
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
id: btn_gpio12
name: "${friendly_name} Button"
pin:
number: GPIO12
mode:
input: true
pullup: true
inverted: true
filters:
- delayed_on: 20ms
- delayed_off: 20ms
on_press:
- if:
condition:
lambda: 'return id(ramp_switch_target_on);'
then:
# Target is currently ON → press should go OFF (start ramp-down)
- lambda: |-
id(ramp_switch_target_on) = false;
id(mosfet_ramp_switch).publish_state(false); // reflect in HA immediately
- script.stop: ramp_on_script
- script.execute: ramp_off_script
else:
# Target is currently OFF → press should go ON (start ramp-up)
- lambda: |-
id(ramp_switch_target_on) = true;
id(mosfet_ramp_switch).publish_state(true); // reflect in HA immediately
- script.stop: ramp_off_script
- script.execute: ramp_on_script
##########################################################################################
# SENSOR COMPONENT
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
- platform: template
id: mosfet_output_pct
name: "${friendly_name} Output (%)"
unit_of_measurement: "%"
icon: mdi:percent
accuracy_decimals: 0
update_interval: 250ms # consider 200ms if you want fewer updates
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
return cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f;
on_value:
then:
- lambda: |-
// Remember latest actual output (0..100) for "Restore Brightness"
id(last_brightness_pct) = x;
// If not suppressing sync, update the 0..100 slider only when its INT changes
if (!id(suppress_slider_sync)) {
float actual = x; // actual %
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f;
float pos = (actual <= 0.0f) ? 0.0f : ((actual - minp) * 100.0f / (maxp - minp));
if (pos < 0.0f) pos = 0.0f;
if (pos > 100.0f) pos = 100.0f;
int pos_i = (int) floorf(pos + 0.5f);
if (pos_i != id(last_set_pos)) {
id(last_set_pos) = pos_i;
id(led_output_set_pct).publish_state(pos_i);
}
}
- platform: template
id: mosfet_output_pwm_pct
name: "${friendly_name} Output PWM (%)"
unit_of_measurement: "%"
icon: mdi:square-wave
accuracy_decimals: 1
update_interval: 250ms
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
if (!cv.is_on()) return 0.0f;
const float lin = cv.get_brightness(); // 0..1 (linear brightness)
const float gamma = atof("${led_gamma}"); // parse substitution string → float
float pwm = powf(lin, gamma); // approx PWM duty after gamma
if (pwm < 0.0f) pwm = 0.0f;
if (pwm > 1.0f) pwm = 1.0f;
return pwm * 100.0f;
##########################################################################################
# OUTPUT COMPONENT
# https://esphome.io/components/light/index.html
##########################################################################################
# An OUTPUT can be binary (0,1) or float, which is any value between 0 and 1.
# PWM Outputs such as "ledc" are float. https://esphome.io/components/output/ledc.html
##########################################################################################
output:
- platform: esp8266_pwm
id: mosfet_pwm
pin: GPIO4
frequency: 500 Hz # high frequency to avoid audible/visible artifacts
- platform: gpio
id: green_led_out # Green LED
pin:
number: GPIO13
inverted: false
##########################################################################################
# LIGHT COMPONENT
# https://esphome.io/components/light/
##########################################################################################
light:
- platform: monochromatic
id: mosfet_leds
name: "${friendly_name}"
output: mosfet_pwm
restore_mode: RESTORE_DEFAULT_OFF
default_transition_length: 2s
icon: mdi:led-strip-variant
gamma_correct: "${led_gamma}"
on_turn_on:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_ON}"
retain: true
- lambda: 'id(ramp_switch_target_on) = true;'
- script.stop: max_on_watchdog
- if:
condition:
lambda: 'return id(max_on_hours) > 0;'
then:
- script.execute: max_on_watchdog
on_turn_off:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_OFF}"
retain: true
- lambda: 'id(ramp_switch_target_on) = false;'
- script.stop: max_on_watchdog
on_state:
- lambda: |-
const float cap = id(max_brightness_pct) / 100.0f;
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on() && cv.get_brightness() > cap + 0.001f) {
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
}
##########################################################################################
# NUMBER COMPONENT
# https://esphome.io/components/number/
##########################################################################################
number:
- platform: template
id: cfg_ramp_up_s
name: "${friendly_name} Fade Up Time (s)"
entity_category: config
unit_of_measurement: s
icon: mdi:timer-sand
mode: slider
min_value: 0
max_value: 60
step: 1
lambda: |-
return (float) id(ramp_up_ms) / 1000.0f;
set_action:
- lambda: |-
int secs = (int) floorf(x + 0.5f);
if (secs < 0) secs = 0;
if (secs > 60) secs = 60;
id(ramp_up_ms) = secs * 1000;
id(cfg_ramp_up_s).publish_state((float) secs);
- platform: template
id: cfg_ramp_down_s
name: "${friendly_name} Fade Down Time (s)"
entity_category: config
unit_of_measurement: s
icon: mdi:timer-sand-complete
mode: slider
min_value: 0
max_value: 60
step: 1
lambda: |-
return (float) id(ramp_down_ms) / 1000.0f;
set_action:
- lambda: |-
int secs = (int) floorf(x + 0.5f);
if (secs < 0) secs = 0;
if (secs > 60) secs = 60;
id(ramp_down_ms) = secs * 1000;
id(cfg_ramp_down_s).publish_state((float) secs);
- platform: template
id: led_output_set_pct
name: "${friendly_name} Output Set (0-100)"
icon: mdi:tune
mode: slider
min_value: 0
max_value: 100
step: 1
# Show current position mapped into 0..100 across [min..max]
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
float actual = cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f; // 0..100 actual
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f; // avoid div/0
if (actual <= 0.0f) return 0.0f; // when OFF, show 0
float pos = (actual - minp) * 100.0f / (maxp - minp);
if (pos < 0.0f) pos = 0.0f;
if (pos > 100.0f) pos = 100.0f;
return floorf(pos + 0.5f); // integer
set_action:
- if:
condition:
lambda: 'return x <= 0.0f;'
then:
# 0 means OFF
- lambda: 'id(suppress_slider_sync) = true;'
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- light.turn_off:
id: mosfet_leds
transition_length: 200ms
- lambda: |-
id(ramp_switch_target_on) = false;
id(led_output_set_pct).publish_state(0);
- delay: 400ms
- lambda: 'id(suppress_slider_sync) = false;'
else:
# Map 1..100 - [min..max] and set ON
- lambda: |-
id(suppress_slider_sync) = true;
float pos = x; // 0..100
if (pos < 1.0f) pos = 1.0f; // 0 is OFF
if (pos > 100.0f) pos = 100.0f;
id(led_output_set_pct).publish_state((int) floorf(pos + 0.5f));
- script.stop: ramp_off_script
- script.stop: ramp_on_script
- light.turn_on:
id: mosfet_leds
brightness: !lambda |-
float pos = id(led_output_set_pct).state; // 1..100
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f;
float out_pct = minp + (pos * (maxp - minp) / 100.0f);
if (out_pct > maxp) out_pct = maxp;
return out_pct / 100.0f;
transition_length: 250ms
- lambda: 'id(ramp_switch_target_on) = true;'
- delay: 400ms
- lambda: 'id(suppress_slider_sync) = false;'
- platform: template
id: cfg_max_on_hours
name: "${friendly_name} Max On (h)"
entity_category: config
unit_of_measurement: h
icon: mdi:timer-cog
mode: slider
min_value: 0
max_value: 48
step: 1
lambda: |-
return (float) id(max_on_hours);
set_action:
- lambda: |-
int hrs = (int) x;
if (hrs < 0) hrs = 0;
if (hrs > 48) hrs = 48;
id(max_on_hours) = hrs;
id(cfg_max_on_hours).publish_state((float) hrs);
- if:
condition:
lambda: 'return id(mosfet_leds).current_values.is_on();'
then:
- script.stop: max_on_watchdog
- if:
condition:
lambda: 'return id(max_on_hours) > 0;'
then:
- script.execute: max_on_watchdog
##########################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
# Scripts can be executed nearly anywhere in your device configuration with a single call.
##########################################################################################
script:
# Blink pattern while ramping UP: quick double-blink, pause, repeat
- id: led_flash_up
mode: restart
then:
- while:
condition:
lambda: 'return true;'
then:
- output.turn_on: green_led_out
- delay: 100ms
- output.turn_off: green_led_out
- delay: 100ms
- output.turn_on: green_led_out
- delay: 100ms
- output.turn_off: green_led_out
- delay: 400ms
# Blink pattern while ramping DOWN: steady slow blink
- id: led_flash_down
mode: restart
then:
- while:
condition:
lambda: 'return true;'
then:
- output.turn_on: green_led_out
- delay: 250ms
- output.turn_off: green_led_out
- delay: 250ms
# Script: ramp up from current level. Obey global max.
- id: ramp_on_script
mode: restart
then:
- script.stop: ramp_off_script
- script.stop: led_flash_down
- script.execute: led_flash_up
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
return (!cv.is_on()) || (cv.get_brightness() + 0.0005f < floor);
then:
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
transition_length: 80ms
# Ramp from current (>= floor) to cap over a fraction of ramp_up_ms.
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(max_brightness_pct) / 100.0f;'
transition_length: !lambda |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
const float cap = id(max_brightness_pct) / 100.0f;
float curr = cv.is_on() ? cv.get_brightness() : 0.0f;
if (curr < floor) curr = floor;
if (curr > cap) curr = cap;
float frac = (cap - curr) / (cap - floor);
if (frac < 0.0f) frac = 0.0f;
if (frac > 1.0f) frac = 1.0f;
id(last_ramp_ms) = (int) (id(ramp_up_ms) * frac);
return (uint32_t) id(last_ramp_ms);
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- script.stop: led_flash_up
- output.turn_off: green_led_out
# Script: ramp down from current level to floor, then cleanly cut to OFF
- id: ramp_off_script
mode: restart
then:
- script.stop: ramp_on_script
- script.stop: led_flash_up
- script.execute: led_flash_down
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
transition_length: !lambda |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
float curr = cv.is_on() ? cv.get_brightness() : 0.0f;
if (curr < floor) curr = floor;
float frac = (curr - floor) / (1.0f - floor);
if (frac < 0.0f) frac = 0.0f;
if (frac > 1.0f) frac = 1.0f;
id(last_ramp_ms) = (int) (id(ramp_down_ms) * frac);
return (uint32_t) id(last_ramp_ms);
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- light.turn_off:
id: mosfet_leds
transition_length: 150ms
- delay: 150ms
- script.stop: led_flash_down
- output.turn_off: green_led_out
- lambda: |-
auto call = id(mosfet_leds).make_call();
call.set_state(false);
call.set_brightness(id(max_brightness_pct) / 100.0f);
call.perform();
- id: max_on_watchdog
mode: restart
then:
- if:
condition:
lambda: 'return id(max_on_hours) > 0;'
then:
- delay: !lambda 'return (uint32_t) (id(max_on_hours) * 3600000UL);'
- if:
condition:
lambda: 'return id(mosfet_leds).current_values.is_on();'
then:
- lambda: |-
id(ramp_switch_target_on) = false;
id(mosfet_ramp_switch).publish_state(false);
- script.stop: ramp_on_script
- script.execute: ramp_off_script

View File

@@ -0,0 +1,220 @@
#############################################
#############################################
# DOWNSTAIRS BATHROOM MAIN LIGHTSWITCH
# V2.0 2025-06-05 YAML Tidyups
# V1.0 2025-02-14 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
# Device Naming
device_name: "esp-downstbathswitch"
friendly_name: "Downstairs Bath Lightswitch (3)"
description_comment: "Downstairs Bathroom Main Lightswitch using a Zemismart KS-811 Triple Push Button. Main Light (1), Cabinet Light (2), Extract Fan (3)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v2.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-downstbathswitch_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-downstbathswitch_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstbathswitch_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Switch Naming
switch_1_name: "Main Lights"
switch_2_name: "Cabinet Light"
switch_3_name: "Extract Fan" # This is virtual only, no power connected to 3rd relay
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: True
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: ${switch_3_name}"
on_multi_click:
# Single click: 1 press → relay on for 5 minutes
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
then:
- if:
condition:
lambda: 'return id(Relay_3).state;'
then:
- switch.turn_off: Relay_3
else:
- switch.turn_on: Relay_3
- delay: 5min
- switch.turn_off: Relay_3
# Double click: 2 presses → relay on for 30 minutes
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.5s
then:
- if:
condition:
lambda: 'return id(Relay_3).state;'
then:
- switch.turn_off: Relay_3
else:
- switch.turn_on: Relay_3
- delay: 30min
- switch.turn_off: Relay_3
# Triple click: 3 presses → relay on for 4 hours
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- if:
condition:
lambda: 'return id(Relay_3).state;'
then:
- switch.turn_off: Relay_3
else:
- switch.turn_on: Relay_3
- delay: 4h
- switch.turn_off: Relay_3
# Hold: pressed for 1 second or more → relay toggles permanently
- timing:
- ON for at least 2s
then:
- if:
condition:
lambda: 'return id(Relay_3).state;'
then:
- switch.turn_off: Relay_3
else:
- switch.turn_on: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: ${switch_3_name}"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,711 @@
##########################################################################################
##########################################################################################
# DOWNSTAIRS BATHROOM HEATED TOWEL RAIL
# Controlled by a Sonoff Basic
#
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
# V1.1 2025-04-12 Fixes to timers and offline modes
# V1.0 2025-02-14 Initial Version
#
# INSTRUCTIONS
# - It allows the device to work in a standalone timer style operation
# - The timer has a morning and evening time (but no weekday/weekend settings)
# - Default values are set, but changed values are remembered in flash
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
# - If on a network and there is a MQTT server, you can set the on/off times via MQTT (See below commands)
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
# - Any new timer times set via MQTT will be remembered though a reboot
# - On startup, or a reboot, the device will always turn on for the BOOST Duration (BOOST mode, default 2 hours)
# - TIMER mode will always be switched on after BOOST mode is complete
# - Home Assistant entities are set so that BOOST mode can be pressed with a button and other modes selectable with a dropdown
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. (Note there is no weekday/weekend setting)
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
# mqtt_timer_topic/operation/ON : Device permanently on
# mqtt_timer_topic/operation/OFF : Device permanently off
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
#
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstbathtowelrail"
friendly_name: "Downstairs Bathroom Towelrail"
description_comment: "Heated Towel Rail, Downstairs Bathroom :: Sonoff Basic"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v2.2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstbathtowelrail_ip
# Device Settings
relay_icon: "mdi:heating-coil"
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "20s" # update time for for general sensors etc
# Timer Settings
mqtt_timer_topic: "viewroad-commands/downstbath-towelrail" # Topics you will use to change stuff
boost_duration_default: "120" # Minutes to stay ON in BOOST mode before reverting to TIMER
morning_on_default: "420" # Default in minutes from midnight. Default 07:00 => 420
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
evening_off_default: "1320" # Default in minutes from midnight. Default 22:00 => 1320 => 1440 is midnight
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptions
- "-fno-rtti" # strip C++ RTTI
on_boot:
priority: 900 # High priority to run after globals are initialized
then:
# This evaluates if we switch on 4+ times in 20 seconds
# If so, just change to on mode permanently.
- lambda: |-
// 1) Figure out the current time in "seconds from midnight"
// using SNTP if available, otherwise current_mins * 60.
bool have_sntp = id(sntp_time).now().is_valid();
int current_time_s = 0;
if (have_sntp) {
auto now = id(sntp_time).now();
current_time_s = now.hour * 3600 + now.minute * 60 + now.second;
} else {
// current_mins is in minutes; convert to seconds
current_time_s = id(current_mins) * 60;
}
// 2) Compare with the last boot time
int diff = current_time_s - id(last_boot_time_s);
// If within 30 seconds, increment boot_count; otherwise reset to 1
if (diff >= 0 && diff <= 30) {
id(boot_count)++;
} else {
id(boot_count) = 1;
}
// Update stored last boot time
id(last_boot_time_s) = current_time_s;
// 3) If we've booted 4+ times in 20s => force ON mode
if (id(boot_count) >= 4) {
id(operation_mode) = 1; // ON
ESP_LOGI("power_cycle", "Detected 4 power cycles in 20s => Forcing ON mode");
} else {
// Otherwise do your normal startup logic:
id(operation_mode) = 3; // on_boot -> sets operation_mode = 3 (BOOST)
id(boost_timer) = 0; // and reset boost_timer = 0 (for time sync if no sntp)
ESP_LOGI("power_cycle", "Boot count=%d => BOOST mode", id(boot_count));
}
- script.execute: evaluate_relay_state # Check what the relay should be doing straight away
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: true
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
####################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
# Relay Switch (Sonoff Basic Relay on GPIO12)
####################################################
switch:
- platform: gpio
name: "Towel Rail Power"
pin: GPIO12
id: relay
restore_mode: RESTORE_DEFAULT_OFF
icon: "${relay_icon}"
##########################################################################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
# Tracks the time (in seconds from midnight) at the previous boot
- id: last_boot_time_s
type: int
restore_value: true
initial_value: "0"
# Counts how many consecutive boots have occurred (within X seconds)
- id: boot_count
type: int
restore_value: true
initial_value: "0"
# Morning On time (minutes from midnight),
- id: morning_on
type: int
restore_value: true
initial_value: "${morning_on_default}"
# Morning Off time (minutes from midnight),
- id: morning_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening On time (minutes from midnight),
- id: evening_on
type: int
restore_value: true
initial_value: "${evening_on_default}"
# Evening Off time (minutes from midnight),
- id: evening_off
type: int
restore_value: true
initial_value: "${evening_off_default}"
# Boost Duration (minutes),
- id: boost_duration
type: int
restore_value: true
initial_value: "${boost_duration_default}"
####################################################
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
####################################################
- id: operation_mode
type: int
restore_value: true
initial_value: "2"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
# Not restored, so it resets each boot.
####################################################
- id: current_mins
type: int
restore_value: false
initial_value: "720" # 720 is 12:00 Noon
####################################################
# boost_timer: counts minutes in BOOST mode
# After 'boost_duration' minutes, revert to TIMER.
# Not restored, so each boot starts fresh at 0.
####################################################
- id: boost_timer
type: int
restore_value: false
initial_value: "0"
##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
############################
# MQTT Subscriptions
############################
############################
# Morning On time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(morning_on) = h * 60 + m;
ESP_LOGI("timer","Received new Morning On: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
############################
# Morning Off time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(morning_off) = h * 60 + m;
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
############################
# Evening On time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(evening_on) = h * 60 + m;
ESP_LOGI("timer","Received new Evening On: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
############################
# Evening Off time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(evening_off) = h * 60 + m;
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
############################
# Boost duration => integer minutes (11439)
############################
- platform: mqtt_subscribe
name: "Boost Duration"
id: boost_time_topic
topic: "${mqtt_timer_topic}/boost-time"
internal: true
on_value:
then:
- lambda: |-
int v = 0;
// Parse as integer
if (sscanf(x.c_str(), "%d", &v) == 1 && v >= 1 && v <= 1439) {
id(boost_duration) = v;
ESP_LOGI("boost_time","Received new Boost Duration: %d mins", v);
} else {
ESP_LOGW("boost_time","Invalid boost_time '%s'", x.c_str());
}
####################################################
# Subscribe to operation mode: OFF, ON, TIMER, BOOST
####################################################
- platform: mqtt_subscribe
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation"
internal: true
on_value:
then:
- lambda: |-
// Check only the first character for mode
char c = x.c_str()[0];
if (c == 'T') { // “TIMER”
id(operation_mode) = 2;
} else if (c == 'O') { // “ON” or “OFF”
// second letter N→ON, F→OFF
id(operation_mode) = (x.size() > 1 && x[1] == 'N') ? 1 : 0;
} else if (c == 'B') { // “BOOST”
id(operation_mode) = 3;
id(boost_timer) = 0;
} else {
ESP_LOGW("timer","Invalid mode: %s", x.c_str());
}
- script.execute: evaluate_relay_state
######################################################
# Expose the current operation mode (OFF, ON, TIMER, BOOST)
######################################################
- platform: template
name: "Operation Mode State"
lambda: |-
// 0=OFF, 1=ON, 2=TIMER, 3=BOOST
switch (id(operation_mode)) {
case 0: return {"OFF"};
case 1: return {"ON"};
case 2: return {"TIMER"};
case 3: return {"BOOST"};
default: return {"UNKNOWN"};
}
update_interval: 5s
######################################################
# Expose the "Morning On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning On Time"
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Morning Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning Off Time"
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening On Time"
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening Off Time"
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay).state) {
// Relay is ON: turn it OFF and set mode to 0 (TIMER)
id(relay).turn_off();
id(operation_mode) = 2;
} else {
// Relay is OFF: turn it ON and set mode to 3 (BOOST)
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
##########################################################################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
sensor:
- platform: template
name: "Timeclock: Boost Duration"
id: boost_duration_time
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
internal: True # No need to show this in Home Assistant
lambda: |-
return id(current_mins);
# A value in mins if a timer is running showing how many mins left
- platform: template
name: "Timer Minutes Remaining"
id: timer_minutes_remaining
unit_of_measurement: "Mins"
update_interval: 5s
accuracy_decimals: "0"
lambda: |-
// always zero if relay is off
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (BOOST)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
rem = id(boost_duration) - id(boost_timer);
}
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
- platform: template
name: "Boost now"
id: boost_button
icon: "mdi:play-circle-outline"
on_press:
# 1) reset BOOST timer and set mode
- lambda: |-
id(boost_timer) = 0;
id(operation_mode) = 3;
# 2) immediately re-evaluate relay state
- script.execute: evaluate_relay_state
- platform: template
name: "Default timer settings"
id: default_timer_settings_button
icon: "mdi:restore"
on_press:
- lambda: |-
// Restore all timing globals to their YAML defaults
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration)= ${boost_duration_default};
// Reset mode to TIMER and clear any running boost
id(operation_mode)= 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
- script.execute: evaluate_relay_state
#################################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#################################################################################################
select:
- platform: template
name: "Operation Mode"
id: operation_mode_select
update_interval: 5s
options:
- "OFF"
- "ON"
- "TIMER"
- "BOOST"
# show the current mode
lambda: |-
switch (id(operation_mode)) {
case 1: return std::string("ON");
case 2: return std::string("TIMER");
case 3: return std::string("BOOST");
default: return std::string("OFF");
}
# when changed in HA, set mode & re-evaluate
set_action:
- lambda: |-
if (x == "OFF") { id(operation_mode) = 0; }
else if (x == "ON") { id(operation_mode) = 1; }
else if (x == "TIMER") { id(operation_mode) = 2; }
else { // BOOST
id(boost_timer) = 0;
id(operation_mode) = 3;
}
- script.execute: evaluate_relay_state
#################################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
#################################################################################################
# Script: evaluate and drive the relay
script:
- id: evaluate_relay_state
then:
- lambda: |-
int mode = id(operation_mode);
// BOOST just forces the relay on
if (mode == 3) {
id(relay).turn_on();
return;
}
// OFF → always off
if (mode == 0) {
id(relay).turn_off();
return;
}
// ON → always on
if (mode == 1) {
id(relay).turn_on();
return;
}
// TIMER → follow schedule windows
{
bool should_on = false;
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off))
should_on = true;
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off))
should_on = true;
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
#################################################################################################
# INTERVAL COMPONENT
# https://esphome.io/components/interval.html
#################################################################################################
# Interval: bumps time (even if no SNTP), then calls the script to evaluate relay state
interval:
- interval: "1min"
then:
- lambda: |-
// — update current_mins via SNTP or fallback
if (!id(sntp_time).now().is_valid()) {
id(current_mins)++;
if (id(current_mins) >= 1440) id(current_mins) = 0;
} else {
auto now = id(sntp_time).now();
id(current_mins) = now.hour * 60 + now.minute;
}
// — if in BOOST, advance boost_timer and expire when done
if (id(operation_mode) == 3) {
id(boost_timer)++;
if (id(boost_timer) >= id(boost_duration)) {
id(operation_mode) = 2;
//id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
}
}
- script.execute: evaluate_relay_state

View File

@@ -0,0 +1,187 @@
##########################################################################################
##########################################################################################
# DOwNSTAIRS BEDROOM 2 LIGHTSWITCH
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Single push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstbedrm2lights"
friendly_name: "Downstairs Bedroom 2 Lightswitch (1)"
description_comment: "Downstairs Bedroom 2 Main Lightswitch using a Zemismart KS-811 Single Push Button. Main Lights (1)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Single" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstbedrm2lights_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "downst-bedroom2-lights"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
# Switch/Relay/Button Naming & Icons
#relay_icon: "mdi:lightbulb-group"
switch_1_name: "Main Lights"
#switch_2_name: "Nil"
#switch_3_name: "Nil"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO02
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
# GPIO16 for KS-811 first button UNLESS
# it is a single button KS-811 in which case it is GPIO00
- platform: gpio
pin:
number: GPIO0
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
# - platform: gpio
# pin:
# number: GPIO05
# mode: INPUT
# inverted: True
# name: "Button 2: ${switch_2_name}"
# on_press:
# - switch.toggle: Relay_2
# GPIO04 for third button (only for KS-811-3 Triple)
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
# GPIO13 for KS-811 first button UNLESS it is KS-811-1 then it is GIPO12
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO12
id: Relay_1
# GPIO12 for second relay (only for KS-811-2 Double or -3 Triple)
# - platform: gpio
# name: "Relay 2: ${switch_2_name}"
# pin: GPIO12
# id: Relay_2
# GPIO14 for third relay (only for KS-811-3 Triple)
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,150 @@
##########################################################################################
##########################################################################################
# DOWNSTAIRS DISHWASHER POWER MONITOR
#
# Controlled by a Athom Smart Plug V1
# package_import_url: github://athom-tech/athom-configs/athom-smart-plug.yaml
#
# V1.2 2025-06-15 Changed to Athom V1
# V1.1 2025-06-12 Tidyups and packages added
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstdishwpower"
friendly_name: "Downstairs Dishwasher Power"
description_comment: "Downstairs Dishwasher Power :: Athom Smart Plug Power V1"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V1" # Project Details
project_version: "v1.2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstdishwpower_ip
# Device Settings
log_level: "NONE" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:power-socket-au"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
# Device Specific included packages
common_athompowermonV1: !include
file: common/athompowermonv1_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_current_limit: "${current_limit}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
name: "${project_name}"
version: "${project_version}"
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp8285
restore_from_flash: true # mainly for calculating cumulative energy, but not that important here
preferences:
flash_write_interval: 10min
mdns:
disabled: false
##########################################################################################
# ESPHome LOGGING
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- switch.toggle: relay
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
switch:
- platform: gpio
name: "Power Output"
pin: GPIO14
id: relay
restore_mode: RESTORE_DEFAULT_ON # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"

View File

@@ -0,0 +1,156 @@
#############################################
#############################################
# DOWNSTAIRS KITCHEN MAIN LIGHTSWITCH
# V2.0 2025-06-05 YAML tidyups
# V1.0 2025-03-28 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
# Device Naming
device_name: "esp-downstkitchlights"
friendly_name: "Downstairs Kitchen Lightswitch (3)"
description_comment: "Downstairs Kitch Main Lightswitch using a Zemismart KS-811 Triple Push Button. Dining Light (1), Kitchen Light (2), Extract Fan (3)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstkitchlights_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Switch Naming
switch_1_name: "Dining Light"
switch_2_name: "Kitchen Light"
switch_3_name: "Extract Fan" # This is virtual only, no power connected to 3rd relay
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} # Appears on the esphome page in HA
area: ${device_area}
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: ${switch_3_name}"
on_press:
- switch.toggle: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: ${switch_3_name}"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,224 @@
##########################################################################################
##########################################################################################
# DOwNSTAIRS BEDROOM 2 LIGHTSWITCH
# V3.6 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Single push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstloungeentry"
friendly_name: "Downstairs Lounge Entry Lightswitch (1)"
description_comment: "Downstairs Lounge Entry Lightswitch using a Zemismart KS-811 Single Push Button. Main Lights (1)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Single" # Project Details
project_version: "v3.6" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstloungeentry_ip
mqtt_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "downst-lounge-entry-light"
#mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
mqtt_remote_device_name: "downst-lounge-main-lights"
mqtt_remote_device_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device_name}/light1/set"
mqtt_remote_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device_name}/light1/state" # Topic we will use to view status locally without HA
mqtt_remote_device_command_on: "On"
mqtt_remote_device_command_off: "Off"
# Switch/Relay/Button Naming & Icons
#relay_icon: "mdi:lightbulb-group"
switch_1_name: "Main Lights" # NOTE there is no physical connection to the lights on this switch
#switch_2_name: "Nil"
#switch_3_name: "Nil"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
- topic: "${mqtt_remote_status_topic}"
payload: "${mqtt_remote_device_command_on}"
then:
- delay: 50ms
- if:
condition:
lambda: 'return !id(Relay_1).state;' # Only turn on if currently OFF
then:
- switch.turn_on: Relay_1
- topic: "${mqtt_remote_status_topic}"
payload: "${mqtt_remote_device_command_off}"
then:
- delay: 50ms
- if:
condition:
lambda: 'return id(Relay_1).state;' # Only turn off if currently ON
then:
- switch.turn_off: Relay_1
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO02
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
# GPIO16 for KS-811 first button UNLESS it is KS-811-1 in which case it is GPIO00
- platform: gpio
pin:
number: GPIO0
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- mqtt.publish:
topic: "${mqtt_remote_device_command_topic}"
payload: !lambda |-
if (id(Relay_1).state) {
return "${mqtt_remote_device_command_off}";
} else {
return "${mqtt_remote_device_command_on}";
}
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
# - platform: gpio
# pin:
# number: GPIO05
# mode: INPUT
# inverted: True
# name: "Button 2: ${switch_2_name}"
# on_press:
# - switch.toggle: Relay_2
# GPIO04 for third button (only for KS-811-3 Triple)
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
# GPIO13 for KS-811 first button UNLESS it is KS-811-1 then it is GPIO12
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO12
id: Relay_1
# GPIO12 for second relay (only for KS-811-2 Double or -3 Triple)
# - platform: gpio
# name: "Relay 2: ${switch_2_name}"
# pin: GPIO12
# id: Relay_2
# GPIO14 for third relay (only for KS-811-3 Triple)
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,221 @@
##########################################################################################
##########################################################################################
# DOWNSTAIRS LOUNGE MAIN LIGHTS
# V3.5 2025-07-28 YAML tidyups
##########################################################################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstloungemain"
friendly_name: "Downstairs Lounge Main Lightswitch (3)"
description_comment: "Downstairs Bedroom Main Lightswitch using a Zemismart KS-811 Triple Push Button. Main Lights (1), Back Wall Lights (2), Spare (3)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstloungemain_ip
mqtt_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
mqtt_device_name: "downst-lounge-main-lights"
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
# Switch/Relay/Button Naming & Icons
#relay_icon: "mdi:lightbulb-group"
switch_1_name: "Main Lights"
switch_2_name: "Back Wall Lights"
switch_3_name: "Spare" # NOTE, Nothing connected to this switch output
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Light control
- topic: "${mqtt_local_command_topic}/light1/set"
payload: "On"
then:
- switch.turn_on: Relay_1
- topic: "${mqtt_local_command_topic}/light1/set"
payload: "Off"
then:
- switch.turn_off: Relay_1
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO02
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
# GPIO16 for KS-811 first button UNLESS
# it is a single button KS-811 in which case it is GPIO00
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
# - mqtt.publish:
# topic: "${mqtt_local_command_topic}"
# payload: !lambda |-
# if (id(Relay_1).state) {
# return "${mqtt_local_command_off}";
# } else {
# return "${mqtt_local_command_on}";
# }
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
# GPIO04 for third button (only for KS-811-3 Triple)
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: ${switch_3_name}"
on_press:
- switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
# GPIO13 for KS-811 first button UNLESS it is KS-811-1 then it is GIPO12
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
# publish status updates when the light turns on/off:
on_turn_on:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light1/state"
payload: "On"
on_turn_off:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light1/state"
payload: "Off"
# GPIO12 for second relay (only for KS-811-2 Double or -3 Triple)
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# GPIO14 for third relay (only for KS-811-3 Triple)
- platform: gpio
name: "Relay 3: ${switch_3_name}"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,187 @@
##########################################################################################
##########################################################################################
# DOwNSTAIRS BEDROOM 2 LIGHTSWITCH
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstloungeoutside"
friendly_name: "Downstairs Lounge Outside Lights (2)"
description_comment: "Downstairs Lounge Outside Lightswitch using a Zemismart KS-811 Double Push Button. Outside Lights (1), Spare (2)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstloungeoutside_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "downst-bedroom2-lights"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
# Switch/Relay/Button Naming & Icons
#relay_icon: "mdi:lightbulb-group"
switch_1_name: "Outside Lights"
switch_2_name: "Spare"
#switch_3_name: "Nil"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
# GPIO16 for KS-811 first button UNLESS
# it is a single button KS-811 in which case it is GPIO00
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
# GPIO04 for third button (only for KS-811-3 Triple)
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
# GPIO13 for KS-811 first button
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
# GPIO12 for second relay (only for KS-811-2 Double or -3 Triple)
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# GPIO14 for third relay (only for KS-811-3 Triple)
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,186 @@
##########################################################################################
##########################################################################################
# DOWNSTAIRS MASTER BEDROOM 1 LIGHTS
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-downstmasterbedrmlights"
friendly_name: "Downstairs Master Bedroom 1 Lightswitch (2)"
description_comment: "Downstairs Master Bedroom 1 Main Lightswitch using a Zemismart KS-811 Triple Push Button. Main Lights (1), Wardrobe Lights (2), Spare (3)"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-downstmasterbedrmlights_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "downst-bedroom2-lights"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
# Switch/Relay/Button Naming & Icons
#relay_icon: "mdi:lightbulb-group"
switch_1_name: "Main Lights"
switch_2_name: "Wardrobe Lights"
#switch_3_name: "Spare"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO02
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
# GPIO16 for KS-811 first button
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
# GPIO04 for third button (only for KS-811-3 Triple)
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
# GPIO13 for KS-811 first relay
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
# GPIO12 for second relay (only for KS-811-2 Double or -3 Triple)
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# GPIO14 for third relay (only for KS-811-3 Triple)
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

108
esphome/esp-entbtproxy.yaml Normal file
View File

@@ -0,0 +1,108 @@
#############################################
#############################################
# ESP32
# Bluetooth Proxy
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-entbtproxy"
friendly_name: "Outside Entrance Bluetooth Proxy"
description_comment: "D1 Mini ESP32 outside entranceway with BT Proxy"
api_key: !secret esp-entbtproxy_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-entbtproxy_ota_pass #unfortunately you can't use substitutions in secrets names
static_ip_address: !secret esp-entbtproxy_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
room: "Entranceway" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
#############################################
# SPECIFIC PROJECT VARIABLE SUBSTITUTIONS
#############################################
#
#
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
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/esp32.html
#############################################
esp32:
board: esp32dev
framework:
#type: arduino
type: esp-idf #Suggested Use ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended #recommended, latest or dev
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# Bluetooth
# https://esphome.io/components/bluetooth_proxy.html
# https://esphome.io/components/esp32_ble_tracker.html
# Remember that this takes a LOT of processing. On the
# ESP32, enable the IDF framework, and disable the
# Web server component. Changing to the IDF framework
# needs to be via cable not OTA to change the
# partition setup.
#############################################
bluetooth_proxy:
active: true
esp32_ble_tracker:
scan_parameters:
# We currently use the defaults to ensure Bluetooth
# can co-exist with WiFi In the future we may be able to
# enable the built-in coexistence logic in ESP-IDF
active: true

437
esphome/esp-entmulti.yaml Normal file
View File

@@ -0,0 +1,437 @@
#############################################
#############################################
#
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-entmulti"
friendly_name: "Outside Entrance Multisensor"
description_comment: "D1 Mini ESP32 outside entranceway with, mmWave presence, PIR and more"
api_key: !secret esp-entmulti_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-entmulti_ota_pass #unfortunately you can't use substitutions in secrets names
static_ip_address: !secret esp-entmulti_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_time: 30s #update time for for general temp sensors etc
update_interval: "60s" # update time for for general sensors etc
room: "Entranceway" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
#############################################
# SPECIFIC PROJECT VARIABLE SUBSTITUTIONS
#############################################
#
#
#if NOT using a secrets file, just replace these with the passwords etc (in quotes)
#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
#static_ip_address: !secret esp-entmulti_static_ip
#static_ip_gateway: !secret esp-entmulti_gateway
#static_ip_subnet: !secret esp-entmulti_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
#web_server_username: !secret web_server_username
#web_server_password: !secret web_server_password
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
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/esp32.html
#############################################
esp32:
board: esp32dev
framework:
#type: arduino
type: esp-idf #Suggested Use ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended #recommended, latest or dev
#############################################
# ESPHome external or custom components to use
# https://esphome.io/components/external_components.html
# https://github.com/ssieb/esphome_components/tree/master/components/serial
#############################################
#external_components:
# - source:
# type: git
# url: https://github.com/ssieb/custom_components #Thanks for @ssieb components.
# components: [ serial ] #text_sensor that reads lines for a uart. Also, a sensor that reads single binary values from the uart.
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# i2c bus
# https://esphome.io/components/i2c.html
# 10, 50, 100, 200, 800 are possible settings
# for frequency, 50kHz is default
#############################################
#i2c:
# sda: GPIO19
# scl: GPIO21
# scan: True #look for devices on boot up and report
#frequency: 100kHz
#############################################
# UART Serial
# hardware on EPS32, but software, and can be glitchy on ESP8266
# https://esphome.io/components/uart.html
#############################################
uart:
id: ld2410_uart
rx_pin: GPIO16 #For ESP32, you can use any pin, Recommend Use UART_2, Don't use UART_0, It might Cause Boot Fail or System Hang
tx_pin: GPIO17 #For ESP32, you can use any pin, Recommend Use UART_2, Don't use UART_0, It might Cause Boot Fail or System Hang
baud_rate: 256000 # default for LD2410 is 25600, 8, 0, NONE
data_bits: 8
stop_bits: 1
parity: NONE
#############################################
# Bluetooth
# https://esphome.io/components/bluetooth_proxy.html
# https://esphome.io/components/esp32_ble_tracker.html
# Remember that this takes a LOT of processing. On the
# ESP32, enable the IDF framework, and disable the
# Web server component. Changing to the IDF framework
# needs to be via cable not OTA to change the
# partition setup.
#############################################
#bluetooth_proxy:
# active: true
# cache_services: true
#
#esp32_ble_tracker:
# scan_parameters:
# active: true
# continuous: false
#############################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
#############################################
#############################################
# General esp status LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2 #ESP32 Onboard LED
ignore_strapping_warning: True #https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
inverted: false
#############################################
# Interval Automations
# https://esphome.io/guides/automations.html
#############################################
#############################################
# LD2410 Sensors
# https://esphome.io/components/sensor/ld2410.html
# https://www.hlktech.net/index.php?id=988
#############################################
ld2410:
uart_id: ld2410_uart
#############################################
# Number Sensors (custom component)
# refer https://github.com/ssieb/esphome_components/tree/master/components/serial
#############################################
number:
- platform: ld2410
timeout:
name: Timeout
light_threshold:
name: Light Threshold
max_move_distance_gate:
name: Max Move Distance Gate
max_still_distance_gate:
name: Max Still Distance Gate
g0:
move_threshold:
name: g0 move threshold
still_threshold:
name: g0 still threshold
g1:
move_threshold:
name: g1 move threshold
still_threshold:
name: g1 still threshold
g2:
move_threshold:
name: g2 move threshold
still_threshold:
name: g2 still threshold
g3:
move_threshold:
name: g3 move threshold
still_threshold:
name: g3 still threshold
g4:
move_threshold:
name: g4 move threshold
still_threshold:
name: g4 still threshold
g5:
move_threshold:
name: g5 move threshold
still_threshold:
name: g5 still threshold
g6:
move_threshold:
name: g6 move threshold
still_threshold:
name: g6 still threshold
g7:
move_threshold:
name: g7 move threshold
still_threshold:
name: g7 still threshold
g8:
move_threshold:
name: g8 move threshold
still_threshold:
name: g8 still threshold
#The ld2410 select allows you to control your LD2410 Sensor.
#distance_resolution (Optional): Control the gates distance resolution. Can be 0.75m or 0.2m. Defaults to 0.75m. All options from Select.
#baud_rate (Optional): Control the serial port baud rate. Defaults to 256000. Once changed, all sensors will stop working until a fresh install with an updated UART Component configuration. All options from Select.
#light_function (Optional): If set, will affect the OUT pin value, based on light threshold. Can be off, low or above. Defaults to off. All options from Select.
#out_pin_level (Optional): Control OUT pin away value. Can be low or high. Defaults to low. All options from Select.
#ld2410_id (Optional, ID): Manually specify the ID for the LD2410 Sensor component if you are using multiple components.
select:
- platform: ld2410
distance_resolution:
name: ${friendly_name} LD2140 Distance Resolution
baud_rate:
name: ${friendly_name} LD2140 Baud Rate
light_function:
name: ${friendly_name} LD2140 Light Function
out_pin_level:
name: ${friendly_name} LD2140 Out Pin Level
#############################################
# General Sensors
# https://esphome.io/components/sensor/index.html
#############################################
sensor:
# - platform: bme280_i2c
# address: 0x76
# update_interval: ${update_time}
# temperature:
# name: ${friendly_name} BME280 Temp
# accuracy_decimals: 1
# oversampling: 2x
# pressure:
# name: ${friendly_name} BME280 Pressure
# oversampling: 2x
# humidity:
# name: ${friendly_name} BME280 Humidity
# accuracy_decimals: 1
# oversampling: 2x
#The ld2410 sensor values
- platform: ld2410
light:
name: Light
moving_distance:
name : Moving Distance
still_distance:
name: Still Distance
moving_energy:
name: Move Energy
still_energy:
name: Still Energy
detection_distance:
name: Detection Distance
g0:
move_energy:
name: g0 move energy
still_energy:
name: g0 still energy
g1:
move_energy:
name: g1 move energy
still_energy:
name: g1 still energy
g2:
move_energy:
name: g2 move energy
still_energy:
name: g2 still energy
g3:
move_energy:
name: g3 move energy
still_energy:
name: g3 still energy
g4:
move_energy:
name: g4 move energy
still_energy:
name: g4 still energy
g5:
move_energy:
name: g5 move energy
still_energy:
name: g5 still energy
g6:
move_energy:
name: g6 move energy
still_energy:
name: g6 still energy
g7:
move_energy:
name: g7 move energy
still_energy:
name: g7 still energy
g8:
move_energy:
name: g8 move energy
still_energy:
name: g8 still energy
# The ld2410 switch allows you to control your LD2410 Sensor.
#Bluetooth switch is only useful of you have a B or C model
switch:
- platform: ld2410
engineering_mode:
name: ${friendly_name} LD2140 Engineering Mode
#bluetooth:
#name: ${friendly_name} LD2140 Control Bluetooth
#The ld2410 button allows resetting
#button:
# - platform: ld2410
# factory_reset:
# name: ${friendly_name} LD2140 Factory reset"
# restart:
# name: ${friendly_name} LD2140 Restart
## query_params:
# name: Query Parameters
#############################################
# Text Sensors
# refer https://esphome.io/components/text_sensor/index.html
#############################################
#The ld2410 text sensor allows you to get information about your LD2410 Sensor.
#Bluetooth sensor is only useful of you have a B or C model
#text_sensor:
# - platform: ld2410
# version:
# name: ${friendly_name} LD2140 Firmware Version
#mac_address:
#name: ${friendly_name} LD2140 BT MAC Address
#############################################
# Binary Sensors
# https://esphome.io/components/binary_sensor/index.html
#############################################
binary_sensor:
- platform: ld2410
has_target:
name: ${friendly_name} Presence
has_moving_target:
name: ${friendly_name} Moving Target
has_still_target:
name: ${friendly_name} Still Target
out_pin_presence_status:
name: ${friendly_name} LD2140 Out Pin Presence Status
#Standard PIR Sensor
- platform: gpio
pin:
number: GPIO13
mode:
input: True
pullup: True
inverted: True
filters:
- delayed_on: 50ms
name: ${friendly_name} PIR Sensor
device_class: motion
#RF Input from Vibration Sensor (Green Bin)
- platform: gpio
pin:
number: GPIO04
mode:
input: true
pullup: true
inverted: True
filters:
- delayed_on: 20ms
name: ${friendly_name} Green Bin motion
device_class: vibration
#RF Input from Vibration Sensor (Red Bin)
- platform: gpio
pin:
number: GPIO15
mode:
input: true
pullup: true
inverted: True
filters:
- delayed_on: 20ms
name: ${friendly_name} Red Bin motion
device_class: vibration

View File

@@ -0,0 +1,181 @@
##########################################################################################
##########################################################################################
# ENTRANCE BATHROOM LIGHTSWITCH
# V3.7 2025-09-34 upload to this bathroom
# V3.5 2025-07-24 YAML tidyups
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# - Light switch with light and extract fan
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-entrancebathrmlights"
friendly_name: "Entrance Bathroom Lightswitch (2)"
description_comment: "Entrance bathroom lightswitch using a Zemismart KS-811 Double Push Button. Main Lights (1), Extract Fan (2)"
device_area: "Entranceway" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-entrancebathrmlights_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "bedroom2-lights"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Switch Naming
switch_1_name: "Main Lights"
switch_2_name: "Extract Fan"
#switch_3_name: "Nil"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,183 @@
#############################################
#############################################
# LAUNDRY DRYER MACHINE POWER
# V1.0 2025-05-28 Initial Version
#############################################
# Sonoff POW R1
# https://devices.esphome.io/devices/Sonoff-POW-R1
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundrydrypow" # Reference name for the device in the system.
project_name: "Sonoff Technologies.POW R1" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Load" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Laundry Dryer Power"
description_comment: "Laundry Dryer Power using a Sonoff Pow R1."
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-laundrydrypow_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-laundrydrypow_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundrydrypow_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: "virtual_button"
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
#status_led:
# pin:
# number: GPIO2
# inverted: yes
#############################################
# SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
name: Button
on_press:
- switch.toggle: virtual_button
- platform: template
name: ${entity_prefix} Running
filters:
- delayed_off: 15s
lambda: |-
if (isnan(id(power).state)) {
return {};
} else if (id(power).state > 4) {
// Running
return true;
} else {
// Not running
return false;
}
sensor:
- platform: hlw8012
sel_pin: 5
cf_pin: 14
cf1_pin: 13
update_interval: 2s
current:
name: ${entity_prefix} Current
voltage:
name: ${entity_prefix} Voltage
power:
name: ${entity_prefix} Power
id: power
#on_value_range:
# - above: 4.0
# then:
# - light.turn_on: led
# - below: 3.0
# then:
# - light.turn_off: led
switch:
- platform: template
name: "Relay"
optimistic: true
id: virtual_button
turn_on_action:
- switch.turn_on: relay
- light.turn_on: led
turn_off_action:
- switch.turn_off: relay
- light.turn_off: led
- platform: gpio
id: relay
pin: GPIO12
#restore_mode: ALWAYS_ON # Or RESTORE_DEFAULT_ON/RESTORE_DEFAULT_OFF
output:
- platform: esp8266_pwm
id: pow_blue_led
pin:
number: GPIO15
inverted: True
light:
- platform: monochromatic
name: Status LED
output: pow_blue_led
id: led

129
esphome/esp-laundryenv.yaml Normal file
View File

@@ -0,0 +1,129 @@
#############################################
#############################################
# LAUNDRY ENVIRONMENT - SONOFF DEV
# V1.0 2025-03-28 Initial Version
#############################################
# Sonoff Dev
# https://wiki.iteadstudio.com/Sonoff_DEV
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundryenv" # Reference name for the device in the system.
project_name: "Sonoff Technologies.Sonoff DEV" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Laundry" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Laundry Environment"
description_comment: "Laundry Environment Sensor, PIR and Smoke Detector using Sonoff DEV"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-laundryenv_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-laundryenv_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundryenv_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
sensor:
- platform: dht
pin: GPIO14
temperature:
name: ${entity_prefix} Temperature
humidity:
name: ${entity_prefix} Humidity
update_interval: ${update_interval}
binary_sensor:
- platform: gpio
pin:
number: GPIO13
mode:
input: true
pullup: false
name: ${entity_prefix} PIR
device_class: motion
#filters:
# - invert:
- platform: gpio
pin:
number: GPIO05
mode:
input: true
pullup: true
inverted: True
name: ${entity_prefix} Smoke Detector

View File

@@ -0,0 +1,184 @@
##########################################################################################
##########################################################################################
# LAUNDRY MAIN LIGHTSWITCH
# V3.5 2025-07-24 YAML tidyups
# V3.0 2025-06-05 YAML tidyups
# V2.0 2025-06-01 Was initially a 3 button switch, now 2
# V1.0 2025-05-30 Initial Version
##########################################################################################
# Zemismart KS-811 Double push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-laundrylights"
friendly_name: "Laundry Main Lightswitch (2)"
description_comment: "Laundry Main Lightswitch using a Zemismart KS-811 Double Push Button. Laundry Lights (1), Laundry Power Signal (2)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v3.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundrylights_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT Controls
#mqtt_device_name: "bedroom2-ceilingfan"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Switch Naming
switch_1_name: "Laundry Lights"
switch_2_name: "Laundry Power Enable" # This is virtual only, no power connected to 3rd relay
#switch_3_name: "Fan 3 Relay"
#switch_4_name: "Fan 4 Relay"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
#########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#########################################################################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
status_led:
pin:
number: GPIO2
inverted: yes
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
#########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#########################################################################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,183 @@
#############################################
#############################################
# LAUNDRY BENCH PLUGS POWER AND SWITCH
# V1.0 2025-03-28 Initial Version
#############################################
# Sonoff POW R1
# https://devices.esphome.io/devices/Sonoff-POW-R1
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundryplugpow" # Reference name for the device in the system.
project_name: "Sonoff Technologies.POW R1" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Load" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Laundry Bench Plug Power"
description_comment: "Laundry Bench Plug Power and switch override using a Sonoff Pow R1."
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-laundryplugpow_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-laundryplugpow_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundryplugpow_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: "virtual_button"
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
#status_led:
# pin:
# number: GPIO2
# inverted: yes
#############################################
# SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
name: Button
on_press:
- switch.toggle: virtual_button
- platform: template
name: ${entity_prefix} Running
filters:
- delayed_off: 15s
lambda: |-
if (isnan(id(power).state)) {
return {};
} else if (id(power).state > 4) {
// Running
return true;
} else {
// Not running
return false;
}
sensor:
- platform: hlw8012
sel_pin: 5
cf_pin: 14
cf1_pin: 13
update_interval: 2s
current:
name: ${entity_prefix} Current
voltage:
name: ${entity_prefix} Voltage
power:
name: ${entity_prefix} Power
id: power
#on_value_range:
# - above: 4.0
# then:
# - light.turn_on: led
# - below: 3.0
# then:
# - light.turn_off: led
switch:
- platform: template
name: "Relay"
optimistic: true
id: virtual_button
turn_on_action:
- switch.turn_on: relay
- light.turn_on: led
turn_off_action:
- switch.turn_off: relay
- light.turn_off: led
- platform: gpio
id: relay
pin: GPIO12
#restore_mode: ALWAYS_ON # Or RESTORE_DEFAULT_ON/RESTORE_DEFAULT_OFF
output:
- platform: esp8266_pwm
id: pow_blue_led
pin:
number: GPIO15
inverted: True
light:
- platform: monochromatic
name: Status LED
output: pow_blue_led
id: led

View File

@@ -0,0 +1,183 @@
#############################################
#############################################
# LAUNDRY WASHING MACHINE POWER
# V1.0 2025-03-28 Initial Version
#############################################
# Sonoff POW R1
# https://devices.esphome.io/devices/Sonoff-POW-R1
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundrywashpow" # Reference name for the device in the system.
project_name: "Sonoff Technologies.POW R1" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Load" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Laundry Washing Machine Power" # Friendly name to show in Home Assistant.
description_comment: "Laundry Washing Machine Power using a Sonoff Pow R1."
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-laundrywashpow_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-laundrywashpow_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundrywashpow_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: "virtual_button"
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
#status_led:
# pin:
# number: GPIO2
# inverted: yes
#############################################
# SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
name: Button
on_press:
- switch.toggle: virtual_button
- platform: template
name: ${entity_prefix} Running
filters:
- delayed_off: 15s
lambda: |-
if (isnan(id(power).state)) {
return {};
} else if (id(power).state > 4) {
// Running
return true;
} else {
// Not running
return false;
}
sensor:
- platform: hlw8012
sel_pin: 5
cf_pin: 14
cf1_pin: 13
update_interval: 2s
current:
name: ${entity_prefix} Current
voltage:
name: ${entity_prefix} Voltage
power:
name: ${entity_prefix} Power
id: power
#on_value_range:
# - above: 4.0
# then:
# - light.turn_on: led
# - below: 3.0
# then:
# - light.turn_off: led
switch:
- platform: template
name: "Relay"
optimistic: true
id: virtual_button
turn_on_action:
- switch.turn_on: relay
- light.turn_on: led
turn_off_action:
- switch.turn_off: relay
- light.turn_off: led
- platform: gpio
id: relay
pin: GPIO12
#restore_mode: ALWAYS_ON # Or RESTORE_DEFAULT_ON/RESTORE_DEFAULT_OFF
output:
- platform: esp8266_pwm
id: pow_blue_led
pin:
number: GPIO15
inverted: True
light:
- platform: monochromatic
name: Status LED
output: pow_blue_led
id: led

248
esphome/esp-leafbat.yaml Normal file
View File

@@ -0,0 +1,248 @@
#############################################
#############################################
# 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' ]

View File

@@ -1,133 +0,0 @@
#############################################
# Common
#############################################
substitutions:
devicename: esp-leafbat2
nice_devicename: "Leaf Battery Monitor 2"
#############################################
# ESPHome
#############################################
esphome:
name: $devicename
########################################
# Specific board for ESPHome device
########################################
esp8266:
board: d1_mini
#############################################
# ESPHome Logging Enable
#############################################
logger:
########################################
# Enable the Home Assistant API
########################################
api:
encryption:
key: !secret esp-leafbat2_api_key
########################################
# Enable Over the Air Update Capability
# Safe mode will detect boot loops
########################################
ota:
safe_mode: true
password: !secret esp-leafbat2_ota_pass
########################################
# Use Wifi
# (credentials are in secrets file)
########################################
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Details for fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp-Leafbat2 Fallback Hotspot"
password: !secret fallback_ap_password
# Static IP (for lower power/quicker on time)
manual_ip:
static_ip: !secret esp-leafbat2_static_ip
gateway: !secret esp-leafbat2_gateway
subnet: !secret esp-leafbat2_subnet
#############################################
# Fallback captive portal
#############################################
captive_portal:
########################################
# Web Portal for display and monitoring
########################################
web_server:
port: 80
auth:
username: !secret web_server_username
password: !secret web_server_password
########################################
# MQTT Monitoring
########################################
mqtt:
broker: !secret mqtt_server
topic_prefix: esphome/esp-leafbat2
username: !secret mqtt_username
password: !secret mqtt_password
#
# Availability Topic
#birth_message:
# topic: esphome/esp-leafbat2/availability
# payload: online
#will_message:
# topic: esphome/esp-leafbat2/availability
# payload: offline
# A way to prevent deep sleep using MQTT command
on_message:
- topic: viewroad-commands/leafbat2/deepsleepoff
payload: 'ON'
then:
- deep_sleep.prevent: deep_sleep_1
- topic: viewroad-commands/leafbat2/deepsleepon
payload: 'ON'
then:
- deep_sleep.enter: deep_sleep_1
########################################
# Deep Sleep
########################################
deep_sleep:
run_duration: 20s
sleep_duration: 10min
id: deep_sleep_1
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
sensor:
# Quality of Wifi in dBm
- platform: wifi_signal
name: "WiFi Signal"
update_interval: 10s
retain: true
# Analog sensor for voltage reading (A0)
- platform: adc
pin: A0
name: "Battery Voltage"
update_interval: 10s
retain: true
filters:
- multiply: 18.52 # trial and error calc/measure
- calibrate_linear:
- 5.99 -> 6.0
- 11.98 -> 11.99
- 16.98 -> 16.98
#name: "Battery Voltage"
# Optional variables:
#retain: true

View File

@@ -0,0 +1,197 @@
#############################################
#############################################
# MAIN BATHROOM FAN/HEAT COMBO SWITCH
# V1.1 2025-08-26 Minor Changes (MQTT)
# V1.0 2025-06-01 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
# Device Naming
device_name: "esp-mainbathfancombo"
friendly_name: "Main Bathroom Fan/Heat Combo Switch (2)"
description_comment: "Main Bathroom Fan/Heat Switch using a Zemismart KS-811 Double Push Button. Extract Fan (1), IR heater (2)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811-2 (Double)" # Project Details
project_version: "v1.1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Main Bathroom" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-mainbathfancombo_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
#mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
#relay_icon: "mdi:heating-coil"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT REMOTE Controls
mqtt_remote_device1_name: "masterbath-towelrail"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/operation"
mqtt_remote_device1_command1: "BOOST"
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} #Appears on the esphome page in HA
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptions
- "-fno-rtti" # strip C++ RTTI
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
# Status LED for KS-811 is gpio2
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
# Buttons for KS-811-2 are GPIO16, GPIO05
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: Extract Fan"
on_press:
- switch.toggle: "Relay_1"
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: true
name: "Button 2: IR Heater"
filters:
- delayed_on: 20ms
- delayed_off: 20ms
on_multi_click:
# ── Single click: one quick press & release ─────────────────
- timing:
- ON for at most 400ms
- OFF for at least 200ms
then:
- switch.toggle: "Relay_2"
# ── Double click: two quick presses/releases ───────────────
- timing:
- ON for at most 400ms
- OFF for at most 400ms
- ON for at most 400ms
- OFF for at least 200ms
then:
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device1_command1}"
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
# Relays for KS-811-2 are GPIO13, GPIO12
#############################################
switch:
- platform: gpio
name: "Relay 1: Extract Fan"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: IR Heater"
pin: GPIO12
id: Relay_2
# Always run the fan when IR heater on. Not critical for this heat/lamp product
# as it has its own failsafes and fan switching on temp, but an added safety measure
on_turn_on:
switch.turn_on: "Relay_1"

View File

@@ -0,0 +1,164 @@
#############################################
#############################################
# MAIN BATHROOM LIGHTSWITCH
# V1.0 2025-06-01 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-mainbathlights"
project_name: "Zemismart Technologies.KS-811-3 (Triple)" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Laundry" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Main Bathroom Lightswitch (3)"
description_comment: "Main Bathroom Lightswitch using a Zemismart KS-811 Triple Push Button. Main Lights (1), Shower Lights (2), Cabinet Lights (3)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-mainbathlights_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} #Appears on the esphome page in HA
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: Main Lights"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: Shower Lights"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: Cabinet Lights"
on_press:
- switch.turn_off: Relay_1
- switch.turn_off: Relay_2
- switch.turn_off: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: Main Lights"
pin: GPIO13
id: Relay_1
on_turn_on:
- switch.turn_on: Relay_2
- switch.turn_on: Relay_3
- platform: gpio
name: "Relay 2: Shower Lights"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: Cabinet Lights"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,166 @@
##########################################################################################
##########################################################################################
# MAIN DISHWASHER POWER
# Controlled by a Athom Smart Plug V3
# package_import_url: github://athom-tech/athom-configs/athom-smart-plug.yaml
#
# V3.4 2025-07-30 Changed to Athom V3
# V1.1 2025-06-12 package added for energy entities
# V1.0 2025-06-10 YAML Tidyups
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-maindishwasherpower"
friendly_name: "Main Dishwasher Power"
description_comment: "Main Dishwasher Power Monitor :: Athom Smart Plug Power Monitor V3"
device_area: "Kitchen" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V3" # Project Details
project_version: "v1.1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-maindishwasherpower_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-maindishwasherpower_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-maindishwasherpower_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:dishwasher"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
# Device Specific included packages
common_athompowermonV3: !include
file: common/athompowermonv3_common.yaml
vars:
local_current_limit: "${current_limit}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
name: "${project_name}"
version: "${project_version}"
platformio_options:
board_build.mcu: esp32c3
board_build.variant: esp32c3
board_build.flash_mode: dio
on_boot:
priority: 200
then:
- switch.turn_on: "relay"
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp32:
board: esp32-c3-devkitm-1
flash_size: 4MB
variant: ESP32C3
framework:
type: esp-idf # "esp-idf" OR "arduino". Suggested ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended # recommended, latest or dev
preferences:
flash_write_interval: 5min
esp32_improv:
authorizer: none
##########################################################################################
# ESPHome LOGGING
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO03
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- switch.toggle:
id: relay
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
switch:
- platform: gpio
name: "Power Output"
pin: GPIO05
id: relay
restore_mode: RESTORE_DEFAULT_ON # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"

View File

@@ -0,0 +1,183 @@
#############################################
#############################################
# OVEN POWER MONITOR
# Monitoring power of an oven (at the main
# switchboard) using a sonoff basic (esp8266)
# and a PZEM-004T. The relay also allows disabling
# of the oven with a contactor (eg on smoke detection)
# https://zorruno.com/2022/nodered-oven-notifications/
##############################################
#############################################
#############################################
# Variable Substitutions
#############################################
substitutions:
devicename: "esp-mainovenmonitor"
friendly_name: "esp-mainovenmonitor"
description_comment: "Oven power monitoring and disable, with a sonoff basic"
api_key: !secret esp-mainovenmonitor_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-mainovenmonitor_ota_pass #unfortunately you can't use substitutions in secrets names
mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
#update_time: 30s #update time for for temp sensors etc
#############################################
# ESPHome
#############################################
esphome:
name: ${devicename}
comment: ${description_comment} #appears on the esphome page in HA
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
#############################################
esp8266:
#board: sonoff_basic
#board: esp01_1m
board: esp8285
framework:
version: latest #recommended, latest or dev
#esp32:
# board: nodemcu-32s
# framework:
# type: arduino
# #type: esp-idf #Suggested Use ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
# version: recommended #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
#############################################
# 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
captive_portal:
#############################################
# Web Portal for display and monitoring
#############################################
web_server:
port: 80
version: 2
include_internal: true
ota: false
# auth:
# username: !secret web_server_username
# password: !secret web_server_password
#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
mqtt:
broker: !secret mqtt_server
topic_prefix: ${mqtt_topic}/${devicename}
#username: !secret mqtt_username
#password: !secret mqtt_password
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
# PEZEM4 Power Monitoring
# https://esphome.io/components/sensor/pzemac.html
#############################################
uart:
rx_pin: GPIO3
tx_pin: GPIO1
#tx_pin: RX
#rx_pin: TX
baud_rate: 9600
stop_bits: 1
modbus:
sensor:
- platform: pzemac
current:
name: "Main Oven Current"
voltage:
name: "Main Oven Voltage"
energy:
name: "Main Oven Energy"
power:
name: "Main Oven Power"
frequency:
name: "Main Oven Mains Frequency"
power_factor:
name: "Main Oven Power Factor"
update_interval: 10s
########################################
########################################
# Relay Output (on sonoff basic)
switch:
- platform: gpio
name: "Main Oven Disable"
pin: GPIO12
button:
- platform: restart
name: "Main Oven ESPHome Restart"
# LED Flashes on errors or warnings
# https://esphome.io/components/status_led.html
status_led:
pin:
number: GPIO13
inverted: true

View File

@@ -1,129 +0,0 @@
#############################################
# ESPHome
#############################################
esphome:
name: "esp-mainovenmonitor"
#############################################
# Specific board for ESPHome device
#############################################
esp8266:
board: sonoff_basic
#############################################
# ESPHome Logging Enable
#############################################
# NOTE: Baudrate set to 0 as we are using the UART with PZEM
logger:
baud_rate: 0
#############################################
# Enable the Home Assistant API
#############################################
api:
encryption:
key: !secret esp-mainovenmonitor_api_key
#############################################
# Enable Over the Air Update Capability
# Safe mode will detect boot loops
#############################################
ota:
safe_mode: true
password: !secret esp-mainovenmonitor_ota_pass
#############################################
# Use Wifi
#############################################
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Details for fallback hotspot (captive portal)
# in case wifi connection fails
ap:
ssid: "Mainovenmonitor Fallback Hotspot"
password: !secret fallback_ap_password
# manual_ip:
# static_ip: 192.168.x.x
# gateway: 192.168.X.x
# subnet: 255.255.255.0
#############################################
# Fallback captive portal
#############################################
captive_portal:
#############################################
# Web Portal for display and monitoring
#############################################
web_server:
port: 80
auth:
username: !secret web_server_username
password: !secret web_server_password
#############################################
# MQTT Monitoring
#############################################
mqtt:
broker: !secret mqtt_server
topic_prefix: esphome/esp-mainovenmonitor
username: !secret mqtt_username
password: !secret mqtt_password
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
# PEZEM4 Power Monitoring
# https://esphome.io/components/sensor/pzemac.html
#############################################
uart:
rx_pin: GPIO3
tx_pin: GPIO1
#tx_pin: RX
#rx_pin: TX
baud_rate: 9600
stop_bits: 1
modbus:
sensor:
- platform: pzemac
current:
name: "Main Oven Current"
voltage:
name: "Main Oven Voltage"
energy:
name: "Main Oven Energy"
power:
name: "Main Oven Power"
frequency:
name: "Main Oven Mains Frequency"
power_factor:
name: "Main Oven Power Factor"
update_interval: 10s
########################################
########################################
# Relay Output (on sonoff basic)
switch:
- platform: gpio
name: "Main Oven Disable"
pin: GPIO12
#button:
# - platform: restart
# name: "Main Oven ESPHome Restart"
# LED Flashes on errors or warnings
# https://esphome.io/components/status_led.html
status_led:
pin:
number: GPIO13
inverted: true

View File

@@ -0,0 +1,731 @@
#############################################
#############################################
# MASTER BATHROOM HEATED TOWEL RAIL
# Controlled by a Sonoff Basic
#
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
# V1.1 2025-04-12 Fixes to timers and offline modes
# V1.0 2025-02-14 Initial Version
#
# INSTRUCTIONS
# - It allows the device to work in a standalone timer style operation
# - The timer has a morning and evening time (but no weekday/weekend settings)
# - Default values are set, but changed values are remembered in flash
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
# - If on a network and there is a MQTT server, you can set the on/off times via MQTT (See below commands)
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
# - Any new timer times set via MQTT will be remembered though a reboot
# - On startup, or a reboot, the device will always turn on for the BOOST Duration (BOOST mode, default 2 hours)
# - TIMER mode will always be switched on after BOOST mode is complete
# - Home Assistant entities are set so that BOOST mode can be pressed with a button and other modes selectable with a dropdown
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. (Note there is no weekday/weekend setting)
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
# mqtt_timer_topic/operation/ON : Device permanently on
# mqtt_timer_topic/operation/OFF : Device permanently off
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
#
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-masterbathtowelrail"
friendly_name: "Master Bathroom Towelrail"
description_comment: "Sonoff Basic controlling ON/OFF/Timer for the Heated Towel Rail in the Master Bathroom"
device_area: "Main Bathroom" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v2.1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-masterbathtowelrail_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-masterbathtowelrail_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-masterbathtowelrail_ip
# Device Settings
relay_icon: "mdi:heating-coil"
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Timer Settings
mqtt_timer_topic: "viewroad-commands/masterbath-towelrail" # Topics you will use to change stuff
boost_duration_default: "120" # Minutes to stay ON in BOOST mode before reverting to TIMER
morning_on_default: "300" # Default in minutes from midnight. Default 05:00 => 300
morning_off_default: "420" # Default in minutes from midnight. Default 07:00 => 420
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
evening_off_default: "1439" # Default in minutes from midnight. Default 23:59 => 1439 => 1440 is midnight
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptions
- "-fno-rtti" # strip C++ RTTI
on_boot:
priority: 900 # High priority to run after globals are initialized
then:
# This evaluates if we switch on 4+ times in 20 seconds
# If so, just change to on mode permanently.
- lambda: |-
// 1) Figure out the current time in "seconds from midnight"
// using SNTP if available, otherwise current_mins * 60.
bool have_sntp = id(sntp_time).now().is_valid();
int current_time_s = 0;
if (have_sntp) {
auto now = id(sntp_time).now();
current_time_s = now.hour * 3600 + now.minute * 60 + now.second;
} else {
// current_mins is in minutes; convert to seconds
current_time_s = id(current_mins) * 60;
}
// 2) Compare with the last boot time
int diff = current_time_s - id(last_boot_time_s);
// If within 30 seconds, increment boot_count; otherwise reset to 1
if (diff >= 0 && diff <= 30) {
id(boot_count)++;
} else {
id(boot_count) = 1;
}
// Update stored last boot time
id(last_boot_time_s) = current_time_s;
// 3) If we've booted 4+ times in 20s => force ON mode
if (id(boot_count) >= 4) {
id(operation_mode) = 1; // ON
ESP_LOGI("power_cycle", "Detected 4 power cycles in 20s => Forcing ON mode");
} else {
// Otherwise do your normal startup logic:
id(operation_mode) = 3; // on_boot -> sets operation_mode = 3 (BOOST)
id(boost_timer) = 0; // and reset boost_timer = 0 (for time sync if no sntp)
ESP_LOGI("power_cycle", "Boot count=%d => BOOST mode", id(boot_count));
}
- script.execute: evaluate_relay_state # Check what the relay should be doing straight away
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: true # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: true
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
####################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
# Relay Switch (Sonoff Basic Relay on GPIO12)
####################################################
switch:
- platform: gpio
name: "Towel Rail Power"
pin: GPIO12
id: relay
restore_mode: RESTORE_DEFAULT_OFF
icon: "${relay_icon}"
##########################################################################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
# Tracks the time (in seconds from midnight) at the previous boot
- id: last_boot_time_s
type: int
restore_value: true
initial_value: "0"
# Counts how many consecutive boots have occurred (within X seconds)
- id: boot_count
type: int
restore_value: true
initial_value: "0"
# Morning On time (minutes from midnight),
- id: morning_on
type: int
restore_value: true
initial_value: "${morning_on_default}"
# Morning Off time (minutes from midnight),
- id: morning_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening On time (minutes from midnight),
- id: evening_on
type: int
restore_value: true
initial_value: "${evening_on_default}"
# Evening Off time (minutes from midnight),
- id: evening_off
type: int
restore_value: true
initial_value: "${evening_off_default}"
# Boost Duration (minutes),
- id: boost_duration
type: int
restore_value: true
initial_value: "${boost_duration_default}"
####################################################
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
####################################################
- id: operation_mode
type: int
restore_value: true
initial_value: "2"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
####################################################
- id: current_mins
type: int
restore_value: false
initial_value: "720" # 720 is 12:00 Noon
####################################################
# boost_timer: counts minutes in BOOST mode
# After 'boost_duration' minutes, revert to TIMER.
# Not restored, so each boot starts fresh at 0.
####################################################
- id: boost_timer
type: int
restore_value: false
initial_value: "0"
##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
############################
# MQTT Subscriptions
############################
############################
# Morning On time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(morning_on) = h * 60 + m;
ESP_LOGI("timer","Received new Morning On: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
############################
# Morning Off time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(morning_off) = h * 60 + m;
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
############################
# Evening On time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(evening_on) = h * 60 + m;
ESP_LOGI("timer","Received new Evening On: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
############################
# Evening Off time => "HH:MM"
############################
- platform: mqtt_subscribe
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off"
internal: true
on_value:
then:
- lambda: |-
int h = 0, m = 0;
if (sscanf(x.c_str(), "%2d:%2d", &h, &m) == 2) {
id(evening_off) = h * 60 + m;
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", h, m);
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
############################
# Boost duration => integer minutes (11439)
############################
- platform: mqtt_subscribe
name: "Boost Duration"
id: boost_time_topic
topic: "${mqtt_timer_topic}/boost-time"
internal: true
on_value:
then:
- lambda: |-
int v = 0;
// Parse as integer
if (sscanf(x.c_str(), "%d", &v) == 1 && v >= 1 && v <= 1439) {
id(boost_duration) = v;
ESP_LOGI("boost_time","Received new Boost Duration: %d mins", v);
} else {
ESP_LOGW("boost_time","Invalid boost_time '%s'", x.c_str());
}
####################################################
# Subscribe to operation mode: OFF, ON, TIMER, BOOST
####################################################
- platform: mqtt_subscribe
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation"
internal: true
on_value:
then:
- lambda: |-
// Check only the first character for mode
char c = x.c_str()[0];
if (c == 'T') { // “TIMER”
id(operation_mode) = 2;
} else if (c == 'O') { // “ON” or “OFF”
// second letter N→ON, F→OFF
id(operation_mode) = (x.size() > 1 && x[1] == 'N') ? 1 : 0;
} else if (c == 'B') { // “BOOST”
id(operation_mode) = 3;
id(boost_timer) = 0;
} else {
ESP_LOGW("timer","Invalid mode: %s", x.c_str());
}
- script.execute: evaluate_relay_state
######################################################
# Expose the current operation mode (OFF, ON, TIMER, BOOST)
######################################################
- platform: template
name: "Operation Mode State"
lambda: |-
// 0=OFF, 1=ON, 2=TIMER, 3=BOOST
switch (id(operation_mode)) {
case 0: return {"OFF"};
case 1: return {"ON"};
case 2: return {"TIMER"};
case 3: return {"BOOST"};
default: return {"UNKNOWN"};
}
update_interval: 5s
######################################################
# Expose the "Morning On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning On Time"
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Morning Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning Off Time"
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening On Time"
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening Off Time"
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
##########################################################################################
# NUMBER COMPONENT
# https://esphome.io/components/number/index.html
##########################################################################################
number:
# A value for setting operation mode via HTTP
- platform: template
name: "Operation Mode Number"
id: operation_mode_number
internal: true # ← hides it from HA
min_value: 0
max_value: 3
step: 1
set_action:
- lambda: |-
// x holds the incoming value (03)
id(operation_mode) = int(x);
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay).state) {
// Relay is ON: turn it OFF and set mode to 0 (TIMER)
id(relay).turn_off();
id(operation_mode) = 2;
} else {
// Relay is OFF: turn it ON and set mode to 3 (BOOST)
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
##########################################################################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
sensor:
- platform: template
name: "Timeclock: Boost Duration"
id: boost_duration_time
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
internal: True # No need to show this in Home Assistant
lambda: |-
return id(current_mins);
# A value in mins if a timer is running showing how many mins left
- platform: template
name: "Timer Minutes Remaining"
id: timer_minutes_remaining
unit_of_measurement: "Mins"
update_interval: 5s
accuracy_decimals: "0"
lambda: |-
// always zero if relay is off
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (BOOST)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
rem = id(boost_duration) - id(boost_timer);
}
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
- platform: template
name: "Boost now"
id: boost_button
icon: "mdi:play-circle-outline"
on_press:
# 1) reset BOOST timer and set mode
- lambda: |-
id(boost_timer) = 0;
id(operation_mode) = 3;
# 2) immediately re-evaluate relay state
- script.execute: evaluate_relay_state
- platform: template
name: "Default timer settings"
id: default_timer_settings_button
icon: "mdi:restore"
on_press:
- lambda: |-
// Restore all timing globals to their YAML defaults
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration)= ${boost_duration_default};
// Reset mode to TIMER and clear any running boost
id(operation_mode)= 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
- script.execute: evaluate_relay_state
#################################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#################################################################################################
select:
- platform: template
name: "Operation Mode"
id: operation_mode_select
update_interval: 5s
options:
- "OFF"
- "ON"
- "TIMER"
- "BOOST"
# show the current mode
lambda: |-
switch (id(operation_mode)) {
case 1: return std::string("ON");
case 2: return std::string("TIMER");
case 3: return std::string("BOOST");
default: return std::string("OFF");
}
# when changed in HA, set mode & re-evaluate
set_action:
- lambda: |-
if (x == "OFF") { id(operation_mode) = 0; }
else if (x == "ON") { id(operation_mode) = 1; }
else if (x == "TIMER") { id(operation_mode) = 2; }
else { // BOOST
id(boost_timer) = 0;
id(operation_mode) = 3;
}
- script.execute: evaluate_relay_state
#################################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
#################################################################################################
# Script: evaluate and drive the relay
script:
- id: evaluate_relay_state
then:
- lambda: |-
int mode = id(operation_mode);
// BOOST just forces the relay on
if (mode == 3) {
id(relay).turn_on();
return;
}
// OFF → always off
if (mode == 0) {
id(relay).turn_off();
return;
}
// ON → always on
if (mode == 1) {
id(relay).turn_on();
return;
}
// TIMER → follow schedule windows
{
bool should_on = false;
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off))
should_on = true;
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off))
should_on = true;
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
#################################################################################################
# INTERVAL COMPONENT
# https://esphome.io/components/interval.html
#################################################################################################
# Interval: bumps time (even if no SNTP), then calls the script to evaluate relay state
interval:
- interval: "1min"
then:
- lambda: |-
// — update current_mins via SNTP or fallback
if (!id(sntp_time).now().is_valid()) {
id(current_mins)++;
if (id(current_mins) >= 1440) id(current_mins) = 0;
} else {
auto now = id(sntp_time).now();
id(current_mins) = now.hour * 60 + now.minute;
}
// — if in BOOST, advance boost_timer and expire when done
if (id(operation_mode) == 3) {
id(boost_timer)++;
if (id(boost_timer) >= id(boost_duration)) {
id(operation_mode) = 2;
//id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
}
}
- script.execute: evaluate_relay_state

View File

@@ -0,0 +1,289 @@
#############################################
#############################################
# Mi Smart LED Desk Lamp 1S
# ESP32, LED Strip with variable white colour temp, rotary encoder
# https://www.mi.com/global/product/mi-led-desk-lamp-1s/
#############################################
#############################################
#############################################
# SECRETS
# Secrets file is usually at config/secrets.yaml
# or config/esphome/secrets.yaml
# depending on your setup with HA
#############################################
# SECRETS FILE needs these specific secrets:
# esp-midesklamp1s_api_key
# esp-midesklamp1s_ota_pass
# esp-midesklamp1s_static_ip (Optional)
# esp-midesklamp1s_gateway (Optional)
# esp-midesklamp1s_subnet (Optional)
#
# SECRETS FILE needs these generic Secrets:
# wifi_ssid
# wifi_password
# fallback_ap_password
# mqtt_server
# mqtt_username
# mqtt_password
# web_server_username (Optional)
# web_server_password (Optional)
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace
# these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: esp-midesklamp1s
friendly_name: "Mi 1S Lamp Office"
description_comment: "Office Desk Lamp with variable white colour temp and rotary encoder for brightness"
api_key: !secret esp-midesklamp1s_api_key # unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-midesklamp1s_ota_pass # unfortunately you can't use substitutions in secrets names
static_ip_address: !secret esp-midesklamp1s_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
room: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name} # appears as main name on the esphome page in HA
comment: ${description_comment} # appears as comment on the esphome page in HA
area: ${room}
#min_version: 2024.6.0
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp32:
board: esp32doit-devkit-v1
framework:
type: esp-idf # Suggest using the ESP-IDF Framework. Changing from 'arduino' to 'esp-idf' needs a cabled flash to correct partitions
version: recommended # recommended, latest or dev
# these just for the esp32doit-devkit-v1 and Mi S Lamp
sdkconfig_options:
CONFIG_FREERTOS_UNICORE: y
advanced:
ignore_efuse_mac_crc: true
ignore_efuse_custom_mac: true
#############################################
# ESPHome Logging Enabl
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} # 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
#############################################
# Time Component
# https://esphome.io/components/time/index.html#time-component
# Sync with HA Time. Probably not really necessary.
#############################################
#time:
# - platform: homeassistant
# id: homeassistant_time
#############################################
# General Sensors
# https://esphome.io/components/sensor/index.html
#############################################
sensor:
# Mi Desk Lamp 1S Configuration
- platform: rotary_encoder
id: rotation
pin_a: GPIO27
pin_b: GPIO26
resolution: 2
on_value:
then:
- if:
condition:
# Check if Button is pressed while rotating
lambda: "return id(pushbutton).state;"
then:
# If Button is pressed, change CW/WW
- lambda: |-
auto min_temp = id(light1).get_traits().get_min_mireds();
auto max_temp = id(light1).get_traits().get_max_mireds();
auto cur_temp = id(light1).current_values.get_color_temperature();
auto new_temp = max(min_temp, min(max_temp, cur_temp + (x*10)));
auto call = id(light1).turn_on();
call.set_color_temperature(new_temp);
call.perform();
else:
# If Button is not pressed, change brightness
- light.dim_relative:
id: light1
relative_brightness: !lambda |-
return x / 25.0;
# Reset Rotation to 0
- sensor.rotary_encoder.set_value:
id: rotation
value: 0
#############################################
# Binary Sensors
# https://esphome.io/components/binary_sensor/index.html
#############################################
binary_sensor:
- platform: gpio
id: pushbutton
pin:
number: GPIO33
inverted: True
mode: INPUT_PULLDOWN
on_click:
then:
- light.toggle:
id: light1
transition_length: 0.2s
#############################################
# Base Output Component
# https://esphome.io/components/output/index.html#base-output-configuration
#############################################
output:
- platform: ledc # Cool White LED PWM
pin: GPIO2
id: output_cw
frequency: 40000Hz
power_supply: power
- platform: ledc
pin: GPIO4 # Warm White LED PWM
id: output_ww
power_supply: power
frequency: 40000Hz
#############################################
# Power Supply Component
# https://esphome.io/components/power_supply.html#power-supply-component
#############################################
power_supply:
- id: power
pin: GPIO12
enable_time: 0s
keep_on_time: 0s
#############################################
# Light Component
# https://esphome.io/components/light/index.html#light-component
#############################################
light:
- platform: cwww # https://esphome.io/components/light/cwww.html#cold-white-warm-white-light
id: light1
default_transition_length: 0s
constant_brightness: true
name: "${friendly_name} Light"
cold_white: output_cw
warm_white: output_ww
cold_white_color_temperature: 4800 K
warm_white_color_temperature: 2500 K # 2500k is the original value of the lamp. To correct binning for 2700k to look more like 2700k use 2650k instead
restore_mode: ALWAYS_OFF
gamma_correct: 0
#globals:
# - id: fade_brightness
# type: float
# initial_value: '0.0'
# - id: fade_step
# type: float
# initial_value: '0.0'
# This script fades the light ON to full brightness over 20 seconds.
#script:
# - id: fade_on
# then:
# # First, capture the current brightness.
# - lambda: |-
# // If the light is off, current brightness will be 0.0.
# float current = id(light1).current_values.get_brightness();
# id(fade_brightness) = current;
# // Compute the step size so that after 100 steps (200ms each) we reach full brightness.
# id(fade_step) = (1.0 - current) / 100.0;
# - repeat:
# count: 100
# then:
# - lambda: |-
# id(fade_brightness) += id(fade_step);
# if (id(fade_brightness) > 1.0) {
# id(fade_brightness) = 1.0;
# }
# // Update the lights brightness.
# id(light1).turn_on({ brightness: id(fade_brightness) });
# - delay: 200ms
# This script fades the light OFF over 20 seconds.
# - id: fade_off
# then:
# - lambda: |-
# // Capture current brightness (if the light is on).
# float current = id(light1).current_values.get_brightness();
# id(fade_brightness) = current;
# // Compute step size so that after 100 steps we reach 0.
# id(fade_step) = current / 100.0;
# - repeat:
# count: 100
# then:
# - lambda: |-
# id(fade_brightness) -= id(fade_step);
# if (id(fade_brightness) < 0.0) {
# id(fade_brightness) = 0.0;
# }
# id(light1).turn_on({ brightness: id(fade_brightness) });
# - delay: 200ms
# # Finally, ensure the light is turned off.
# - lambda: |-
# id(light1).turn_off();
#switch:
# - platform: template
# name: "Fade on and off"
# id: virtual_switch
# turn_on_action:
# - script.execute: fade_on
# turn_off_action:
# - script.execute: fade_off

View File

@@ -0,0 +1,86 @@
#############################################
#############################################
#
#
#
# V1.0 2025-02-14 Initial Version
#
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# INSTRUCTIONS
# -
#
#############################################
#############################################
substitutions:
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
devicename: "esp-nexmulti1"
friendly_name: "esp-nexmulti1"
description_comment: "Multisensor 1 test"
api_key: !secret esp-nexmulti1_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-nexmulti1_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-nexmulti1_ip
update_interval: 60s # update time for for general sensors etc
#############################################
# Included Common Packages
# https://esphome.io/components/esphome.html
#############################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_static_ip_address: ${static_ip_address}
local_api_key: ${api_key}
local_ota_pass: ${ota_pass}
common_mqtt: !include
file: common/mqtt_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: ${friendly_name}
local_update_interval: ${update_interval}
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${devicename}
friendly_name: ${friendly_name}
comment: ${description_comment} #Appears on the esphome page in HA
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
#############################################
# 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
#############################################
# Real time clock time source for ESPHome
# If it's invalid, we fall back to an internal clock
# https://esphome.io/components/time/
# https://esphome.io/components/time/sntp
#############################################
#time:
# - platform: sntp
# id: sntp_time

View File

@@ -0,0 +1,477 @@
#############################################
#############################################
#
# ESP Home
# NFC Tag Reader for Home Assistant
#
#############################################
#############################################
# Project Link:
# https://github.com/adonno/tagreader
#############################################
# Remember to set the switches on the PN532
# for i2C, Switch1: On (up), Switch2: Off (down)
#############################################
#############################################
# Substitution Text
#############################################
substitutions:
name: esp-nfcaccess1-33ad74
friendly_name: TagReader
#############################################
# Inport the project from Github
#############################################
#dashboard_import:
# package_import_url: github://adonno/tagreader/tagreader.yaml
#############################################
# Use Wifi
#############################################
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Details for fallback hotspot (captive portal)
# in case wifi connection fails
ap:
ssid: "${name} Hotspot"
password: !secret fallback_ap_password
#############################################
# Improv
# Use the open Improv standard for configuring Wi-Fi on the
# device by using a serial connection to the device, eg. USB.
#############################################
improv_serial:
#############################################
# Fallback captive portal
# Enable the captive portal for inital WiFi setup
#############################################
captive_portal:
#############################################
# Enable Over the Air Update Capability
# Safe mode will detect boot loops
#############################################
ota:
safe_mode: true # Avoid boot loops
password: !secret esp-nfcaccess1-33ad74_ota_pass
#############################################
# ESPHome Logging Enable
#############################################
logger:
# baud_rate: 0 # Set baud rate to 0 if using the UART for soething else
# level: VERY_VERBOSE
# level: VERBOSE
#############################################
# Web Portal for display and monitoring
#############################################
#web_server:
# port: 80
# auth:
# username: !secret web_server_username
# password: !secret web_server_password
########################################
# Specific board for ESPHome device
########################################
esp8266:
board: d1_mini
#############################################
# ESPHome
#############################################
esphome:
name: ${name}
# Automatically add the mac address to the name
# so you can use a single firmware for all devices
name_add_mac_suffix: false
# This will allow for (future) project identification,
# configuration and updates.
project:
name: adonno.tag_reader
version: "1.4"
# If buzzer is enabled, notify on api connection success
on_boot:
priority: -10
then:
- wait_until:
api.connected:
- logger.log: API is connected!
- rtttl.play: "success:d=24,o=5,b=100:c,g,b"
- light.turn_on:
id: activity_led
brightness: 100%
red: 0%
green: 0%
blue: 100%
flash_length: 500ms
- switch.turn_on: buzzer_enabled
- switch.turn_on: led_enabled
#############################################
# Global variables
#############################################
globals:
- id: source
type: std::string
- id: url
type: std::string
- id: info
type: std::string
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
#############################################
# Switches
# Define switches to control LED and buzzer from HA
#############################################
switch:
- platform: template
name: "${friendly_name} Buzzer Enabled"
id: buzzer_enabled
icon: mdi:volume-high
optimistic: true
restore_state: true
entity_category: config
- platform: template
name: "${friendly_name} LED enabled"
id: led_enabled
icon: mdi:alarm-light-outline
optimistic: true
restore_state: true
entity_category: config
#############################################
# Buttons
# Define buttons for writing tags via HA
#############################################
button:
- platform: template
name: Write Tag Random
id: write_tag_random
# Optional variables:
icon: "mdi:pencil-box"
on_press:
then:
- light.turn_on:
id: activity_led
brightness: 100%
red: 100%
green: 0%
blue: 100%
- lambda: |-
static const char alphanum[] = "0123456789abcdef";
std::string uri = "https://www.home-assistant.io/tag/";
for (int i = 0; i < 8; i++)
uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
uri += "-";
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 4; i++)
uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
uri += "-";
}
for (int i = 0; i < 12; i++)
uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
auto message = new nfc::NdefMessage();
message->add_uri_record(uri);
ESP_LOGD("tagreader", "Writing payload: %s", uri.c_str());
id(pn532_board).write_mode(message);
- rtttl.play: "write:d=24,o=5,b=100:b"
- wait_until:
not:
pn532.is_writing:
- light.turn_off:
id: activity_led
- rtttl.play: "write:d=24,o=5,b=100:b,b"
- platform: template
name: Clean Tag
id: clean_tag
icon: "mdi:nfc-variant-off"
on_press:
then:
- light.turn_on:
id: activity_led
brightness: 100%
red: 100%
green: 64.7%
blue: 0%
- lambda: 'id(pn532_board).clean_mode();'
- rtttl.play: "write:d=24,o=5,b=100:b"
- wait_until:
not:
pn532.is_writing:
- light.turn_off:
id: activity_led
- rtttl.play: "write:d=24,o=5,b=100:b,b"
- platform: template
name: Cancel writing
id: cancel_writing
icon: "mdi:pencil-off"
on_press:
then:
- lambda: 'id(pn532_board).read_mode();'
- light.turn_off:
id: activity_led
- rtttl.play: "write:d=24,o=5,b=100:b,b"
- platform: restart
name: "${friendly_name} Restart"
entity_category: config
#############################################
# HA API
# Enable Home Assistant API
#############################################
api:
encryption:
key: "K2H5vBNv8NLegc3RmwTQVFV7ULtba2DWDOlABmXmGBM="
services:
- service: rfidreader_tag_ok
then:
- rtttl.play: "beep:d=16,o=5,b=100:b"
- service: rfidreader_tag_ko
then:
- rtttl.play: "beep:d=8,o=5,b=100:b"
- service: play_rtttl
variables:
song_str: string
then:
- rtttl.play: !lambda 'return song_str;'
- service: write_tag_id
variables:
tag_id: string
then:
- light.turn_on:
id: activity_led
brightness: 100%
red: 100%
green: 0%
blue: 0%
- lambda: |-
auto message = new nfc::NdefMessage();
std::string uri = "https://www.home-assistant.io/tag/";
uri += tag_id;
message->add_uri_record(uri);
id(pn532_board).write_mode(message);
- rtttl.play: "write:d=24,o=5,b=100:b"
- wait_until:
not:
pn532.is_writing:
- light.turn_off:
id: activity_led
- rtttl.play: "write:d=24,o=5,b=100:b,b"
- service: write_music_tag
variables:
music_url: string
music_info: string
then:
- light.turn_on:
id: activity_led
brightness: 100%
red: 100%
green: 0%
blue: 0%
- lambda: |-
auto message = new nfc::NdefMessage();
std::string uri = "";
std::string text = "";
uri += music_url;
text += music_info;
if ( music_url != "" ) {
message->add_uri_record(uri);
}
if ( music_info != "" ) {
message->add_text_record(text);
}
id(pn532_board).write_mode(message);
- rtttl.play: "write:d=24,o=5,b=100:b"
- wait_until:
not:
pn532.is_writing:
- light.turn_off:
id: activity_led
- rtttl.play: "write:d=24,o=5,b=100:b,b"
#############################################
# i2C bus
#############################################
i2c:
scan: False
frequency: 400kHz
#############################################
# NFC Reader
#############################################
pn532_i2c:
id: pn532_board
on_tag:
then:
- if:
condition:
switch.is_on: led_enabled
then:
- light.turn_on:
id: activity_led
brightness: 100%
red: 0%
green: 100%
blue: 0%
flash_length: 500ms
- delay: 0.15s #to fix slow component
- lambda: |-
id(source)="";
id(url)="";
id(info)="";
if (tag.has_ndef_message()) {
auto message = tag.get_ndef_message();
auto records = message->get_records();
for (auto &record : records) {
std::string payload = record->get_payload();
std::string type = record->get_type();
size_t hass = payload.find("https://www.home-assistant.io/tag/");
size_t applemusic = payload.find("https://music.apple.com");
size_t spotify = payload.find("https://open.spotify.com");
size_t sonos = payload.find("sonos-2://");
if (type == "U" and hass != std::string::npos ) {
ESP_LOGD("tagreader", "Found Home Assistant tag NDEF");
id(source)="hass";
id(url)=payload;
id(info)=payload.substr(hass + 34);
}
else if (type == "U" and applemusic != std::string::npos ) {
ESP_LOGD("tagreader", "Found Apple Music tag NDEF");
id(source)="amusic";
id(url)=payload;
}
else if (type == "U" and spotify != std::string::npos ) {
ESP_LOGD("tagreader", "Found Spotify tag NDEF");
id(source)="spotify";
id(url)=payload;
}
else if (type == "U" and sonos != std::string::npos ) {
ESP_LOGD("tagreader", "Found Sonos app tag NDEF");
id(source)="sonos";
id(url)=payload;
}
else if (type == "T" ) {
ESP_LOGD("tagreader", "Found music info tag NDEF");
id(info)=payload;
}
else if ( id(source)=="" ) {
id(source)="uid";
}
}
}
else {
id(source)="uid";
}
- if:
condition:
lambda: 'return ( id(source)=="uid" );'
then:
- homeassistant.tag_scanned: !lambda |-
ESP_LOGD("tagreader", "No HA NDEF, using UID");
return x;
else:
- if:
condition:
lambda: 'return ( id(source)=="hass" );'
then:
- homeassistant.tag_scanned: !lambda 'return id(info);'
else:
- homeassistant.event:
event: esphome.music_tag
data:
reader: !lambda |-
return App.get_name().c_str();
source: !lambda |-
return id(source);
url: !lambda |-
return id(url);
info: !lambda |-
return id(info);
- if:
condition:
switch.is_on: buzzer_enabled
then:
- rtttl.play: "success:d=24,o=5,b=100:c,g,b"
on_tag_removed:
then:
- homeassistant.event:
event: esphome.tag_removed
#############################################
# OUTPUTS
#############################################
# Define the buzzer output
output:
- platform: esp8266_pwm
pin: D7
id: buzzer
#############################################
# BINARY SENSORS
#############################################
binary_sensor:
- platform: status
name: "${friendly_name} Status"
entity_category: diagnostic
#############################################
# TEXT SENSORS
#############################################
text_sensor:
- platform: version
hide_timestamp: true
name: "${friendly_name} ESPHome Version"
entity_category: diagnostic
- platform: wifi_info
ip_address:
name: "${friendly_name} IP Address"
icon: mdi:wifi
entity_category: diagnostic
ssid:
name: "${friendly_name} Connected SSID"
icon: mdi:wifi-strength-2
entity_category: diagnostic
#############################################
# Ring Tone Text Transfer Language for melody
#############################################
# Define buzzer as output for RTTTL
rtttl:
output: buzzer
#############################################
# LED
#############################################
# Configure LED
light:
- platform: neopixelbus
variant: WS2812
pin: D8
num_leds: 1
flash_transition_length: 500ms
type: GRB
id: activity_led
name: "${friendly_name} LED"
restore_mode: ALWAYS_OFF

View File

@@ -0,0 +1,456 @@
#############################################
#############################################
# HiLink LD1125H mmWave sensor, with BME280 Temp/Hum/Pres Sensor on an ESP32
# https://zorruno.com/2024/mmwave-occupancy-with-esp32-ld1125h/
#
# https://github.com/patrick3399/Hi-Link_mmWave_Radar_ESPHome/tree/main
# https://github.com/patrick3399/Hi-Link_mmWave_Radar_ESPHome/blob/main/LD1125H/ESP32-LD1125H-Complete.yaml
#
# mth1: 0 to 2.8m sensitive
# mth2: 2.8 to 8m sensitive
# mth3: above 8m sensitive
# rmax: max distance
# Clearance Time: Mov/Occ to Clearance waiting time
# Movement Time: Mov to Occ waiting time
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-occupancyoffice"
friendly_name: "Office Occupancy & Environment"
description_comment: "D1 Mini ESP32 with LD1125H mmWave and environment sensors for downstairs office"
api_key: !secret esp-occupancyoffice_api_key #unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-occupancyoffice_ota_pass #unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-occupancyoffice_ip
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
device_area: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
#############################################
# SPECIFIC PROJECT VARIABLE SUBSTITUTIONS
#############################################
#
#
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} #appears on the esphome page in HA
min_version: 2024.6.0
area: "${device_area}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptions
#- "-fno-rtti" # strip C++ RTTI
on_boot:
- priority: -200
then:
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string th1st = "mth1=" + str_sprintf("%.0f", id(LD1125H_mth1).state) + "\r\n";
return std::vector<uint8_t>(th1st.begin(), th1st.end());
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string th2st = "mth2=" + str_sprintf("%.0f", id(LD1125H_mth2).state) + "\r\n";
return std::vector<uint8_t>(th2st.begin(), th2st.end());
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string th3st = "mth3=" + str_sprintf("%.0f", id(LD1125H_mth3).state) + "\r\n";
return std::vector<uint8_t>(th3st.begin(), th3st.end());
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string rmaxst = "rmax=" + str_sprintf("%.1f", id(LD1125H_rmax).state) + "\r\n";
return std::vector<uint8_t>(rmaxst.begin(), rmaxst.end());
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp32:
board: esp32dev
framework:
#type: arduino
type: esp-idf #Suggest using the ESP-IDF Framework. Changing from arduino to esp-idf needs a cabled download to change partitions
version: recommended #recommended, latest or dev
#############################################
# ESPHome external or custom components to use
# https://esphome.io/components/external_components.html
# https://github.com/ssieb/esphome_components/tree/master/components/serial
#############################################
external_components:
- source:
type: git
url: https://github.com/ssieb/custom_components #Thanks for @ssieb components.
components: [ serial ] #text_sensor that reads lines for a uart. Also, a sensor that reads single binary values from the uart.
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: ${log_level} #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
#############################################
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
encryption:
key: ${api_key}
#key: "puCd6EGmFp3hU56N8dOo5u17bXwDr0aVRWiDoNdPDoE="
on_client_connected:
- esp32_ble_tracker.start_scan:
continuous: true
on_client_disconnected:
- esp32_ble_tracker.stop_scan:
#############################################
# i2c bus
# https://esphome.io/components/i2c.html
# 10, 50, 100, 200, 800 are possible settings
# for frequency, 50kHz is default
#############################################
i2c:
sda: GPIO19
scl: GPIO21
scan: True #look for devices on boot up and report
frequency: 50kHz
#############################################
# UART Serial
# hardware on EPS32, but software, and can be glitchy on ESP8266
# https://esphome.io/components/uart.html
#############################################
uart:
id: LD1125H_UART_BUS
rx_pin: GPIO16 #For ESP32, you can use any pin, Recommend Use UART_2, Don't use UART_0, It might Cause Boot Fail or System Hang
tx_pin: GPIO17 #For ESP32, you can use any pin, Recommend Use UART_2, Don't use UART_0, It might Cause Boot Fail or System Hang
baud_rate: 115200
data_bits: 8
stop_bits: 1
parity: NONE
#############################################
# Bluetooth
# https://esphome.io/components/bluetooth_proxy.html
# https://esphome.io/components/esp32_ble_tracker.html
# Remember that this takes a LOT of processing. On the
# ESP32, enable the IDF framework, and disable the
# Web server component. Changing to the IDF framework
# needs to be via cable not OTA to change the
# partition setup.
#############################################
bluetooth_proxy:
active: true
cache_services: true
esp32_ble_tracker:
scan_parameters:
continuous: false
#############################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
#############################################
globals:
- id: LD1125H_Last_Time
type: time_t
restore_value: no
#initial_value: time(NULL)
#initial_value: !lambda 'return ::time(nullptr);'
initial_value: "0"
- id: LD1125H_Last_Mov_Time
type: time_t
restore_value: no
#initial_value: time(NULL)
#initial_value: !lambda 'return ::time(nullptr);'
initial_value: "0"
- id: LD1125H_Clearence_Status
type: bool
restore_value: no
initial_value: "false"
#############################################
# General esp status LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2 #ESP32 Onboard LED
ignore_strapping_warning: True #https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
inverted: false
#############################################
# Interval Automations
# https://esphome.io/guides/automations.html
#############################################
interval:
- interval: 1s # Clearance Scan Time
setup_priority: -200
then:
- lambda: |-
// Use the global C time() function (::time(nullptr))
if ((::time(nullptr) - id(LD1125H_Last_Time)) > id(LD1125H_Clear_Time).state) {
if ((id(LD1125H_Clearence_Status) == false) ||
(id(LD1125H_Occupancy).state != "Clearance")) {
id(LD1125H_Occupancy).publish_state("Clearance");
id(LD1125H_Clearence_Status) = true;
}
if (id(LD1125H_MovOcc_Binary).state == true) {
id(LD1125H_MovOcc_Binary).publish_state(false);
}
if (id(LD1125H_Mov_Binary).state == true) {
id(LD1125H_Mov_Binary).publish_state(false);
}
// Update the last-seen timestamp
id(LD1125H_Last_Time) = ::time(nullptr);
}
#############################################
# Number Sensors (custom component)
# refer https://github.com/ssieb/esphome_components/tree/master/components/serial
#############################################
number:
- platform: template
name: ${friendly_name} LD1125H mth1 #mth1 is 0~2.8m Sensitivity.
id: LD1125H_mth1
icon: "mdi:cogs"
optimistic: true
restore_value: true #If you don't want to store the setting at ESP, set it to false.
initial_value: "60.0" #Default mth1 Setting
min_value: 10.0
max_value: 600.0
step: 5.0
set_action:
then:
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string th1st = "mth1=" + str_sprintf("%.0f",x) +"\r\n";
return std::vector<uint8_t>(th1st.begin(), th1st.end());
- platform: template
name: ${friendly_name} LD1125H mth2 #mth2 is 2.8~8m Sensitivity.
id: LD1125H_mth2
icon: "mdi:cogs"
optimistic: true
restore_value: true #If you don't want to store the setting at ESP, set it to false.
initial_value: "30" #Default mth2 Setting
min_value: 5
max_value: 300
step: 5
set_action:
then:
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string th2st = "mth2=" + str_sprintf("%.0f",x) +"\r\n";
return std::vector<uint8_t>(th2st.begin(), th2st.end());
- platform: template
name: ${friendly_name} LD1125H mth3 #mth3 is above 8m Sensitivity.
id: LD1125H_mth3
icon: "mdi:cogs"
optimistic: true
restore_value: true #If you don't want to store the setting at ESP, set it to false.
initial_value: "20" #Default mth3 Setting
min_value: 5
max_value: 200
step: 5
set_action:
then:
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string th3st = "mth3=" + str_sprintf("%.0f",x) +"\r\n";
return std::vector<uint8_t>(th3st.begin(), th3st.end());
- platform: template
name: ${friendly_name} LD1125H rmax #rmax is max detection distance.
id: LD1125H_rmax
icon: "mdi:cogs"
optimistic: true
restore_value: true #If you don't want to store the setting at ESP, set it to false.
initial_value: "8" #Default rmax Setting
min_value: 0.4
max_value: 12
step: 0.1
set_action:
then:
- uart.write:
id: LD1125H_UART_BUS
data: !lambda |-
std::string rmaxst = "rmax=" + str_sprintf("%.1f",x) +"\r\n";
return std::vector<uint8_t>(rmaxst.begin(), rmaxst.end());
- platform: template
name: ${friendly_name} LD1125H Clearence Time
id: LD1125H_Clear_Time
icon: "mdi:cogs"
optimistic: true
restore_value: true #If you don't want to store the setting at ESP, set it to false.
initial_value: "5" #LD1125H Mov/Occ > Clearence Time Here
min_value: 0.5
max_value: 20
step: 0.5
- platform: template
name: ${friendly_name} LD1125H Movement Time
id: LD1125H_Mov_Time
icon: "mdi:cogs"
optimistic: true
restore_value: true #If you don't want to store the setting at ESP, set it to false.
initial_value: "1" #LD1125H Mov > Occ Time Here
min_value: 0.5
max_value: 10
step: 0.5
#############################################
# General Sensors
# https://esphome.io/components/sensor/index.html
#############################################
sensor:
- platform: bme280_i2c
temperature:
name: "Temperature"
accuracy_decimals: 1
oversampling: 2x
pressure:
name: "Pressure"
oversampling: 2x
humidity:
name: "Humidity"
accuracy_decimals: 1
oversampling: 2x
address: 0x76
update_interval: ${update_interval}
- platform: template
name: "mmWave Distance"
id: LD1125H_Distance
icon: "mdi:signal-distance-variant"
unit_of_measurement: "m"
accuracy_decimals: 2
filters: # Use Fliter To Debounce
- sliding_window_moving_average:
window_size: 8
send_every: 2
- heartbeat: 0.2s
#############################################
# Text Sensors
# refer https://esphome.io/components/text_sensor/index.html
#############################################
text_sensor:
- platform: serial
uart_id: LD1125H_UART_BUS
name: ${friendly_name} LD1125H UART Text
id: LD1125H_UART_Text
icon: "mdi:format-text"
internal: True
on_value:
lambda: |-
if (id(LD1125H_UART_Text).state.substr(0,3) == "occ") {
id(LD1125H_Distance).publish_state(
atof(id(LD1125H_UART_Text).state.substr(9).c_str())
);
if ((::time(nullptr) - id(LD1125H_Last_Mov_Time)) >
id(LD1125H_Mov_Time).state) {
id(LD1125H_Occupancy).publish_state("Occupancy");
if (id(LD1125H_MovOcc_Binary).state == false) {
id(LD1125H_MovOcc_Binary).publish_state(true);
}
if (id(LD1125H_Mov_Binary).state == true) {
id(LD1125H_Mov_Binary).publish_state(false);
}
}
if (id(LD1125H_MovOcc_Binary).state == false) {
id(LD1125H_MovOcc_Binary).publish_state(true);
}
// Update both last-movement and last-anything timestamps
id(LD1125H_Last_Time) = ::time(nullptr);
if (id(LD1125H_Clearence_Status) == true) {
id(LD1125H_Clearence_Status) = false;
}
}
else if (id(LD1125H_UART_Text).state.substr(0,3) == "mov") {
id(LD1125H_Distance).publish_state(
atof(id(LD1125H_UART_Text).state.substr(9).c_str())
);
id(LD1125H_Occupancy).publish_state("Movement");
if (id(LD1125H_MovOcc_Binary).state == false) {
id(LD1125H_MovOcc_Binary).publish_state(true);
}
if (id(LD1125H_Mov_Binary).state == false) {
id(LD1125H_Mov_Binary).publish_state(true);
}
// Update both movement-specific and general timestamps
id(LD1125H_Last_Mov_Time) = ::time(nullptr);
id(LD1125H_Last_Time) = ::time(nullptr);
if (id(LD1125H_Clearence_Status) == true) {
id(LD1125H_Clearence_Status) = false;
}
}
- platform: template
name: "mmWave Occupancy"
id: LD1125H_Occupancy
icon: "mdi:motion-sensor"
#############################################
# Binary Sensors
# https://esphome.io/components/binary_sensor/index.html
#############################################
binary_sensor:
- platform: template
name: "mmWave Occupancy or Movement"
id: LD1125H_MovOcc_Binary
device_class: occupancy
- platform: template
name: "mmWave Movement"
id: LD1125H_Mov_Binary
device_class: motion

View File

@@ -0,0 +1,525 @@
##########################################################################################
##########################################################################################
# HiLink LD2410 mmWave sensor, with BME280 Temp/Hum/Pres Sensor and PIR on an ESP32
# VERSION
# V2.1 2025-08-25 Added some MQTT to send commands to turn on remote lights
# V2.0 2025-06-05 YAML Tidyups
#
# https://github.com/patrick3399/Hi-Link_mmWave_Radar_ESPHome/tree/main
# https://github.com/patrick3399/Hi-Link_mmWave_Radar_ESPHome/blob/main/LD1125H/ESP32-LD1125H-Complete.yaml
#
# https://esphome.io/components/sensor/ld2410.html
# https://www.simplysmart.house/blog/presence-detection-ld2410-home-assistant
#
# The B and C versions of this device can use Bluetooth, but we are not using it here.
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-occupancystair"
friendly_name: "Stair Occupancy and Underhouse Environment"
description_comment: "D1 Mini ESP32 with LD2410 mmWave for internal stairwell and environment sensors for under house"
device_area: "Underhouse" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Generic.ESP32" # Project Details
project_version: "v2.1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets (unfortunately you can't use substitutions inside secrets names)
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-occupancystair_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT REMOTE Controls
mqtt_remote_device1_name: "stair-footerlights"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/set"
#mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}/state"
mqtt_remote_device_command_ON: "ON"
mqtt_remote_device_command_OFF: "OFF"
#########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome CORE CONFIGURATION
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # appears on the esphome page in HA
min_version: 2024.6.0
area: "${device_area}"
#on_boot: # Initial Setting, will remember previous values (if set)
#priority: -200
#then:
##########################################################################################
# ESP PLATFORM AND FRAMEWORK
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
##########################################################################################
esp32:
board: esp32dev
framework:
type: esp-idf # "esp-idf" OR "arduino". Suggested ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended # recommended, latest or dev
##########################################################################################
# GLOBAL VARIABLES
# https://esphome.io/components/globals.html
##########################################################################################
globals:
- id: stair_footer_auto_default_s
type: int
restore_value: yes
initial_value: '20'
##########################################################################################
# i2c BUS COMPONENT
# https://esphome.io/components/i2c.html
##########################################################################################
i2c:
sda: GPIO19
scl: GPIO21
scan: True
frequency: 100kHz #10, 50, 100, 200, 800 are possible settings, 100kHz was reliable for me
##########################################################################################
# LOGGER COMPONENT
# https://esphome.io/components/logger.html
# Logs all log messages through the serial port and through MQTT topics.
##########################################################################################
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
##########################################################################################
# BLUETOOTH
# Proxy https://esphome.io/components/bluetooth_proxy.html
# BLE https://esphome.io/components/esp32_ble_tracker.html
# Remember that this takes a LOT of processing. On the
# ESP32, enable the IDF framework, and disable the
# Web server component. Changing to the IDF framework
# needs to be via cable not OTA to change the
# partition setup.
##########################################################################################
#bluetooth_proxy:
#esp32_ble_tracker:
##########################################################################################
# UART BUS
# hardware on EPS32, but software (and can be glitchy) on ESP8266
# https://esphome.io/components/uart.html
##########################################################################################
uart:
id: ld2410_uart
rx_pin: GPIO16 #For ESP32, you can use any pin, Recommend Use UART_2, Don't use UART_0, It might Cause Boot Fail or System Hang
tx_pin: GPIO17 #For ESP32, you can use any pin, Recommend Use UART_2, Don't use UART_0, It might Cause Boot Fail or System Hang
baud_rate: 256000 # default for LD2410 is 25600, 8, 0, NONE
data_bits: 8
stop_bits: 1
parity: NONE
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
# ESP32 D1 Mini Board: Onboard Status LED on GPIO2, active-low
#########################################################################################
status_led:
pin:
number: GPIO2 # ESP32 Onboard LED
ignore_strapping_warning: True #https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
inverted: false
##########################################################################################
# LD2410 Sensors
# https://esphome.io/components/sensor/ld2410.html
# https://www.hlktech.net/index.php?id=988
##########################################################################################
ld2410:
uart_id: ld2410_uart
#uart_id (Optional, ID): Manually specify the ID of the UART Component if you want to use multiple UART buses.
#throttle (Optional, int): Time in milliseconds to control the rate of data updates. Defaults to 1000ms.
#id (Optional, ID): Manually specify the ID for this LD2410 Sensor component if you need multiple components.
# light (Optional, int): When in engineering mode, indicates the light sensitivity, otherwise unknown. Value between 0 and 255 inclusive. Though it seems that the value 85 is the lowest value at complete darkness. All options from Sensor.
# moving_distance (Optional, int): Distance in cm of detected moving target. All options from Sensor.
# still_distance (Optional, int): Distance in cm of detected still target. All options from Sensor.
# moving_energy (Optional, int): Energy for moving target. Value between 0 and 100 inclusive. All options from Sensor.
# still_energy (Optional, int): Energy for still target. Value between 0 and 100 inclusive. All options from Sensor.
# detection_distance (Optional, int): Distance in cm of target. All options from Sensor.
# gX (Optional): Energies for the Xth gate (X => 0 to 8).
# move_energy (Optional, int): When in engineering mode, the move energy of the gate, otherwise unknown. Value between 0 and 100 inclusive. All options from Sensor.
#still_energy (Optional, int): When in engineering mode, the still energy of the gate, otherwise unknown. Value between 0 and 100 inclusive. All options from Sensor.
#ld2410_id (Optional, ID): Manually specify the ID for the LD2410 Sensor component if you are using multiple components.
##########################################################################################
# NUMBER COMPONENT
# https://esphome.io/components/number/
##########################################################################################
#The ld2410 number values for setting thresholds
# timeout: 5s
# max_move_distance: 2.25m
# max_still_distance: 2.25m
# g0_move_threshold: 40 # 0m / 0'
# g0_still_threshold: 10 # 0m / 0'
# g1_move_threshold: 40 # 0 - 0.75m / 0 - 2.46'
# g1_still_threshold: 10 # 0 - 0.75m / 0 - 2.46'
# g2_move_threshold: 40 # 0.75 - 1.5m / 2.46' - 4.92'
# g2_still_threshold: 10 # 0.75 - 1.5m / 2.46' - 4.92'
# g3_move_threshold: 40 # 1.5 - 2.25m / 4.92' - 7.38'
# g3_still_threshold: 10 # 1.5 - 2.25m / 4.92' - 7.38'
# g4_move_threshold: 40 # 2.25 - 3m' / 7.38' - 9.84'
# g4_still_threshold: 40 # 2.25 - 3m' / 7.38' - 9.84'
# g5_move_threshold: 40 # 3 - 3.75 / 9.84' - 12.30'
# g5_still_threshold: 40 # 3 - 3.75 / 9.84' - 12.30'
# g6_move_threshold: 30 # 3.75 - 4.5m / 12.30' - 14.76'
# g6_still_threshold: 15 # 3.75 - 4.5m / 12.30' - 14.76'
# g7_move_threshold: 30 # 4.5 - 5.25m / 14.76' - 17.22'
# g7_still_threshold: 15 # 4.5 - 5.25m / 14.76' - 17.22'
# g8_move_threshold: 30 # 5.25 - 6m / 17.22' - 19.68'
# g8_still_threshold: 15 # 5.25 - 6m / 17.22' - 19.68'
number:
- platform: ld2410
timeout:
name: "Timeout"
light_threshold:
name: "Light Threshold"
max_move_distance_gate:
name: "Max Move Distance Gate"
max_still_distance_gate:
name: "Max Still Distance Gate"
g0:
move_threshold:
name: "g0 move threshold"
still_threshold:
name: "g0 still threshold"
g1:
move_threshold:
name: "g1 move threshold"
still_threshold:
name: "g1 still threshold"
g2:
move_threshold:
name: "g2 move threshold"
still_threshold:
name: "g2 still threshold"
g3:
move_threshold:
name: "g3 move threshold"
still_threshold:
name: "g3 still threshold"
g4:
move_threshold:
name: "g4 move threshold"
still_threshold:
name: "g4 still threshold"
g5:
move_threshold:
name: "g5 move threshold"
still_threshold:
name: "g5 still threshold"
g6:
move_threshold:
name: "g6 move threshold"
still_threshold:
name: "g6 still threshold"
g7:
move_threshold:
name: "g7 move threshold"
still_threshold:
name: "g7 still threshold"
g8:
move_threshold:
name: "g8 move threshold"
still_threshold:
name: "g8 still threshold"
- platform: template
id: stair_footer_auto_time_s
name: "Stair Footer Lights Auto Time"
unit_of_measurement: s
min_value: 10
max_value: 300
step: 1
optimistic: true
restore_value: true
initial_value: 20
#########################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#########################################################################################
# LD2410 SELECT
# distance_resolution (Optional): Control the gates distance resolution. Can be 0.75m or 0.2m.
# Defaults to 0.75m. All options from Select.
# baud_rate (Optional): Control the serial port baud rate. Defaults to 256000. Once changed,
# all sensors will stop working until a fresh install with an updated UART Component
# configuration. All options from Select.
# light_function (Optional): If set, will affect the OUT pin value, based on light
# threshold. Can be off, low or above. Defaults to off. All options from Select.
# out_pin_level (Optional): Control OUT pin away value. Can be low or high. Defaults
# to low. All options from Select.
# ld2410_id (Optional, ID): Manually specify the ID for the LD2410 Sensor component
# if you are using multiple components.
#########################################################################################
select:
- platform: ld2410
distance_resolution:
name: "${friendly_name} LD2140 Distance Resolution"
baud_rate:
name: "${friendly_name} LD2140 Baud Rate"
light_function:
name: "${friendly_name} LD2140 Light Function"
out_pin_level:
name: "${friendly_name} LD2140 Out Pin Level"
##########################################################################################
# SENSOR COMPONENT
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
- platform: bme280_i2c
temperature:
name: "Temperature"
accuracy_decimals: 1
oversampling: 2x
pressure:
name: "Pressure"
oversampling: 2x
humidity:
name: "Humidity"
accuracy_decimals: 1
oversampling: 2x
address: 0x76
update_interval: "${update_interval}"
# The ld2410 sensor values
# https://esphome.io/components/sensor/ld2410/#sensor
- platform: ld2410
light:
name: "Light"
moving_distance:
name: "Moving Distance"
still_distance:
name: "Still Distance"
moving_energy:
name: "Move Energy"
still_energy:
name: "Still Energy"
detection_distance:
name: "Detection Distance"
g0:
move_energy:
name: "g0 move energy"
still_energy:
name: "g0 still energy"
g1:
move_energy:
name: "g1 move energy"
still_energy:
name: "g1 still energy"
g2:
move_energy:
name: "g2 move energy"
still_energy:
name: "g2 still energy"
g3:
move_energy:
name: "g3 move energy"
still_energy:
name: "g3 still energy"
g4:
move_energy:
name: "g4 move energy"
still_energy:
name: "g4 still energy"
g5:
move_energy:
name: "g5 move energy"
still_energy:
name: "g5 still energy"
g6:
move_energy:
name: "g6 move energy"
still_energy:
name: "g6 still energy"
g7:
move_energy:
name: "g7 move energy"
still_energy:
name: "g7 still energy"
g8:
move_energy:
name: "g8 move energy"
still_energy:
name: "g8 still energy"
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# The ld2410 switch allows you to control your LD2410 Sensor.
# Bluetooth switch is only useful of you have a B or C model
##########################################################################################
switch:
# https://esphome.io/components/sensor/ld2410/#switch
- platform: ld2410
engineering_mode:
name: "${friendly_name} LD2140 Engineering Mode"
#bluetooth:
#name: "${friendly_name} LD2140 Control Bluetooth"
- platform: template
id: stair_footer_auto_enabled
name: "Stair Footer Lights Auto"
optimistic: true
restore_mode: RESTORE_DEFAULT_ON
#########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
binary_sensor:
# The ld2410 binary sensors to get presence notification
# https://esphome.io/components/sensor/ld2410/#binary-sensor
- platform: ld2410
has_target:
name: "mmWave Presence"
on_press:
- if:
condition:
switch.is_on: stair_footer_auto_enabled
then:
- script.execute: stair_footer_auto_script
has_moving_target:
name: "mmWave Moving Target"
on_press:
- if:
condition:
switch.is_on: stair_footer_auto_enabled
then:
- script.execute: stair_footer_auto_script
has_still_target:
name: "mmWave Still Target"
out_pin_presence_status:
name: "LD2140 Out Pin Presence Status"
entity_category: diagnostic
# Generic PIR Sensor
# https://devices.esphome.io/devices/Generic-PIR
- platform: gpio
pin:
number: GPIO13
mode:
input: true
pullup: true
inverted: true
name: "PIR Sensor"
device_class: motion
filters:
- delayed_on: 50ms
on_press:
- if:
condition:
switch.is_on: stair_footer_auto_enabled
then:
- script.execute: stair_footer_auto_script
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
# The ld2410 button allows resetting
#################################################################################################
button:
- platform: ld2410
factory_reset:
name: "${friendly_name} LD2140 Factory reset"
restart:
name: "${friendly_name} LD2140 Restart"
query_params:
name: "Query Parameters"
#################################################################################################
# TEXT SENSOR COMPONENT
# https://esphome.io/components/text_sensor/
#################################################################################################
text_sensor:
# The ld2410 text sensor allows you to get information about your LD2410 Sensor.
# Bluetooth sensor is only useful of you have a B or C model
# https://esphome.io/components/sensor/ld2410/#text-sensor
- platform: ld2410
version:
name: "${friendly_name} LD2140 Firmware Version"
#mac_address:
#name: "${friendly_name} LD2140 BT MAC Address"
##########################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
# Scripts can be executed nearly anywhere in your device configuration with a single call.
##########################################################################################
script:
# Sends commands to turn on and off a remote light
- id: stair_footer_auto_script
mode: restart
then:
# Turn lights on (command the Top switch's footer relay)
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device_command_ON}"
retain: false
# Wait for the HA-adjustable timeout
- delay: !lambda 'return (uint32_t)(id(stair_footer_auto_time_s).state) * 1000;'
# Turn lights back off
- mqtt.publish:
topic: "${mqtt_remote_device1_command_topic}"
payload: "${mqtt_remote_device_command_OFF}"
retain: false

View File

@@ -0,0 +1,310 @@
#############################################
#############################################
# OFFICE DUAL LIGHT CONTROL
# - Overhead cool white lights
# - Right hand warm bunker light
#
# Controlled by a Sonoff Dual R1
# A sonoff dual uses serial data to turn the relays on/off
#
# V2.4 - 2025-06-30 Tidied up MQTT direct relay control (esps can control each other via MQTT)
# V2.3 - 2025-06-18 Added MQTT direct relay control
#
# NOTES:
# Command the lights on with MQTT
# ${mqtt_local_command_full_topic}/relay1/set ON or OFF
# ${mqtt_local_command_full_topic}/relay2/set ON or OFF
# ${mqtt_local_status_full_topic}/relay1/set ON or OFF
# ${mqtt_local_status_full_topic}/relay2/set ON or OFF
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-officeduallights"
friendly_name: "Office Dual Lights"
description_comment: "Dual Office Lights (Overhead and right hand Bunker) :: Sonoff Dual R1"
device_area: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Dual R1" # Project Details
project_version: "v2.3" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-officeduallights_ip # unfortunately you can't use substitutions inside secrets names
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
relay_icon: "mdi:lightbulb-group"
log_level: "NONE" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT Controls
mqtt_device_name: "office-dual-lights"
mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Switch Naming
switch_1_name: "Dual L1 Relay"
relay_1_icon: "mdi:lightbulb"
light_1_name: "Light 1 (Right Hand Bunker Light)"
switch_2_name: "Dual L2 Relay"
relay_2_icon: "mdi:lightbulb"
light_2_name: "Light 2 (Cool White Overhead Lights)"
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
platformio_options:
build_flags:
- "-Os" # optimize for size
- "-Wl,--gc-sections" # drop unused code/data
- "-fno-exceptions" # strip C++ exceptaions
- "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: False # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: True
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
# NOTE: Sonoff Dual CANNOT send serial data to log
###############################################
logger:
level: "${log_level}" #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, Serial control)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
##########################################################################################
# UART Bus
# https://esphome.io/components/uart.html
##########################################################################################
uart:
tx_pin: GPIO01
rx_pin: GPIO03
baud_rate: 19200
#data_bits: 8
#stop_bits: 1
#parity: EVEN
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
# Direct MQTT control for relays via serial commands
on_message:
# Relay 1 control
- topic: "${mqtt_local_command_topic}/relay1/set"
payload: "ON"
then:
- switch.turn_on: relay_1
- light.turn_on: light_1
- topic: "${mqtt_local_command_topic}/relay1/set"
payload: "OFF"
then:
- switch.turn_off: relay_1
- light.turn_off: light_1
# Relay 2 control
- topic: "${mqtt_local_command_topic}/relay2/set"
payload: "ON"
then:
- switch.turn_on: relay_2
- light.turn_on: light_2
- topic: "${mqtt_local_command_topic}/relay2/set"
payload: "OFF"
then:
- switch.turn_off: relay_2
- light.turn_off: light_2
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Sonoff Dual LED is GPIO13
#############################################
status_led:
pin:
number: GPIO13
inverted: yes
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Sonoff Dual R1 buttons are GPIO04, GPIO14
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
inverted: true
id: button_1
on_press:
- switch.toggle: relay_1
- platform: gpio
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: true
id: button_2
on_press:
- switch.toggle: relay_2
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff Dual R1 requires serial data to switch
#############################################
switch:
- platform: template
name: "${switch_1_name}"
id: relay_1
optimistic: true
icon: "${relay_1_icon}"
internal: true
turn_on_action:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/relay1/state"
payload: "ON"
- if:
condition:
switch.is_off: relay_2
then:
- uart.write: [0xA0, 0x04, 0x01, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x03, 0xA1]
turn_off_action:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/relay1/state"
payload: "OFF"
- if:
condition:
switch.is_off: relay_2
then:
- uart.write: [0xA0, 0x04, 0x00, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x02, 0xA1]
- platform: template
name: "${switch_2_name}"
id: relay_2
optimistic: true
icon: "${relay_2_icon}"
internal: true
turn_on_action:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/relay2/state"
payload: "ON"
- if:
condition:
switch.is_off: relay_1
then:
- uart.write: [0xA0, 0x04, 0x02, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x03, 0xA1]
turn_off_action:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/relay2/state"
payload: "OFF"
- if:
condition:
switch.is_off: relay_1
then:
- uart.write: [0xA0, 0x04, 0x00, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x01, 0xA1]
################################################################################
# TEMPLATE OUTPUTS: drive the real relays when the light state changes
################################################################################
output:
- platform: template
id: light_output_1
type: binary
write_action:
- lambda: |-
if (state) id(relay_1).turn_on();
else id(relay_1).turn_off();
- platform: template
id: light_output_2
type: binary
write_action:
- lambda: |-
if (state) id(relay_2).turn_on();
else id(relay_2).turn_off();
##########################################################################################
# LIGHT COMPONENT
# https://esphome.io/components/light/
##########################################################################################
light:
- platform: binary
name: "${light_1_name}"
id: light_1
output: light_output_1
- platform: binary
name: "Light 2 (Cool White Overhead Lights)"
id: light_2
output: light_output_2

View File

@@ -0,0 +1,199 @@
#############################################
#############################################
# OFFICE ELV CONTROL
# Controlled by a Sonoff Basic R1
#
# V1.0 2025-06-14 Initial Version
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-officeelvcontrol"
friendly_name: "Office ELV Control"
description_comment: "ELV Power Supply control in the Office (under desk) :: Sonoff Basic"
device_area: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-officeelvcontrol_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
#mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
relay_icon: "mdi:generator-portable"
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Device Naming
relay_1_name: "Office ELV Power Supply"
button_1_name: "Power Button"
# MQTT Controls
mqtt_device_name: "office-elv-control"
mqtt_main_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command stuff from external without HA
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
# platformio_options:
# build_flags:
# - "-Os" # optimize for size
# - "-Wl,--gc-sections" # drop unused code/data
# - "-fno-exceptions" # strip C++ exceptions
# - "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: False
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff Basic R1 Relay Switch is GPIO12
#############################################
switch:
- platform: gpio
name: "${relay_1_name}"
pin: GPIO12
id: relay1
restore_mode: RESTORE_DEFAULT_ON
icon: "${relay_icon}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Sonoff Basic R1 Button is GPIO03
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO03
mode: INPUT_PULLUP
inverted: true
name: "${button_1_name}"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay1).state) {
// Relay is ON: turn it OFF
id(relay1).turn_off();
} else {
// Relay is OFF: turn it ON
id(relay1).turn_on();
}
# Mimics actual relay status (but not controllable)
- platform: template
name: "${relay_1_name} Status"
lambda: |-
return id(relay1).state;
on_press:
- mqtt.publish:
topic: "${mqtt_topic}/relay1/state"
payload: "ON"
on_release:
- mqtt.publish:
topic: "${mqtt_topic}/relay1/state"
payload: "OFF"
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Sonoff Basic R1 LED is GPIO13
#############################################
status_led:
pin:
number: GPIO13
inverted: yes
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Relay 1 control
- topic: "${mqtt_main_topic}/relay1/set"
payload: "ON"
then:
- switch.turn_on: relay1
- topic: "${mqtt_main_topic}/relay1/set"
payload: "OFF"
then:
- switch.turn_off: relay1

View File

@@ -0,0 +1,168 @@
#############################################
#############################################
# OFFICE MAIN LIGHTSWITCH
# V2.0 2025-06-05 YAML Tidyups
# V1.0 2025-05-31 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
# Device Naming
device_name: "esp-officelights"
friendly_name: "Office Main Lightswitch (3)"
description_comment: "Office Main Lightswitch using a Zemismart KS-811 Triple Push Button. Bunker Light A (1), Spare A (2), Spare B (3)"
device_area: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v2.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-officelights_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Switch Naming
switch_1_name: "Nighttime Lights" # Only one light (Bunker Light) actually connected to this relay
switch_2_name: "Daytime Lights" # This is virtual only, no power connected to 2nd relay
switch_3_name: "Spare" # This is virtual only, no power connected to 3rd relay
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: "${log_level}" #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
id: logger_id
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: ${switch_1_name}"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: ${switch_2_name}"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: ${switch_3_name}"
on_press:
- switch.toggle: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: ${switch_3_name}"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,199 @@
#############################################
#############################################
# OFFICE USB HUB POWER
# Controlled by a Sonoff Basic R1
#
# V1.0 2025-06-14 Initial Version
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-officeusbhubpower"
friendly_name: "Office USB Hub Power"
description_comment: "Office USB Hub Power Supply control in the Office (On wall) :: Sonoff Basic"
device_area: "Office" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-officeusbhubpower_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
relay_icon: "mdi:generator-portable"
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# Device Naming
relay_1_name: "USB Hub Power Supply"
button_1_name: "Power Button"
# MQTT Controls
mqtt_device_name: "office-usbhub-power"
mqtt_main_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command stuff from external without HA
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
# platformio_options:
# build_flags:
# - "-Os" # optimize for size
# - "-Wl,--gc-sections" # drop unused code/data
# - "-fno-exceptions" # strip C++ exceptions
# - "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: False
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff Basic R1 Relay Switch is GPIO12
#############################################
switch:
- platform: gpio
name: "${relay_1_name}"
pin: GPIO12
id: relay1
restore_mode: RESTORE_DEFAULT_ON
icon: "${relay_icon}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Sonoff Basic R1 Button is GPIO03
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO03
mode: INPUT_PULLUP
inverted: true
name: "${button_1_name}"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay1).state) {
// Relay is ON: turn it OFF
id(relay1).turn_off();
} else {
// Relay is OFF: turn it ON
id(relay1).turn_on();
}
# Mimics actual relay status (but not controllable)
- platform: template
name: "${relay_1_name} Status"
lambda: |-
return id(relay1).state;
on_press:
- mqtt.publish:
topic: "${mqtt_topic}/relay1/state"
payload: "ON"
on_release:
- mqtt.publish:
topic: "${mqtt_topic}/relay1/state"
payload: "OFF"
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Sonoff Basic R1 LED is GPIO13
#############################################
status_led:
pin:
number: GPIO13
inverted: yes
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Relay 1 control
- topic: "${mqtt_main_topic}/relay1/set"
payload: "ON"
then:
- switch.turn_on: relay1
- topic: "${mqtt_main_topic}/relay1/set"
payload: "OFF"
then:
- switch.turn_off: relay1

View File

@@ -0,0 +1,705 @@
##########################################################################################
##########################################################################################
# POOL LIGHT POWER AND TIMER
# Controlled by a Athom Smart Plug V1
#
# dashboard_import:
# package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml
#
# V2.4 2025-06-15 Changed back to an Athom V1 (esp8266)
# V2.3 2025-06-15 Changed to an Athom V3 (esp32)
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
#
# INSTRUCTIONS
# - It allows the device to work in a standalone timer style operation
# - The timer has a morning and evening time (but no weekday/weekend settings)
# - Default values are set, but changed values are remembered in flash
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
# - If on a network and there is a MQTT server, you can set the on/off times via MQTT (See below commands)
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
# - Any new timer times set via MQTT will be remembered though a reboot
# - On startup, or a reboot, the device will always turn on for the BOOST Duration (BOOST mode, default 2 hours)
# - TIMER mode will always be switched on after BOOST mode is complete
# - Home Assistant entities are set so that BOOST mode can be pressed with a button and other modes selectable with a dropdown
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. (Note there is no weekday/weekend setting)
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
# mqtt_timer_topic/operation/ON : Device permanently on
# mqtt_timer_topic/operation/OFF : Device permanently off
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
#
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-poollightpower"
friendly_name: "Pool Light Power"
description_comment: "Pool Light Power :: Athom Smart Plug Power V1"
device_area: "Outside" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V1" # Project Details
project_version: "v2.4" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-poollightpower_ip
# Device Settings
log_level: "NONE" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "10s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:power-socket-au"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
mqtt_timer_topic: "viewroad-commands/poollight-timer" # Topics you will use to change stuff
boost_duration_default: "180" # Minutes to stay ON in BOOST mode before reverting to TIMER
morning_on_default: "450" # Default in minutes from midnight. Default 07:30 => 450
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450 (same as ON as no need for morning schedule)
evening_on_default: "1140" # Default in minutes from midnight. Default 19:00 => 1140
evening_off_default: "1350" # Default in minutes from midnight. Default 22:30 => 1350 => 1440 is midnight
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
# Device Specific included packages
common_athompowermonV1: !include
file: common/athompowermonv1_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_current_limit: "${current_limit}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
name: "${project_name}"
version: "${project_version}"
#platformio_options:
# build_unflags:
# - -std=gnu++20
# - -std=gnu++2a
# build_flags:
# - -std=gnu++11
# - -Os
# - -Wl,--gc-sections
# - -fno-exceptions
# - -fno-rtti
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp8285
restore_from_flash: true # mainly for calculating cumulative energy, but not that important here
#framework:
# version: 2.7.4
preferences:
flash_write_interval: 10min
mdns:
disabled: True # binary size saving
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
# Tracks the time (in seconds from midnight) at the previous boot
- id: last_boot_time_s
type: int
restore_value: true
initial_value: "0"
# Counts how many consecutive boots have occurred (within X seconds)
- id: boot_count
type: int
restore_value: true
initial_value: "0"
# Morning On time (minutes from midnight),
- id: morning_on
type: int
restore_value: true
initial_value: "${morning_on_default}"
# Morning Off time (minutes from midnight),
- id: morning_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening On time (minutes from midnight),
- id: evening_on
type: int
restore_value: true
initial_value: "${evening_on_default}"
# Evening Off time (minutes from midnight),
- id: evening_off
type: int
restore_value: true
initial_value: "${evening_off_default}"
# Boost Duration (minutes),
- id: boost_duration
type: int
restore_value: true
initial_value: "${boost_duration_default}"
####################################################
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
####################################################
- id: operation_mode
type: int
restore_value: true
initial_value: "2"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
####################################################
- id: current_mins
type: int
restore_value: true
initial_value: "720" # 720 is 12:00 Noon
####################################################
# boost_timer: counts minutes in BOOST mode
# After 'boost_duration' minutes, revert to TIMER.
####################################################
- id: boost_timer
type: int
restore_value: true
initial_value: "0"
##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
############################
# MQTT Subscriptions
############################
####################################################
# Subscribe to the Morning On time, format "HH:MM"
# We check x.size() == 5 and x[2] == ':',
# then parse x.substr(0,2) and x.substr(3,2)
# std::string uses 'substr', not 'substring'.
####################################################
- platform: mqtt_subscribe
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
internal: True
on_value:
then:
- lambda: |-
// Expect "HH:MM" => total length = 5, with ':'
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str()); // "HH"
int minute = atoi(x.substr(3, 2).c_str()); // "MM"
id(morning_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
####################################################
# Morning Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(morning_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
####################################################
# Evening On time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
####################################################
# Evening Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
####################################################
# Boost duration => 1 - 1439
####################################################
- platform: mqtt_subscribe
name: "Boost Duration"
id: boost_time_topic
topic: "${mqtt_timer_topic}/boost-time" # Stored as an integer from 1-1439
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
// parse as integer
char *endptr;
long v = strtol(x.c_str(), &endptr, 10);
// invalid if nothing parsed, trailing chars, or out of 01439
if (endptr == x.c_str() || *endptr != '\0' || v < 0 || v > 1439) {
ESP_LOGE("boost_time", "Invalid boost_time '%s'", x.c_str());
} else {
id(boost_duration) = static_cast<int>(v);
}
####################################################
# Subscribe to operation mode:
# OFF, ON, TIMER, BOOST
# We do case-insensitive compare using strcasecmp
# (Requires <strings.h> typically included in ESPHome)
####################################################
# MQTT subscription: set mode, then immediately re-evaluate relay
- platform: mqtt_subscribe
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation"
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (strcasecmp(x.c_str(), "TIMER") == 0) {
id(operation_mode) = 2;
ESP_LOGI("timer","Operation mode set to TIMER");
} else if (strcasecmp(x.c_str(), "ON") == 0) {
id(operation_mode) = 1;
ESP_LOGI("timer","Operation mode set to ON");
} else if (strcasecmp(x.c_str(), "OFF") == 0) {
id(operation_mode) = 0;
ESP_LOGI("timer","Operation mode set to OFF");
} else if (strcasecmp(x.c_str(), "BOOST") == 0) {
id(operation_mode) = 3;
id(boost_timer) = 0;
ESP_LOGI("timer","Operation mode set to BOOST");
} else {
ESP_LOGW("timer","Invalid operation mode: %s", x.c_str());
}
- script.execute: evaluate_relay_state
######################################################
# Expose the current operation mode (OFF, ON, TIMER, BOOST)
######################################################
- platform: template
name: "Operation Mode State"
lambda: |-
// 0=OFF, 1=ON, 2=TIMER, 3=BOOST
switch (id(operation_mode)) {
case 0: return {"OFF"};
case 1: return {"ON"};
case 2: return {"TIMER"};
case 3: return {"BOOST"};
default: return {"UNKNOWN"};
}
update_interval: 5s
######################################################
# Expose the "Morning On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning On Time"
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Morning Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning Off Time"
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening On Time"
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening Off Time"
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO03
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay).state) {
// Relay is ON: turn it OFF and set mode to 0 (TIMER)
id(relay).turn_off();
id(operation_mode) = 2;
} else {
// Relay is OFF: turn it ON and set mode to 3 (BOOST)
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
##########################################################################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
sensor:
- platform: template
name: "Timeclock: Boost Duration"
id: boost_duration_time
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
internal: True # No need to show this in Home Assistant
lambda: |-
return id(current_mins);
# A value in mins if a timer is running showing how many mins left
- platform: template
name: "Timer Minutes Remaining"
id: timer_minutes_remaining
unit_of_measurement: "Mins"
update_interval: 5s
accuracy_decimals: "0"
lambda: |-
// always zero if relay is off
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (BOOST)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
rem = id(boost_duration) - id(boost_timer);
}
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
switch:
- platform: gpio
name: "Power Output"
pin: GPIO14
id: relay
restore_mode: RESTORE_DEFAULT_OFF # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
- platform: template
name: "Boost now"
id: boost_button
icon: "mdi:play-circle-outline"
on_press:
# 1) reset BOOST timer and set mode
- lambda: |-
id(boost_timer) = 0;
id(operation_mode) = 3;
# 2) immediately re-evaluate relay state
- script.execute: evaluate_relay_state
- platform: template
name: "Default timer settings"
id: default_timer_settings_button
icon: "mdi:restore"
on_press:
- lambda: |-
// Restore all timing globals to their YAML defaults
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration)= ${boost_duration_default};
// Reset mode to TIMER and clear any running boost
id(operation_mode)= 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
- script.execute: evaluate_relay_state
#################################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#################################################################################################
select:
- platform: template
name: "Operation Mode"
id: operation_mode_select
update_interval: 5s
options:
- "OFF"
- "ON"
- "TIMER"
- "BOOST"
# show the current mode
lambda: |-
switch (id(operation_mode)) {
case 1: return std::string("ON");
case 2: return std::string("TIMER");
case 3: return std::string("BOOST");
default: return std::string("OFF");
}
# when changed in HA, set mode & re-evaluate
set_action:
- lambda: |-
if (x == "OFF") { id(operation_mode) = 0; }
else if (x == "ON") { id(operation_mode) = 1; }
else if (x == "TIMER") { id(operation_mode) = 2; }
else { // BOOST
id(boost_timer) = 0;
id(operation_mode) = 3;
}
- script.execute: evaluate_relay_state
#################################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
#################################################################################################
# Script: evaluate and drive the relay
script:
- id: evaluate_relay_state
then:
- lambda: |-
int mode = id(operation_mode);
// BOOST just forces the relay on
if (mode == 3) {
id(relay).turn_on();
return;
}
// OFF → always off
if (mode == 0) {
id(relay).turn_off();
return;
}
// ON → always on
if (mode == 1) {
id(relay).turn_on();
return;
}
// TIMER → follow schedule windows
{
bool should_on = false;
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off))
should_on = true;
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off))
should_on = true;
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
#################################################################################################
# INTERVAL COMPONENT
# https://esphome.io/components/interval.html
#################################################################################################
# Interval: bumps time (even if no SNTP), then calls the script to evaluate relay state
interval:
- interval: "1min"
then:
- lambda: |-
// — update current_mins via SNTP or fallback
if (!id(sntp_time).now().is_valid()) {
id(current_mins)++;
if (id(current_mins) >= 1440) id(current_mins) = 0;
} else {
auto now = id(sntp_time).now();
id(current_mins) = now.hour * 60 + now.minute;
}
// — if in BOOST, advance boost_timer and expire when done
if (id(operation_mode) == 3) {
id(boost_timer)++;
if (id(boost_timer) >= id(boost_duration)) {
id(operation_mode) = 2;
//id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
}
}
- script.execute: evaluate_relay_state

View File

@@ -0,0 +1,701 @@
##########################################################################################
##########################################################################################
# POOL PUMP POWER AND TIMER
# Controlled by a Athom Smart Plug V3
#
# dashboard_import:
# package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml
#
# V2.3 2025-06-19 Minor changes to remember mode in reboot and to ensure offline functionality
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
#
# INSTRUCTIONS
# - It allows the device to work in a standalone timer style operation
# - The timer has a morning and evening time (but no weekday/weekend settings)
# - Default values are set, but changed values are remembered in flash
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
# - If on a network and there is a MQTT server, you can set the on/off times via MQTT (See below commands)
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
# - Any new timer times set via MQTT will be remembered though a reboot
# - On startup, or a reboot, the device will always turn on for the BOOST Duration (BOOST mode, default 2 hours)
# - TIMER mode will always be switched on after BOOST mode is complete
# - Home Assistant entities are set so that BOOST mode can be pressed with a button and other modes selectable with a dropdown
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. (Note there is no weekday/weekend setting)
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
# mqtt_timer_topic/operation/ON : Device permanently on
# mqtt_timer_topic/operation/OFF : Device permanently off
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
#
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-poolpumppower"
friendly_name: "Pool Pump Power"
description_comment: "Pool Pump Power :: Athom Smart Plug Power V3"
device_area: "Outside" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V3" # Project Details
project_version: "v2.2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-poolpumppower_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-poolpumppower_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-poolpumppower_ip
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "20s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:pool"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
mqtt_timer_topic: "viewroad-commands/poolpump-timer" # Topics you will use to change stuff
boost_duration_default: "60" # Minutes to stay ON in STARTUP mode before reverting to TIMER
morning_on_default: "450" # Default in minutes from midnight. Default 07:30 => 450
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450 (same as ON as no need for morning schedule)
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
evening_off_default: "1350" # Default in minutes from midnight. Default 22:30 => 1350 => 1440 is midnight
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
# Device Specific included packages
common_athompowermonV3: !include
file: common/athompowermonv3_common.yaml
vars:
local_current_limit: "${current_limit}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
name: "${project_name}"
version: "${project_version}"
platformio_options:
board_build.mcu: esp32c3
board_build.variant: esp32c3
board_build.flash_mode: dio
on_boot:
then:
- script.execute: evaluate_relay_state
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp32:
board: esp32-c3-devkitm-1
flash_size: 4MB
variant: ESP32C3
framework:
type: esp-idf # "esp-idf" OR "arduino". Suggested ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended # recommended, latest or dev
preferences:
flash_write_interval: 5min
esp32_improv:
authorizer: none
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
# Tracks the time (in seconds from midnight) at the previous boot
- id: last_boot_time_s
type: int
restore_value: true
initial_value: "0"
# Counts how many consecutive boots have occurred (within X seconds)
- id: boot_count
type: int
restore_value: true
initial_value: "0"
# Morning On time (minutes from midnight),
- id: morning_on
type: int
restore_value: true
initial_value: "${morning_on_default}"
# Morning Off time (minutes from midnight),
- id: morning_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening On time (minutes from midnight),
- id: evening_on
type: int
restore_value: true
initial_value: "${evening_on_default}"
# Evening Off time (minutes from midnight),
- id: evening_off
type: int
restore_value: true
initial_value: "${evening_off_default}"
# Boost Duration (minutes),
- id: boost_duration
type: int
restore_value: true
initial_value: "${boost_duration_default}"
####################################################
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = BOOST
####################################################
- id: operation_mode
type: int
restore_value: true
initial_value: "2"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
####################################################
- id: current_mins
type: int
restore_value: true
initial_value: "720" # 720 is 12:00 Noon
####################################################
# boost_timer: counts minutes in BOOST mode
# After 'boost_duration' minutes, revert to TIMER.
####################################################
- id: boost_timer
type: int
restore_value: true
initial_value: "0"
##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
############################
# MQTT Subscriptions
############################
####################################################
# Subscribe to the Morning On time, format "HH:MM"
# We check x.size() == 5 and x[2] == ':',
# then parse x.substr(0,2) and x.substr(3,2)
# std::string uses 'substr', not 'substring'.
####################################################
- platform: mqtt_subscribe
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
internal: True
on_value:
then:
- lambda: |-
// Expect "HH:MM" => total length = 5, with ':'
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str()); // "HH"
int minute = atoi(x.substr(3, 2).c_str()); // "MM"
id(morning_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
####################################################
# Morning Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(morning_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
####################################################
# Evening On time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
####################################################
# Evening Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
####################################################
# Boost duration => 1 - 1439
####################################################
- platform: mqtt_subscribe
name: "Boost Duration"
id: boost_time_topic
topic: "${mqtt_timer_topic}/boost-time" # Stored as an integer from 1-1439
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
// parse as integer
char *endptr;
long v = strtol(x.c_str(), &endptr, 10);
// invalid if nothing parsed, trailing chars, or out of 01439
if (endptr == x.c_str() || *endptr != '\0' || v < 0 || v > 1439) {
ESP_LOGE("boost_time", "Invalid boost_time '%s'", x.c_str());
} else {
id(boost_duration) = static_cast<int>(v);
}
####################################################
# Subscribe to operation mode:
# OFF, ON, TIMER, BOOST
# We do case-insensitive compare using strcasecmp
# (Requires <strings.h> typically included in ESPHome)
####################################################
# MQTT subscription: set mode, then immediately re-evaluate relay
- platform: mqtt_subscribe
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation"
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (strcasecmp(x.c_str(), "TIMER") == 0) {
id(operation_mode) = 2;
ESP_LOGI("timer","Operation mode set to TIMER");
} else if (strcasecmp(x.c_str(), "ON") == 0) {
id(operation_mode) = 1;
ESP_LOGI("timer","Operation mode set to ON");
} else if (strcasecmp(x.c_str(), "OFF") == 0) {
id(operation_mode) = 0;
ESP_LOGI("timer","Operation mode set to OFF");
} else if (strcasecmp(x.c_str(), "BOOST") == 0) {
id(operation_mode) = 3;
id(boost_timer) = 0;
ESP_LOGI("timer","Operation mode set to BOOST");
} else {
ESP_LOGW("timer","Invalid operation mode: %s", x.c_str());
}
- script.execute: evaluate_relay_state
######################################################
# Expose the current operation mode (OFF, ON, TIMER, BOOST)
######################################################
- platform: template
name: "Operation Mode State"
lambda: |-
// 0=OFF, 1=ON, 2=TIMER, 3=BOOST
switch (id(operation_mode)) {
case 0: return {"OFF"};
case 1: return {"ON"};
case 2: return {"TIMER"};
case 3: return {"BOOST"};
default: return {"UNKNOWN"};
}
update_interval: 5s
######################################################
# Expose the "Morning On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning On Time"
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Morning Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning Off Time"
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening On Time"
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening Off Time"
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
binary_sensor:
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
name: "Power Button"
id: power_button
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay).state) {
// Relay is ON: turn it OFF and set mode to 0 (TIMER)
id(relay).turn_off();
id(operation_mode) = 2;
} else {
// Relay is OFF: turn it ON and set mode to 3 (BOOST)
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
return id(relay).state;
##########################################################################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
sensor:
- platform: template
name: "Timeclock: Boost Duration"
id: boost_duration_time
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
internal: True # No need to show this in Home Assistant
lambda: |-
return id(current_mins);
# A value in mins if a timer is running showing how many mins left
- platform: template
name: "Timer Minutes Remaining"
id: timer_minutes_remaining
unit_of_measurement: "Mins"
update_interval: 5s
accuracy_decimals: "0"
lambda: |-
// always zero if relay is off
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (BOOST)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
rem = id(boost_duration) - id(boost_timer);
}
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
switch:
- platform: gpio
name: "Power Output"
pin: GPIO5
id: relay
restore_mode: RESTORE_DEFAULT_OFF # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
- platform: template
name: "Boost now"
id: boost_button
icon: "mdi:play-circle-outline"
on_press:
# 1) reset BOOST timer and set mode
- lambda: |-
id(boost_timer) = 0;
id(operation_mode) = 3;
# 2) immediately re-evaluate relay state
- script.execute: evaluate_relay_state
- platform: template
name: "Default timer settings"
id: default_timer_settings_button
icon: "mdi:restore"
on_press:
- lambda: |-
// Restore all timing globals to their YAML defaults
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration)= ${boost_duration_default};
// Reset mode to TIMER and clear any running boost
id(operation_mode)= 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
- script.execute: evaluate_relay_state
#################################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#################################################################################################
select:
- platform: template
name: "Operation Mode"
id: operation_mode_select
update_interval: 5s
options:
- "OFF"
- "ON"
- "TIMER"
- "BOOST"
# show the current mode
lambda: |-
switch (id(operation_mode)) {
case 1: return std::string("ON");
case 2: return std::string("TIMER");
case 3: return std::string("BOOST");
default: return std::string("OFF");
}
# when changed in HA, set mode & re-evaluate
set_action:
- lambda: |-
if (x == "OFF") { id(operation_mode) = 0; }
else if (x == "ON") { id(operation_mode) = 1; }
else if (x == "TIMER") { id(operation_mode) = 2; }
else { // BOOST
id(boost_timer) = 0;
id(operation_mode) = 3;
}
- script.execute: evaluate_relay_state
#################################################################################################
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
#################################################################################################
# Script: evaluate and drive the relay
script:
- id: evaluate_relay_state
then:
- lambda: |-
int mode = id(operation_mode);
// BOOST just forces the relay on
if (mode == 3) {
id(relay).turn_on();
return;
}
// OFF → always off
if (mode == 0) {
id(relay).turn_off();
return;
}
// ON → always on
if (mode == 1) {
id(relay).turn_on();
return;
}
// TIMER → follow schedule windows
{
bool should_on = false;
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off))
should_on = true;
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off))
should_on = true;
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
#################################################################################################
# INTERVAL COMPONENT
# https://esphome.io/components/interval.html
#################################################################################################
# Interval: bumps time (even if no SNTP), then calls the script to evaluate relay state
interval:
- interval: "1min"
then:
- lambda: |-
// — update current_mins via SNTP or fallback
if (!id(sntp_time).now().is_valid()) {
id(current_mins)++;
if (id(current_mins) >= 1440) id(current_mins) = 0;
} else {
auto now = id(sntp_time).now();
id(current_mins) = now.hour * 60 + now.minute;
}
// — if in BOOST, advance boost_timer and expire when done
if (id(operation_mode) == 3) {
id(boost_timer)++;
if (id(boost_timer) >= id(boost_duration)) {
id(operation_mode) = 2;
//id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
}
}
- script.execute: evaluate_relay_state

View File

@@ -2,7 +2,7 @@
# ESPHome
#############################################
esphome:
name: esp-netcupfan2
name: esp-ranchsliderfans
########################################
# Specific board for ESPHome device
@@ -20,7 +20,7 @@ logger:
#############################################
api:
encryption:
key: !secret esp-netcupfan2_api_key
key: !secret esp-ranchsliderfans_api_key
#############################################
# Enable Over the Air Update Capability
@@ -28,18 +28,19 @@ api:
#############################################
ota:
safe_mode: true
password: !secret esp-netcupfan2_ota_pass
password: !secret esp-ranchsliderfans_ota_pass
#############################################
# Use Wifi
#############################################
wifi:
#use_address: esp-netcupfan2.local
ssid: !secret wifi_ssid
password: !secret wifi_password
# Details for fallback hotspot
# in case wifi connection fails
ap:
ssid: "Netcupfan2 Fallback Hotspot"
ssid: "Ranchsliderfans Fallback Hotspot"
password: !secret fallback_ap_password
#############################################
@@ -61,7 +62,7 @@ web_server:
#############################################
mqtt:
broker: !secret mqtt_server
topic_prefix: esphome/esp-netcupfan2
topic_prefix: esphome/esp-ranchsliderfans
username: !secret mqtt_username
password: !secret mqtt_password
@@ -74,14 +75,14 @@ mqtt:
# Relay Output on a GPIO Pin
switch:
- platform: gpio
name: "Relay_Main"
name: "Ranch Slider Fan Boost"
pin: GPIO15
# Allows a fan control point in Home Assistant
fan:
- platform: speed
output: pwm_output
name: "Net Cupboard Fan 2 Speed"
name: "Ranch Slider Fans Speed"
speed_count: 500
# PWM Fan Output on the GPIO Pin

View File

@@ -0,0 +1,296 @@
#############################################
#############################################
# UNDERHOUSE LIGHTS
# Controlled by a Sonoff 4Ch R2
# https://github.com/jvyoralek/homeassistant-config/blob/master/esphome/sonoff-4ch.yaml
#
# V1.0 2025-06-18 Initial Version
#
##########################################################################################
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
device_name: "esp-underhouselights"
friendly_name: "Underhouse Lights"
description_comment: "Underhouse Lights Control :: Sonoff 4Ch R2"
device_area: "Underhouse" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff 4Ch R2" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-underhouselights_ip
mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
# Device Settings
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT Controls
mqtt_device_name: "underhouse-lights-control"
mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Switch Naming
switch_1_name: "Underhouse Entrance Lights"
button_1_name: "Button 1"
relay_1_icon: "mdi:lightbulb"
switch_2_name: "Underhouse Storage Lights"
button_2_name: "Button 2"
relay_2_icon: "mdi:lightbulb"
switch_3_name: "Spare 3"
button_3_name: "Button 3"
relay_3_icon: "mdi:lightbulb"
switch_4_name: "Spare 4"
button_4_name: "Button 4"
relay_4_icon: "mdi:lightbulb"
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
area: "${device_area}"
# platformio_options:
# build_flags:
# - "-Os" # optimize for size
# - "-Wl,--gc-sections" # drop unused code/data
# - "-fno-exceptions" # strip C++ exceptions
# - "-fno-rtti" # strip C++ RTTI
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: True # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: False
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
logger:
level: "${log_level}" #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
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff 4Ch R2 Relays are GPIO12,05,04,15
#############################################
switch:
- platform: gpio
id: relay1
name: "${switch_1_name}"
pin: GPIO12
icon: "${relay_1_icon}"
- platform: gpio
id: relay2
name: "${switch_2_name}"
pin: GPIO5
icon: "${relay_2_icon}"
- platform: gpio
id: relay3
name: "${switch_3_name}"
pin: GPIO4
icon: "${relay_3_icon}"
- platform: gpio
id: relay4
name: "${switch_4_name}"
pin: GPIO15
icon: "${relay_4_icon}"
##########################################################################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Sonoff 4Ch R2 Buttons are GPIO00,09,10,14
#############################################
binary_sensor:
- platform: gpio
id: button1
name: "${button_1_name}"
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
on_press:
- switch.toggle: relay1
- platform: gpio
id: button2
name: "${button_2_name}"
pin:
number: GPIO9
mode: INPUT_PULLUP
inverted: True
on_press:
- switch.toggle: relay2
- platform: gpio
id: button3
name: "${button_3_name}"
pin:
number: GPIO10
mode: INPUT_PULLUP
inverted: True
on_press:
- switch.toggle: relay3
- platform: gpio
id: button4
name: "${button_4_name}"
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: True
on_press:
- switch.toggle: relay4
- platform: gpio
pin:
number: GPIO03
mode: INPUT_PULLUP
inverted: true
name: "${switch_1_name} Remote Switch"
id: light_button_1
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay1).state) {
// Relay is ON: turn it OFF
id(relay1).turn_off();
} else {
// Relay is OFF: turn it ON
id(relay1).turn_on();
}
- platform: gpio
pin:
number: GPIO02
mode: INPUT_PULLUP
inverted: true
name: "${switch_2_name} Remote Switch"
id: light_button_2
filters:
- delayed_on: 20ms
on_click:
- min_length: 20ms
max_length: 500ms
then:
- lambda: |-
if (id(relay2).state) {
// Relay is ON: turn it OFF
id(relay2).turn_off();
} else {
// Relay is OFF: turn it ON
id(relay2).turn_on();
}
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
# Sonoff Basic R1 LED is GPIO13
#############################################
status_led:
pin:
number: GPIO13
inverted: yes
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
on_message:
# Relay 1 control
- topic: "${mqtt_local_command_topic}/relay1/set"
payload: "ON"
then:
- switch.turn_on: relay1
- topic: "${mqtt_local_command_topic}/relay1/set"
payload: "OFF"
then:
- switch.turn_off: relay1
# Relay 2 control
- topic: "${mqtt_local_command_topic}/relay2/set"
payload: "ON"
then:
- switch.turn_on: relay2
- topic: "${mqtt_local_command_topic}/relay2/set"
payload: "OFF"
then:
- switch.turn_off: relay2
# Relay 3 control
- topic: "${mqtt_local_command_topic}/relay3/set"
payload: "ON"
then:
- switch.turn_on: relay3
- topic: "${mqtt_local_command_topic}/relay3/set"
payload: "OFF"
then:
- switch.turn_off: relay3
# Relay 4 control
- topic: "${mqtt_local_command_topic}/relay4/set"
payload: "ON"
then:
- switch.turn_on: relay4
- topic: "${mqtt_local_command_topic}/relay4/set"
payload: "OFF"
then:
- switch.turn_off: relay4

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,228 @@
#############################################
#############################################
#############################################
#############################################
#############################################
# Variable Substitutions
# Give this a useful name & description here
# and change values accordingly
#############################################
substitutions:
devicename: "esp-winefridge"
friendly_name: "Wine Fridge Controller"
description_comment: "Peltier based wine fridge thermostat controller"
#if NOT using a secrets file, just replace these with the passwords etc in speech marks
api_key: !secret esp-winefridge_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-winefridge_ota_pass #unfortunately you can't use substitutions in secrets names
wifi_ssid: !secret wifi_ssid
wifi_pass: !secret wifi_password
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
update_time: 30s #update time for for various temp sensors etc
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${devicename}
comment: ${description_comment} #appears on the esphome page in HA
#on_boot: #Initial Setting stuff
#priority: -200
#then:
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html OR
# https://esphome.io/components/esp8266.html
#############################################
#esp32:
# board:
# framework:
# type: arduino
# #type: esp-idf #Suggested Use ESP-IDF Framework for ESP32, or unplugging the UART Cable Might Cause ESP32 Hang.
# version: recommended #recommended, latest or dev
esp8266:
board: d1_mini
#############################################
# 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
#############################################
# 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_pass}
#power_save_mode: LIGHT #https://esphome.io/components/wifi.html#wifi-power-save-mode
#manual_ip: #optional static IP address
#static_ip: 192.168.x.x
#gateway: 192.168.X.x
#subnet: 255.255.255.0
ap: #Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: $devicename fallback AP
password: !secret fallback_ap_password
ap_timeout: 30min #default is 1min
#############################################
# Web Portal for display and monitoring
# Turning this off if you don't really need it
# is probably a good idea to save resources.
# Also, don't use it with other big ESP32 components
# such as Bluetooth proxy (it will run out of flash
# and not compile, or it will crash occasionally)
# https://esphome.io/components/web_server.html
#############################################
web_server:
port: 80
# version: 1 #V1 occasionally works better, V2 The nicer page
# username: !secret web_server_username #probably a good idea to secure it
# password: !secret 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}
#############################################
# General espHome status LED
# Not needed, but can be useful to see issues
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2 #Wemos ESP32 and ESP8266 Onboard LEDs use GPIO2
inverted: false
#ignore_strapping_warning: True #https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
#############################################
#############################################
# General Sensors
# https://esphome.io/components/sensor/index.html
#############################################
#############################################
sensor:
################################
# UPTIME
# Device uptime info
# https://esphome.io/components/sensor/uptime.html
################################
- platform: uptime
name: ${friendly_name} Uptime
################################
# WIFI SIGNAL
# Quality of Wifi in dBm
# https://esphome.io/components/sensor/wifi_signal.html
################################
- platform: wifi_signal
name: ${friendly_name} WiFi Signal
update_interval: ${update_time}
#retain: true #retain useful if sleeping
- platform: resistance
sensor: temp_analogue
configuration: DOWNSTREAM
resistor: 4.7kOhm
name: Resistance Sensor
- platform: adc
id: temp_analogue
#update_interval: 10s
update_interval: never #don't update until the correect GPIO turns on. This allows two devices on one AI.
name: "Temp Analogue"
pin: A0
filters:
- multiply: 3.3
- platform: template
name: "Analogue Sensor 1"
update_interval: never
id: template_analoguesensor_1
lambda: |-
return id(temp_analogue);
- platform: template
name: "Analogue Sensor 2"
update_interval: never
id: template_analoguesensor_2
lambda: |-
return id(temp_analogue);
# lambda: |- return float(id(ch1s));
switch:
- platform: gpio
pin: D0
id: a1_sensor_vcc
- platform: gpio
pin: D1
id: a2_sensor_vcc
interval:
- interval: 60s
then:
- switch.turn_on: a1_sensor_vcc
- component.update: temp_analogue
- component.update: template_analoguesensor_1
- switch.turn_off: a1_sensor_vcc
- switch.turn_on: a2_sensor_vcc
- component.update: temp_analogue
- component.update: template_analoguesensor_2
- switch.turn_off: a2_sensor_vcc

View File

@@ -0,0 +1,136 @@
substitutions:
name: m5stack-atom-echo-80be58
friendly_name: M5Stack Atom Echo 80be58
esphome:
name: "${name}"
friendly_name: "${friendly_name}"
name_add_mac_suffix: false
project:
name: m5stack.atom-echo-voice-assistant
version: "1.0"
min_version: 2023.5.0
api:
encryption:
key: XmXtlYVEuioSNapvz8G/9caKCI0T7aRW/CFy/f83D6g=
esp32:
board: m5stack-atom
framework:
type: esp-idf
logger:
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
dashboard_import:
package_import_url: github://esphome/firmware/voice-assistant/m5stack-atom-echo.yaml@main
improv_serial:
i2s_audio:
i2s_lrclk_pin: GPIO33
i2s_bclk_pin: GPIO19
microphone:
- platform: i2s_audio
id: echo_microphone
i2s_din_pin: GPIO23
adc_type: external
pdm: true
speaker:
- platform: i2s_audio
id: echo_speaker
i2s_dout_pin: GPIO22
dac_type: external
mode: mono
voice_assistant:
microphone: echo_microphone
speaker: echo_speaker
on_start:
- light.turn_on:
id: led
blue: 100%
red: 0%
green: 0%
effect: none
on_tts_start:
- light.turn_on:
id: led
blue: 0%
red: 0%
green: 100%
effect: none
on_tts_end:
- light.turn_on:
id: led
blue: 0%
red: 0%
green: 100%
effect: pulse
on_end:
- delay: 1s
- wait_until:
not:
speaker.is_playing:
- light.turn_off: led
on_error:
- light.turn_on:
id: led
blue: 0%
red: 100%
green: 0%
effect: none
- delay: 1s
- light.turn_off: led
binary_sensor:
- platform: gpio
pin:
number: GPIO39
inverted: true
name: Button
disabled_by_default: true
entity_category: diagnostic
id: echo_button
on_multi_click:
- timing:
- ON FOR AT LEAST 350ms
then:
- voice_assistant.start:
- timing:
- ON FOR AT LEAST 350ms
- OFF FOR AT LEAST 10ms
then:
- light.turn_on:
id: led
blue: 100%
red: 0%
green: 0%
effect: pulse
- voice_assistant.stop:
light:
- platform: esp32_rmt_led_strip
id: led
name: None
disabled_by_default: true
entity_category: config
pin: GPIO27
default_transition_length: 0s
chipset: SK6812
num_leds: 1
rgb_order: grb
rmt_channel: 0
effects:
- pulse:
transition_length: 250ms
update_interval: 250ms

View File

@@ -0,0 +1,17 @@
substitutions:
name: m5stack-atom-echo-82d244
friendly_name: M5Stack Atom Echo 82d244
packages:
m5stack.atom-echo-voice-assistant: github://esphome/firmware/voice-assistant/m5stack-atom-echo.yaml@main
esphome:
name: ${name}
name_add_mac_suffix: false
friendly_name: ${friendly_name}
api:
encryption:
key: Oh2XL+cpsK1jjTO8lW31IZ6CQiQNdqCpUgCZpoe4eYI=
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password

View File

@@ -1,9 +1,8 @@
basement_lights:
name: Basement Lights
entities:
- switch.tasmo_s4chan_4231_underhouselights_a
- switch.tasmo_s4chan_4231_underhouselights_b
- switch.tasmo_ks811t_7846_officelights2_a
- switch.tasmo_ks811t_7846_officelights2_b
- switch.tasmo_sdual_7681_officelights1_a
- switch.tasmo_sdual_7681_officelights1_b
- switch.esp_officelights_relay_1_nighttime_lights
- switch.esp_officelights_relay_2_daytime_lights
- switch.esp_underhouselights_underhouse_entrance_lights
- switch.esp_underhouselights_underhouse_storage_lights
- switch.esp_laundrylights_relay_1_laundry_lights

View File

@@ -1,42 +0,0 @@
battery_levels:
name: Battery Levels
entities:
# Moes Multi Button Scene Switches (CR2430)
- sensor.master_bedroom_scene_switch_east_zss02_battery
- sensor.master_bedroom_scene_switch_west_zss01_battery
# Xiaomi Round Buttons (Button cell 2032)
- sensor.laundry_drier_start_button_zbt01_battery
# Xiaomi Vibration Sensor (Button cell 2032)
- sensor.vibration_sensor_1_zvi01_battery
# Smoke Detectors (CR123A) NO BATTERY INFO
#NO READING- sensor.main_lounge_smoke_detector_zsm01_battery
#NO READING- sensor.downstairs_lounge_lounge_smoke_detector_zsm02_battery
# Xiaomi Leak Detection (Button cell 2032)
- sensor.main_kitchen_sink_leak_zlk01_battery
- sensor.guest_bathroom_laundry_sink_leak_zlk02_battery
- sensor.basement_laundry_floor_leak_zlk03_battery
# offline - sensor.downstairs_kitchen_sink_leak_zlk04_battery
# Tuya Leak Detection (2x AA Batteries)
- sensor.downstairs_kitchen_sink_leak_zlk05_battery
- sensor.downstairs_bathroom_leak_zlk06_battery
# Xiaomi Temp, Humidity Sensors (Button cell 2032)
- sensor.master_bedroom_temperature_zth01_battery
- sensor.bedroom_2_temperature_zth02_battery
- sensor.bedroom_3_temperature_zth03_battery
- sensor.main_bathroom_temperature_zth04_battery
- sensor.storage_loft_temperature_zth05_battery
- sensor.downstairs_bedroom_1_temperature_zth06_battery
# Tuya Temp, Humidity Sensors (2x AAA Batteries)
- sensor.humidor_environment_zth07_battery
- sensor.main_kitchen_fridge_temp_zth08_battery
# Fantem multisensor Sensors (2X CR123A OR 5V MicroUSB)
#TEMP DISABLE - sensor.main_kitchen_multisensor_zms01_battery
- sensor.data_cupboard_multisensor_zms02_battery
# Tuya LCD Temp, Humidity Sensors (2x AAA Batteries)
- sensor.office_lcd_temperature_ztl01_battery
- sensor.master_bedroom_lcd_temperature_ztl02_battery
- sensor.downstairs_lounge_lcd_temperature_ztl03_battery
# Sonoff PIR (CR2450)
- sensor.piano_pir_zir01_battery
# Tuya Reed Switch (2x AAA Batteries)
#NO READING - sensor.downstairs_bedrooms_hatch_zrs01_battery

View File

@@ -0,0 +1,14 @@
downstairs_flat_lights:
name: Downstairs Flat Lights
# All the lights in the Downstairs flat
# Doesn't include the outdoor switch lights, or bedroom wardrobe light
entities:
- switch.esp_downstloungemain_relay_1_main_lights # Lounge Main Lights
- switch.esp_downstloungemain_relay_2_back_wall_lights # Lounge Back Wall Lights
- switch.esp_downstmasterbedrmlights_relay_1_main_lights # Main Bedroom, Main Lights
- switch.esp_downstbedrm2lights_relay_1_main_lights # Small Bedroom Room Lights
- switch.esp_downstkitchlights_relay_2_kitchen_light # Kitchen Lights
- switch.esp_downstkitchlights_relay_1_dining_light # Dining Lights
- switch.esp_downstbathswitch_relay_1_main_lights # Bathroom main lights
- switch.esp_downstbathswitch_relay_2_cabinet_light # Bathroom Cabinet Lights

View File

@@ -1,15 +0,0 @@
foxhole_lights:
name: Foxhole Lights
# Doesn't include the outdoor switch lights
entities:
- switch.tasmo_ks811t_0707_downstloun_2a # Lounge Main Lights
- switch.tasmo_ks811t_0707_downstloun_2b # Lounge Wall Lights
- switch.tasmo_ks811s_3136_downstbed2_1a # Craft Room Lights
- switch.tasmo_ks811t_3642_downstbed1_1a # Main Bedroom, Main Lights
- switch.tasmo_ks811t_3642_downstbed1_1b # Main Bedroom, Wardrobe Light
- switch.tasmo_ks811t_3642_downstbed1_1c # Main Bedroom, Bedside switch
- switch.tasmo_ks811t_2192_downstkitch_1a # Kitchen Entry Lights
- switch.tasmo_ks811t_2192_downstkitch_1b # Kitchen Main Lights
- switch.tasmo_ks811t_2192_downstkitch_1c # Kitchen (Unused, future strip lights)
- switch.tasmo_ks811t_1181_downstbath_a # Downstairs Bathroom Main Lights
- switch.tasmo_ks811t_1181_downstbath_b # Downstairs Bathroom Mirror Lights

View File

@@ -1,21 +0,0 @@
simulation_lights:
name: Simulation Lights
# Lights included in 'away from home' random patterns
entities:
- switch.tasmo_ks811d_1242_entrance_a # Entranceway Main Lights
- switch.tasmo_ks811d_0302_entrybath_a # Entranceway Guest Bathroom Lights
- switch.tasmo_ks811d_6110_kitchen_a # Main Kitchen, Main Lights
- switch.tasmo_ks811d_6110_kitchen_b # Main Kitchen, Bench Lights
- switch.tasmo_ks811s_2940_hallway_1a # Hallway Main Lights
- switch.tasmo_ks811d_1701_stairs_2a # Stairs, Lower ceiling lights
- switch.tasmo_ks811t_0702_lounge_3a # Lounge Main, South
- switch.tasmo_ks811t_0702_lounge_3b # Lounge Main, Middle
- switch.tasmo_ks811t_0702_lounge_3c # Lounge Main, North (above stairs)
- switch.tasmo_ks811t_0707_downstloun_2a # Foxhole Lounge Main Lights
- switch.tasmo_ks811t_0707_downstloun_2b # Foxhole Lounge Wall Lights
- switch.tasmo_ks811s_3136_downstbed2_1a # Foxhole Craft Room Lights
- switch.tasmo_ks811t_3642_downstbed1_1a # Foxhole Main Bedroom, Main Lights
- switch.tasmo_ks811t_2192_downstkitch_1a # Foxhole Kitchen Entry Lights
- switch.tasmo_ks811t_2192_downstkitch_1b # Foxhole Kitchen Main Lights
- switch.tasmo_ks811t_1181_downstbath_a # Foxhole Downstairs Bathroom Main Lights
- switch.tasmo_s4chan_4231_underhouselights_b # Underhouse Main Lights

View File

@@ -0,0 +1,37 @@
automation:
- alias: "Turn on the Wifi Guest Network"
trigger:
platform: state
entity_id: input_boolean.wifi_guest_network
to: "on"
action:
service: media_player.play_media
target:
entity_id: media_player.view_rd_kitchen_echo_show
data:
media_content_type: "custom"
media_content_id: "ask t.p.link to enable guest network"
- alias: "Turn off the Wifi Guest Network"
trigger:
platform: state
entity_id: input_boolean.wifi_guest_network
to: "off"
action:
service: media_player.play_media
target:
entity_id: media_player.view_rd_kitchen_echo_show
data:
media_content_type: "custom"
media_content_id: "ask t.p.link to disable guest network"
- alias: "Switch guest network off after time"
trigger:
platform: state
entity_id: input_boolean.wifi_guest_network
from: "off"
to: "on"
for: "02:00:00"
condition: []
action:
service: input_boolean.turn_off
entity_id: input_boolean.wifi_guest_network
mode: single

View File

@@ -0,0 +1,7 @@
sensor:
- platform: anniversary
name: events
multiple: false
anniversaries:
- event: "Holiday"
date: "2024-9-14"

View File

@@ -1,11 +0,0 @@
mqtt:
sensor:
- unique_id: washing_machine_finished
name: "Washing Machine Finished"
state_topic: "viewroad-status/activityfeed/washercomplete"
- unique_id: dryer_finished
name: "Dryer Finished"
state_topic: "viewroad-status/activityfeed/dryercomplete"
- unique_id: dishwasher_finished
name: "Dishwasher Finished"
state_topic: "viewroad-status/activityfeed/dishwashercomplete"

View File

@@ -0,0 +1,31 @@
mqtt:
sensor:
- unique_id: washing_machine_finished
device_class: timestamp
name: "Washer Finished"
state_topic: "viewroad-status/activityfeed/Washer_complete"
icon: mdi:washing-machine
- unique_id: dryer_finished
name: "Dryer Finished"
state_topic: "viewroad-status/activityfeed/Dryer_complete"
icon: mdi:tumble-dryer
- unique_id: dishwasher_finished
name: "Dishwasher Finished"
state_topic: "viewroad-status/activityfeed/Dishwasher_complete"
icon: mdi:dishwasher
- unique_id: 16Acharger_finished
name: "Leaf Done"
state_topic: "viewroad-status/activityfeed/Leaf_Charger_complete"
icon: mdi:car-electric
- unique_id: 32Acharger_finished
name: "Atto Done"
state_topic: "viewroad-status/activityfeed/Atto_Charger_complete"
icon: mdi:car-electric
- unique_id: Ryobi_Charger_1_finished
name: "Ryobi 1 Done"
state_topic: "viewroad-status/activityfeed/Ryobi_Charger_1_complete"
icon: mdi:circular-saw
- unique_id: Ryobi_Charger_2_finished
name: "Ryobi 2 Done"
state_topic: "viewroad-status/activityfeed/Ryobi_Charger_2_complete"
icon: mdi:circular-saw

Some files were not shown because too many files have changed in this diff Show More