Help with light automation using Hue bridge and HASS IO with and without MQTT

Hi, all

I recently went with Phillips Hue route with lighting. I have the basic starter kit that included to lightbulbs and the bridge. I also have a Smart Things door sensor. I’m trying to get the lightbult in my living room to turn on when I open a certain door between sunset and sunrise. It worked well with a regular bulb and a Smart Things switch, but I have done away with the switch for now and my Smat Things app can’t read my Hue bridge for some reason.

ANYWAY…My goal is to migrate to Home Assistant I currently have Smart Things communicating with HA through MQTT, and it appears to be functioning. However, I’ve tried to create two different automations to activate my light (one using MQTT and another as a “state” of a device monitored by MQTT) and neither are working.

  • id: ‘XXXXXXXXXXX’
    alias: Turn on Living Room Lamp after Door Open after Sunset
    trigger:
    • entity_id: binary_sensor.garage_house_door
      from: closed
      platform: state
      to: open
      condition:
    • after: sunset
      before: sunrise
      condition: sun
      action:
    • data:
      entity_id: light.living_room_lamp_1
      service: light.turn_on
  • id: ‘XXXXXXXXXXXXX’
    alias: MQTT Turn on Living Room Lamp at Door Open
    trigger:
    • payload: open
      platform: mqtt
      topic: smartthings/Garage House Door/contact/state
      condition:
    • after: sunset
      before: sunrise
      condition: sun
      action:
    • data:
      entity_id: light.living_room_lamp_1
      service: light.turn_on

I’ve tried one and then the other, both with no results.

Any suggestions?

Part of the reason you may not have received any replies yet is because you did not properly format your YAML code, which makes it pretty much unreadable. Please see the instructions at the top of the page and reformat. If it doesn’t look right, then delete it, paste it back in directly from your text editor, and format it right away.

Having said that, I can’t really comment on the second automation since I don’t use MQTT. But for the first automation (at least the best I can read it as-is), you have two issues.

First, a binary_sensor is always ‘on’ or ‘off’ (well, when not ‘unknown’ or ‘unavailable’), not open or closed. For a door binary_sensor, ‘on’ means open and ‘off’ means closed.

Second, your condition says it has to be after sunset and before sunrise, which is impossible. After sunset goes from sunset to midnight, and before sunrise goes from midnight to sunrise. It can’t be both at the same time. What you want is a condition that is true if one or the other is true. See sun condition where it explains this very scenario. The other alternative, which is simpler, is to use a condition like this (if you don’t need offsets):

  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon