diff --git a/esphome/esp-masterbdrmsouth.yaml b/esphome/esp-masterbdrmsouth.yaml index 77b2d6e..aecf632 100644 --- a/esphome/esp-masterbdrmsouth.yaml +++ b/esphome/esp-masterbdrmsouth.yaml @@ -6,9 +6,13 @@ # https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-masterbdrmsouth.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 WEST switch Relay 2. +# South switch Relay 1 is now a virtual South Lights control/mirror. +# South switch Relay 2 is now a virtual North 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 South 3-gang switch. #:########################################################################################:# # HARDWARE: @@ -17,66 +21,54 @@ #:########################################################################################:# # OPERATION NOTES: # - 3-gang KS-811 triple switch. -# - Relay 1 = Master Bedroom South Lights. PHYSICAL LOAD CONNECTED on this switch. -# - 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. +# * On this SOUTH switch, Relay 1 is virtual/mirror only. +# * Actual South Lights physical load is WEST switch Relay 2. +# * Button 1 toggles Relay 1, which sends the South Lights command to the WEST switch. +# - 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 virtual/mirror only and follows Tasmota POWER1 feedback. +# - 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 west 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 Relay 2 mirror state on this switch. # -# - 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" -> South Lights ON via WEST Relay 2 +# Payload "Off" / "OFF" -> South Lights OFF via WEST Relay 2 # # - 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" mirrors the actual South Lights 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 through MQTT command to the WEST switch. +# - Button 2 controls the North Lights through direct Tasmota MQTT command. +# - Tasmota feedback updates Relay 2 mirror via stat POWER1. # - 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 attempts HA action fallback to the WEST physical South Lights relay entity. +# - 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 2 cannot control the North Lights because they are on the separate Tasmota/iFan02 controller. +# - This SOUTH switch cannot control the remote loads. # - Button 3 cannot control the bedside lamp. # - Accurate time is NOT needed (SNTP not needed). #:########################################################################################:# @@ -90,12 +82,12 @@ substitutions: # Device Naming device_name: "esp-masterbdrmsouth" friendly_name: "Master Bedroom Main Lights South" - description_comment: "Master Bedroom Main Lights South using a Zemismart KS-811 Triple Push Button. South Lights physical 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 South using a Zemismart KS-811 Triple Push Button. South Lights virtual control on Relay 1, North Lights Tasmota/iFan02 virtual mirror on Relay 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,19 +105,25 @@ 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 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 entities + north_lights_ha_entity: "light.tasmo_ifan02_3793_bedrm1_1" + # NOTE: If HA renames WEST Relay 2 after the YAML name correction, update this entity_id. + south_lights_physical_entity: "switch.master_bedroom_master_bedroom_main_lights_west_relay_2_master_bedroom_north_lights" # Switch/Relay/Button Naming switch_1_name: "Master Bedroom South Lights" @@ -200,10 +198,10 @@ 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' @@ -213,6 +211,16 @@ globals: restore_value: false initial_value: 'false' + - id: south_lights_state + type: bool + restore_value: false + initial_value: 'false' + + - id: north_lights_state + type: bool + restore_value: false + initial_value: 'false' + #:########################################################################################:# # MQTT: Device-specific MQTT command + retained status/sync triggers # This adds device-specific MQTT command triggers to the common MQTT configuration. @@ -238,9 +246,11 @@ 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: |- @@ -251,25 +261,25 @@ mqtt: } # ------------------------------------------------------------------------- - # South Lights control/sync (Relay 1 on both bedroom switches) + # South Lights command/status. Actual physical South load is WEST Relay 2. + # This SOUTH switch mirrors the state on Relay 1. # ------------------------------------------------------------------------- - topic: "${mqtt_local_command_topic2}/set" then: - lambda: |- if (x == "On" || x == "ON" || x == "on") { - id(set_south_on_from_command).execute(); + id(set_south_on_from_sync).execute(); } else if (x == "Off" || x == "OFF" || x == "off") { - id(set_south_off_from_command).execute(); + id(set_south_off_from_sync).execute(); } - # Retained status is also subscribed to as the MQTT backup sync path. - 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_on_from_sync).execute(); } else if (x == "Off" || x == "OFF" || x == "off") { - id(set_south_off_from_mqtt).execute(); + id(set_south_off_from_sync).execute(); } #:########################################################################################:# @@ -287,6 +297,7 @@ status_led: #:########################################################################################:# binary_sensor: # Button 1 (GPIO16) - polling (GPIO16 has no interrupts) + # South Lights: toggles virtual Relay 1, which sends command to WEST physical Relay 2. - platform: gpio pin: number: GPIO16 @@ -298,8 +309,7 @@ binary_sensor: - switch.toggle: relay_1 # 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 follows Tasmota feedback only. - platform: gpio pin: number: GPIO05 @@ -307,7 +317,7 @@ binary_sensor: inverted: true name: "Button 2: ${switch_2_name}" 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 +342,108 @@ binary_sensor: #:########################################################################################:# script: # --------------------------------------------------------------------------- - # North Lights scripts - Tasmota/iFan02 is source of truth + # South Lights helpers + # Actual physical South Lights relay is WEST switch Relay 2. # --------------------------------------------------------------------------- + - id: request_south_lights_on + mode: restart + then: + - if: + condition: + mqtt.connected: + then: + - mqtt.publish: + topic: "${mqtt_local_command_topic2}/set" + payload: "On" + else: + - if: + condition: + api.connected: + state_subscription_only: true + then: + - homeassistant.action: + action: switch.turn_on + data: + entity_id: "${south_lights_physical_entity}" + else: + - logger.log: + level: WARN + format: "Button 1 pressed, but MQTT and HA/API are not connected. South Lights not changed." + + - id: request_south_lights_off + mode: restart + then: + - if: + condition: + mqtt.connected: + then: + - mqtt.publish: + topic: "${mqtt_local_command_topic2}/set" + payload: "Off" + else: + - if: + condition: + api.connected: + state_subscription_only: true + then: + - homeassistant.action: + action: switch.turn_off + data: + entity_id: "${south_lights_physical_entity}" + else: + - logger.log: + level: WARN + format: "Button 1 pressed, but MQTT and HA/API are not connected. South Lights not changed." + + - id: set_south_on_from_sync + mode: restart + then: + - lambda: |- + id(south_lights_state) = true; + id(south_update_from_sync) = true; + - switch.turn_on: relay_1 + - lambda: |- + id(south_update_from_sync) = false; + + - id: set_south_off_from_sync + mode: restart + then: + - lambda: |- + id(south_lights_state) = false; + 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,24 +458,17 @@ script: topic: "${north_lights_tasmota_command_topic}" payload: "OFF" - - id: toggle_north_lights - mode: restart - then: - - if: - condition: - switch.is_on: relay_2 - then: - - script.execute: request_north_lights_off - else: - - script.execute: request_north_lights_on - - id: set_north_on_from_tasmota mode: restart then: - lambda: |- + id(north_lights_state) = true; id(north_update_from_tasmota) = true; - switch.turn_on: relay_2 - - delay: 100ms + - mqtt.publish: + topic: "${mqtt_local_status_topic1}/state" + payload: "On" + retain: true - lambda: |- id(north_update_from_tasmota) = false; @@ -373,50 +476,19 @@ script: mode: restart then: - lambda: |- + id(north_lights_state) = false; id(north_update_from_tasmota) = true; - switch.turn_off: relay_2 - - delay: 100ms + - mqtt.publish: + topic: "${mqtt_local_status_topic1}/state" + payload: "Off" + retain: true - lambda: |- id(north_update_from_tasmota) = false; # --------------------------------------------------------------------------- - # 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: @@ -448,62 +520,39 @@ script: # https://esphome.io/components/switch/ #:########################################################################################:# switch: - # Relay 1 (GPIO13) - Master Bedroom South Lights - PHYSICAL LOAD CONNECTED HERE + # Relay 1 (GPIO13) - Master Bedroom South Lights - VIRTUAL/MIRROR ONLY ON THIS SWITCH - platform: gpio name: "Relay 1: ${switch_1_name}" pin: GPIO13 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. - platform: gpio name: "Relay 2: ${switch_2_name}" pin: GPIO12 id: relay_2 restore_mode: RESTORE_DEFAULT_OFF on_turn_on: - - mqtt.publish: - topic: "${mqtt_local_status_topic1}/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" + - lambda: |- + if (!id(north_update_from_tasmota)) { + id(request_north_lights_on).execute(); + } on_turn_off: - - mqtt.publish: - topic: "${mqtt_local_status_topic1}/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" + - lambda: |- + if (!id(north_update_from_tasmota)) { + id(request_north_lights_off).execute(); + } # Relay 3 (GPIO14) - unused output, intentionally not exposed to Home Assistant. # Button 3 controls the bedside lamp via Home Assistant/API instead.