From 02f3e44248c3de5c7bb305b3b6cdd7a2c87818c0 Mon Sep 17 00:00:00 2001 From: ESPHome Device Builder Date: Fri, 12 Jun 2026 22:03:22 +1200 Subject: [PATCH] Edit esp-masterbdrmwest.yaml --- esphome/esp-masterbdrmwest.yaml | 313 ++++++++++++++++---------------- 1 file changed, 153 insertions(+), 160 deletions(-) diff --git a/esphome/esp-masterbdrmwest.yaml b/esphome/esp-masterbdrmwest.yaml index bbabb43..a2578dc 100644 --- a/esphome/esp-masterbdrmwest.yaml +++ b/esphome/esp-masterbdrmwest.yaml @@ -6,9 +6,12 @@ # https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-masterbdrmwest.yaml #:########################################################################################:# # VERSIONS: -# v1.1 2026-06-12 North Lights now controlled via Tasmota iFan02 MQTT cmnd/stat topics. -# Relay 2 on both switches is virtual/mirror only and follows Tasmota feedback. -# Added sync guard globals/scripts to prevent MQTT/HA feedback chatter. +# v1.2 2026-06-12 Reworked after live wiring checks. +# Confirmed North Lights are controlled by Tasmota/iFan02 Power1. +# Confirmed South Lights physical load is on this WEST switch Relay 2. +# Relay 1 is now a virtual South Lights control/mirror. +# Button 2 now sends Tasmota POWER1 TOGGLE and listens to stat POWER1 feedback. +# v1.1 2026-06-12 North Lights controlled via Tasmota iFan02 MQTT cmnd/stat topics. # v1.0 2026-06-11 Initial YAML for Master Bedroom West 3-gang switch. #:########################################################################################:# # HARDWARE: @@ -17,65 +20,53 @@ #:########################################################################################:# # OPERATION NOTES: # - 3-gang KS-811 triple switch. -# - Relay 1 = Master Bedroom South Lights. VIRTUAL/MIRROR ONLY on this switch; no load connected. -# - Relay 2 = Master Bedroom North Lights. VIRTUAL/MIRROR ONLY on this switch; no load connected. -# - Actual North Lights are controlled by Tasmota iFan02 entity light.tasmo_ifan02_3793_bedrm1_1. +# - Button 1 = Master Bedroom South Lights. +# * Actual South Lights physical load is on this WEST switch Relay 2. +# * Button 1 directly toggles Relay 2 for local/offline operation. +# * Relay 1 is virtual/mirror only and follows the South Lights state. +# - Button 2 = Master Bedroom North Lights. +# * Actual North Lights are controlled by Tasmota/iFan02 light.tasmo_ifan02_3793_bedrm1_1. +# * Button 2 sends MQTT TOGGLE to the Tasmota POWER1 command topic. +# * Relay 2 is NOT used as a North Lights mirror because it is the physical South Lights load. +# - Button 3 = Bedside Lamp Jump/Fade via Home Assistant action. +# * Requires ESPHome integration permission: "Allow the device to perform Home Assistant actions". # - Relay 3 = unused output. Forced off/internal; Button 3 does NOT toggle Relay 3. -# - Button 1 toggles South Lights locally by toggling Relay 1. -# - Button 2 requests North Lights ON/OFF via Tasmota MQTT and waits for Tasmota stat feedback. -# - Button 3 checks the Home Assistant bedside lamp jump/fade switch state and calls the opposite HA action. -# - South Lights are mirrored to the matching south switch in two ways: -# Method 1: HA blueprint sync between the relay entities (fast path when HA is online). -# Method 2: MQTT retained status sync between switches (backup path if HA is down, broker online). -# - North Lights are mirrored using: -# Method 1: HA blueprint sync between the two relay entities and light.tasmo_ifan02_3793_bedrm1_1. -# Method 2: Direct Tasmota MQTT cmnd/stat topics. Tasmota stat is the source of truth for Relay 2. # - GPIO16 does not support interrupts; polling is used for Button 1 (suppresses warning). #:########################################################################################:# # MQTT COMMANDS: # - North Lights Tasmota command topic: # ${north_lights_tasmota_command_topic} -# Payload "ON" -> iFan02 Light ON -# Payload "OFF" -> iFan02 Light OFF -# Empty payload -> Query current Power1 state +# Payload "TOGGLE" -> iFan02 Light toggle +# Payload "ON" -> iFan02 Light ON +# Payload "OFF" -> iFan02 Light OFF +# Empty payload -> Query current POWER1 state # # - North Lights Tasmota status topic: # ${north_lights_tasmota_status_topic} -# Payload "ON" / "OFF" updates Relay 2 mirror state on both bedroom switches. +# Payload "ON" / "OFF" updates internal North Lights state. # -# - North Lights local command topic: -# ${mqtt_local_command_topic1}/set -# Payload "On"/"ON" -> Publish Tasmota Power1 ON command -# Payload "Off"/"OFF" -> Publish Tasmota Power1 OFF command -# -# - North Lights local status topic: -# ${mqtt_local_status_topic1}/state -# Payload "On" / "Off" retained when Relay 2 mirror changes. -# NOTE: For North Lights, this is a local status mirror only. Tasmota stat is the sync source of truth. -# -# - South Lights command topic: +# - South Lights local command topic: # ${mqtt_local_command_topic2}/set -# Payload "On" -> Relay 1 ON -# Payload "Off" -> Relay 1 OFF +# Payload "On" / "ON" -> Relay 2 ON +# Payload "Off" / "OFF" -> Relay 2 OFF # # - South Lights retained status/sync topic: # ${mqtt_local_status_topic2}/state -# Payload "On" / "Off" published when Relay 1 changes state. -# This same retained status topic is also subscribed to for HA-down two-way sync. +# Payload "On" / "Off" published when Relay 2 physical South Lights changes state. #:########################################################################################:# # OFFLINE NOTES: # a) Home Assistant OFFLINE, WiFi OK, MQTT broker OK -# - Button 1 still controls/syncs South Lights between both switches via MQTT retained status. -# - Button 2 still controls the Tasmota/iFan02 North Lights via direct MQTT cmnd/stat topics. -# - iFan02/Tasmota feedback updates Relay 2 mirror state on both switches via stat topic. +# - Button 1 controls the South Lights locally via WEST Relay 2 and publishes MQTT status. +# - Button 2 controls the North Lights through direct Tasmota MQTT command. # - Button 3 cannot toggle the bedside lamp because it requires Home Assistant/API. # # b) MQTT OFFLINE (or broker unreachable), Home Assistant/API OK -# - Button 1 and Button 2 can still control/sync through HA where the relevant HA entities are available. +# - Button 1 still controls the South Lights locally via WEST Relay 2. +# - Button 2 attempts HA action fallback to light.toggle the Tasmota/iFan02 HA entity. # - Button 3 can still toggle the bedside lamp via Home Assistant/API. # # c) Home Assistant and MQTT OFFLINE, WiFi OK or entire network OFFLINE -# - Button 1 controls local Relay 1 only. +# - Button 1 still controls the local South Lights physical relay. # - Button 2 cannot control the North Lights because they are on the separate Tasmota/iFan02 controller. # - Button 3 cannot control the bedside lamp. # - Accurate time is NOT needed (SNTP not needed). @@ -90,12 +81,12 @@ substitutions: # Device Naming device_name: "esp-masterbdrmwest" friendly_name: "Master Bedroom Main Lights West" - description_comment: "Master Bedroom Main Lights West using a Zemismart KS-811 Triple Push Button. South Lights virtual on Relay 1, North Lights Tasmota/iFan02 mirror on Relay 2, Button 3 toggles bedside lamp jump/fade. (Layout V1.1)" + description_comment: "Master Bedroom Main Lights West using a Zemismart KS-811 Triple Push Button. South Lights physical on Relay 2, Relay 1 South Lights mirror, North Lights Tasmota/iFan02 control on Button 2, Button 3 toggles bedside lamp jump/fade. (Layout V1.1)" device_area: "Master Bedroom" # Project Naming project_name: "Zemismart Technologies.KS-811 Triple" - project_version: "v1.1" + project_version: "v1.2" # Passwords & Secrets api_key: !secret esp-api_key @@ -113,23 +104,27 @@ substitutions: update_interval: "60s" # MQTT LOCAL Controls - # NOTE: device_name1 is North Lights and maps to Relay 2 mirror on both bedroom switches. + # NOTE: device_name1 is North Lights and maps to the Tasmota/iFan02 light. mqtt_device_name1: "masterbrdm-northlights" mqtt_local_command_topic1: "${mqtt_command_main_topic}/${mqtt_device_name1}" mqtt_local_status_topic1: "${mqtt_status_main_topic}/${mqtt_device_name1}" - # NOTE: device_name2 is South Lights and maps to Relay 1 on both bedroom switches. + # NOTE: device_name2 is South Lights and maps to this WEST switch Relay 2 physical load. mqtt_device_name2: "masterbrdm-southlights" mqtt_local_command_topic2: "${mqtt_command_main_topic}/${mqtt_device_name2}" mqtt_local_status_topic2: "${mqtt_status_main_topic}/${mqtt_device_name2}" # Tasmota/iFan02 North Lights MQTT control and feedback - north_lights_tasmota_command_topic: "cmnd/tasmo-ifan02-3793-bedrm1-1/Power1" - north_lights_tasmota_status_topic: "stat/tasmo-ifan02-3793-bedrm1-1/Power1" + # NOTE: Tasmota status topic observed as uppercase POWER1. MQTT topics are case-sensitive. + north_lights_tasmota_command_topic: "cmnd/tasmo-ifan02-3793-bedrm1-1/POWER1" + north_lights_tasmota_status_topic: "stat/tasmo-ifan02-3793-bedrm1-1/POWER1" + + # Home Assistant fallback entity + north_lights_ha_entity: "light.tasmo_ifan02_3793_bedrm1_1" # Switch/Relay/Button Naming switch_1_name: "Master Bedroom South Lights" - switch_2_name: "Master Bedroom North Lights" + switch_2_name: "Master Bedroom South Lights" # NOTE: Relay 2 is the physical South Lights load on this WEST switch switch_3_name: "Bedside Lamp Jump/Fade" # NOTE: Relay 3 output is unused and forced off # Home Assistant bedside lamp entity controlled by Button 3 @@ -200,15 +195,15 @@ logger: #:########################################################################################:# # GLOBALS -# Used to suppress MQTT feedback loops/chatter when a relay state is being set from MQTT feedback. +# Used to suppress MQTT/HA feedback loops and hold the last known remote states. #:########################################################################################:# globals: - - id: south_update_from_mqtt + - id: south_update_from_sync type: bool restore_value: false initial_value: 'false' - - id: north_update_from_tasmota + - id: north_lights_state type: bool restore_value: false initial_value: 'false' @@ -238,38 +233,44 @@ mqtt: id(request_north_lights_on).execute(); } else if (x == "Off" || x == "OFF" || x == "off") { id(request_north_lights_off).execute(); + } else if (x == "Toggle" || x == "TOGGLE" || x == "toggle") { + id(request_north_lights_toggle).execute(); } - # Tasmota/iFan02 Power1 feedback is the source of truth for Relay 2 mirror state. + # Tasmota/iFan02 POWER1 feedback is the source of truth for North Lights state. - topic: "${north_lights_tasmota_status_topic}" then: - lambda: |- if (x == "ON" || x == "On" || x == "on") { - id(set_north_on_from_tasmota).execute(); + id(north_lights_state) = true; + id(publish_north_status_on).execute(); } else if (x == "OFF" || x == "Off" || x == "off") { - id(set_north_off_from_tasmota).execute(); + id(north_lights_state) = false; + id(publish_north_status_off).execute(); } # ------------------------------------------------------------------------- - # South Lights control/sync (Relay 1 on both bedroom switches) + # South Lights command/status. Actual physical South load is this Relay 2. # ------------------------------------------------------------------------- - topic: "${mqtt_local_command_topic2}/set" then: - lambda: |- if (x == "On" || x == "ON" || x == "on") { - id(set_south_on_from_command).execute(); + id(request_south_lights_on).execute(); } else if (x == "Off" || x == "OFF" || x == "off") { - id(set_south_off_from_command).execute(); + id(request_south_lights_off).execute(); + } else if (x == "Toggle" || x == "TOGGLE" || x == "toggle") { + id(toggle_south_lights_local).execute(); } - # Retained status is also subscribed to as the MQTT backup sync path. + # Retained South status is used only to keep Relay 1 mirror in step. - topic: "${mqtt_local_status_topic2}/state" then: - lambda: |- if (x == "On" || x == "ON" || x == "on") { - id(set_south_on_from_mqtt).execute(); + id(set_south_mirror_on).execute(); } else if (x == "Off" || x == "OFF" || x == "off") { - id(set_south_off_from_mqtt).execute(); + id(set_south_mirror_off).execute(); } #:########################################################################################:# @@ -287,27 +288,27 @@ status_led: #:########################################################################################:# binary_sensor: # Button 1 (GPIO16) - polling (GPIO16 has no interrupts) + # South Lights: direct local physical control through Relay 2. - platform: gpio pin: number: GPIO16 mode: INPUT inverted: true use_interrupt: false - name: "Button 1: ${switch_1_name}" + name: "Button 1: Master Bedroom South Lights" on_press: - - switch.toggle: relay_1 + - switch.toggle: relay_2 # Button 2 (GPIO05) - # NOTE: This does not toggle Relay 2 directly. It sends the opposite command to the iFan02/Tasmota light. - # Relay 2 mirror state is then updated by the Tasmota stat/Power1 feedback topic. + # North Lights: direct TOGGLE to Tasmota/iFan02. Relay 2 is not used for North because it is South physical load. - platform: gpio pin: number: GPIO05 mode: INPUT inverted: true - name: "Button 2: ${switch_2_name}" + name: "Button 2: Master Bedroom North Lights" on_press: - - script.execute: toggle_north_lights + - script.execute: request_north_lights_toggle # Button 3 (GPIO04) # NOTE: This does not toggle Relay 3. It checks the HA entity state and calls the opposite HA action. @@ -332,8 +333,71 @@ binary_sensor: #:########################################################################################:# script: # --------------------------------------------------------------------------- - # North Lights scripts - Tasmota/iFan02 is source of truth + # South Lights helpers + # Actual physical South Lights relay is this WEST switch Relay 2. # --------------------------------------------------------------------------- + - id: request_south_lights_on + mode: restart + then: + - switch.turn_on: relay_2 + + - id: request_south_lights_off + mode: restart + then: + - switch.turn_off: relay_2 + + - id: toggle_south_lights_local + mode: restart + then: + - switch.toggle: relay_2 + + - id: set_south_mirror_on + mode: restart + then: + - lambda: |- + id(south_update_from_sync) = true; + - switch.turn_on: relay_1 + - lambda: |- + id(south_update_from_sync) = false; + + - id: set_south_mirror_off + mode: restart + then: + - lambda: |- + id(south_update_from_sync) = true; + - switch.turn_off: relay_1 + - lambda: |- + id(south_update_from_sync) = false; + + # --------------------------------------------------------------------------- + # North Lights helpers + # Actual North Lights are Tasmota/iFan02 POWER1. + # --------------------------------------------------------------------------- + - id: request_north_lights_toggle + mode: restart + then: + - if: + condition: + mqtt.connected: + then: + - mqtt.publish: + topic: "${north_lights_tasmota_command_topic}" + payload: "TOGGLE" + else: + - if: + condition: + api.connected: + state_subscription_only: true + then: + - homeassistant.action: + action: light.toggle + data: + entity_id: "${north_lights_ha_entity}" + else: + - logger.log: + level: WARN + format: "Button 2 pressed, but MQTT and HA/API are not connected. North Lights not changed." + - id: request_north_lights_on mode: restart then: @@ -348,75 +412,25 @@ script: topic: "${north_lights_tasmota_command_topic}" payload: "OFF" - - id: toggle_north_lights + - id: publish_north_status_on mode: restart then: - - if: - condition: - switch.is_on: relay_2 - then: - - script.execute: request_north_lights_off - else: - - script.execute: request_north_lights_on + - mqtt.publish: + topic: "${mqtt_local_status_topic1}/state" + payload: "On" + retain: true - - id: set_north_on_from_tasmota + - id: publish_north_status_off mode: restart then: - - lambda: |- - id(north_update_from_tasmota) = true; - - switch.turn_on: relay_2 - - delay: 100ms - - lambda: |- - id(north_update_from_tasmota) = false; - - - id: set_north_off_from_tasmota - mode: restart - then: - - lambda: |- - id(north_update_from_tasmota) = true; - - switch.turn_off: relay_2 - - delay: 100ms - - lambda: |- - id(north_update_from_tasmota) = false; + - mqtt.publish: + topic: "${mqtt_local_status_topic1}/state" + payload: "Off" + retain: true # --------------------------------------------------------------------------- - # South Lights scripts - MQTT retained status sync with no re-publish chatter + # Bedside lamp helper # --------------------------------------------------------------------------- - - id: set_south_on_from_command - mode: restart - then: - - switch.turn_on: relay_1 - - - id: set_south_off_from_command - mode: restart - then: - - switch.turn_off: relay_1 - - - id: set_south_on_from_mqtt - mode: restart - then: - - lambda: |- - id(south_update_from_mqtt) = true; - - switch.turn_on: relay_1 - - delay: 100ms - - lambda: |- - id(south_update_from_mqtt) = false; - - - id: set_south_off_from_mqtt - mode: restart - then: - - lambda: |- - id(south_update_from_mqtt) = true; - - switch.turn_off: relay_1 - - delay: 100ms - - lambda: |- - id(south_update_from_mqtt) = false; - - # --------------------------------------------------------------------------- - # Button 3 - HA bedside lamp jump/fade switch - # --------------------------------------------------------------------------- - # Toggle the HA bedside lamp jump/fade switch by checking the current imported HA state first. - # If it is ON, send OFF. If it is OFF/unknown, send ON. - id: toggle_bedside_lamp_jump_fade mode: restart then: @@ -455,26 +469,17 @@ switch: id: relay_1 restore_mode: RESTORE_DEFAULT_OFF on_turn_on: - - if: - condition: - lambda: 'return !id(south_update_from_mqtt);' - then: - - mqtt.publish: - topic: "${mqtt_local_status_topic2}/state" - payload: "On" - retain: true + - lambda: |- + if (!id(south_update_from_sync)) { + id(request_south_lights_on).execute(); + } on_turn_off: - - if: - condition: - lambda: 'return !id(south_update_from_mqtt);' - then: - - mqtt.publish: - topic: "${mqtt_local_status_topic2}/state" - payload: "Off" - retain: true + - lambda: |- + if (!id(south_update_from_sync)) { + id(request_south_lights_off).execute(); + } - # Relay 2 (GPIO12) - Master Bedroom North Lights - VIRTUAL/MIRROR ONLY ON THIS SWITCH - # Tasmota stat/Power1 is the source of truth. Local relay state mirrors actual North Lights state. + # Relay 2 (GPIO12) - Master Bedroom South Lights - PHYSICAL LOAD CONNECTED HERE - platform: gpio name: "Relay 2: ${switch_2_name}" pin: GPIO12 @@ -482,28 +487,16 @@ switch: restore_mode: RESTORE_DEFAULT_OFF on_turn_on: - mqtt.publish: - topic: "${mqtt_local_status_topic1}/state" + topic: "${mqtt_local_status_topic2}/state" payload: "On" retain: true - - if: - condition: - lambda: 'return !id(north_update_from_tasmota);' - then: - - mqtt.publish: - topic: "${north_lights_tasmota_command_topic}" - payload: "ON" + - script.execute: set_south_mirror_on on_turn_off: - mqtt.publish: - topic: "${mqtt_local_status_topic1}/state" + topic: "${mqtt_local_status_topic2}/state" payload: "Off" retain: true - - if: - condition: - lambda: 'return !id(north_update_from_tasmota);' - then: - - mqtt.publish: - topic: "${north_lights_tasmota_command_topic}" - payload: "OFF" + - script.execute: set_south_mirror_off # Relay 3 (GPIO14) - unused output, intentionally not exposed to Home Assistant. # Button 3 controls the bedside lamp via Home Assistant/API instead.