Single ESP8266 Module for Two Lights Issue

Hello All!

I’m running into an issue with my first automation. Maybe someone has an idea of what is going on here!

I’m using a Wemos D1 mini, a WS2812b LED strip, and a PIR motion sensor. What I’m expecting to happen is if motion is detected, turn the LEDs on red and then if no motion is detected for 10 seconds fade the lights off. I got this to work mostly as expected. However I’m running into a snag. I also want to use the LEDs manually to set cool effects and things. Using MQTT, I have topics for my PIR state, the LED lights activated manually, and the LEDs activated via motion.

Here is the lights.yaml configuration:

  - platform: mqtt_json
    name: "Motion Light"
    state_topic: "home/bedroom/motion/light"
    command_topic: "home/bedroom/motion/light/set"
    brightness: true
    flash: true
    rgb: true
    optimistic: false
    qos: 0

- platform: mqtt_json
  name: "Night Light"
  state_topic: "home/bedroom"
  command_topic: "home/bedroom/set"
  effect: true
  effect_list:
    - solid
    - twinkle
  brightness: true
  flash: true
  rgb: true
  optimistic: false
  qos: 0

These lights appear on the GUI fine. So here is my automation configuration for the motion portion. I toyed with a script but either way I ran into an issue.

- alias: "Bedroom Motion Lights On"
  trigger:
    - platform: state
      entity_id: binary_sensor.bedroom_motion
      state: 'on'
  condition:
    - condition: state
      entity_id: light.night_light
      state: 'off'
  action:
  #  - service: light.turn_on
  #    entity_id: light.motion_light
  #    data:
  #      color_name: red
     - service: mqtt.publish
       data_template:
         topic: "home/bedroom/motion/light/set"
         payload: '{"state": "ON", "color": {"b": 0, "r": 255, "g": 0}}'

- alias: "Bedroom Motion Lights Off"
  trigger:
    - platform: state
      entity_id: binary_sensor.bedroom_motion
      from: 'on'         #Not necessary
      to: 'off'          #Not necessary
      state: 'off'
      for:
        seconds: 10
  action:
  #- service: light.turn_off
  #  entity_id: light.motion_light
  #  data:
  #    transition: 5
     - service: mqtt.publish
       data_template:
         topic: "home/bedroom/motion/light/set"
         payload: '{"transition": 5, "state": "OFF"}'

What happens is if motion is detected, the light.motion_light AND light.night_light both turn on. The motion_light turns off immediately and then the night_light turns off after the trigger of no motion for 10 seconds is met. The lights will fade off as expected. The problem with this is if i have the night_light turned on manually, say I want it to display some effect, and trip the sensor, if the sensor is off for 10 seconds it hits that trigger and fades the lights off. I tried a few different things but can’t seem to figure it out. Here is one example that I don’t like at all:

 - alias: "Bedroom Motion Lights On"
  trigger:
    - platform: state
      entity_id: binary_sensor.bedroom_motion
      state: 'on'
  condition:
    - condition: state
      entity_id: light.night_light
      state: 'off'
  action:
  #  - service: light.turn_on
  #    entity_id: light.motion_light
  #    data:
  #      color_name: red
     - service: mqtt.publish
       data_template:
         topic: "home/bedroom/motion/light/set"
         payload: '{"state": "ON", "color": {"b": 0, "r": 255, "g": 0}}'
     - service: automation.turn_off
       entity_id: automation.bedroom_motion_lights_on
- alias: "Bedroom Motion Lights Off"
  trigger:
    - platform: state
      entity_id: binary_sensor.bedroom_motion
      from: 'on'
      to: 'off'
      state: 'off'
      for:
        seconds: 10
  condition:
    - condition: state
      entity_id: automation.bedroom_motion_lights_on
      state: 'off'
  action:
  #- service: light.turn_off
  #  entity_id: light.motion_light
  #  data:
  #    transition: 5
     - service: mqtt.publish
       data_template:
         topic: "home/bedroom/motion/light/set"
         payload: '{"transition": 5, "state": "OFF"}'
     - service: automation.turn_on
       entity_id: automation.bedroom_motion_lights_on

The problem with this is I still can’t really set the lights manually if motion is detected and I don’t like turning that automation entity off and on. I just used it as sort of a place holder condition. Something additional for HA to check. Setting off the motion sensor when I walk into my room is inevitable. What I’d like to do is if I walk in there and set the motion_lights on, I’d like to be able to override that and turn just the night_light on. But for whatever reason the night_light is always used instead of the motion_light in the automation and deactivates the lights if the trigger is hit. I subscribed to both topics (home/bedroom/set and home/bedroom/motion/light/set) and I can see that if motion is detected I get the correct payload from home/bedroom/motion/light/set and if it’s not detected for 10 seconds I see the OFF and transition payload. And if I turn the lights on manually I see the correct payload published to home/bedroom/set. So I’m stumped as to why HA is actually turning night_light on instead of motion_light when I’m expecting it to. I can post parts of the ESP ino file if that would help at all. It might be a lot to digest but I’d appreciate any input and if any additional info is required (like parts of the ino file or some screen shots or shoot even a video) let me know!

I’m replying to myself in case I figure it out and someone else stumbles into this as well. I’m going to enable verbose logging and see what exactly HA is doing.