Automated light color change after dark?

Hi all,

This is my first post on the HA forums. I’ve begun setting up my HA and right now I have outdoor bulbs automatically turning on triggered by sunset state … indoors I have bulbs automatically turning on, condition: after sunset, triggered by: motion sensor. On the indoor bulbs, I’d like them to turn on, triggered by motion (condition after sunset), color: RED after a certain hour. Is this possible? I’m using Philips Hue bulbs without a hub (zigbee2mqtt).

I did a quick search of the forum and didn’t find anything. Thanks in advance!

what is the brand of the bulbs you are using?

depending on the brand it will determine what the commands will be to set the color of the bulb (if it’s supported).

Once you post that info then also post the code that you already have (in the correct format using the instructions above). Then we can see what you have and recommend changes based on your actual entities.

So I got the color working using RGB and I am using the newest gen Philips Hue Color Ambiance bulbs. Exact model is 9290012575A. These are the scripts I’m currently running (no motion detection) but would like to do accomplish the same thing indoors WITH motion detection. For instance, I’d like to walk into the hallway or bathroom after 8pm, lights turn on red, I leave, lights turn off. This is my automations.yaml:

## CHANGE LIGHTS TO RED ON SUNSET

  - alias: Auto light change to RED
    trigger:
      - platform: sun
        event: sunset
        offset: "-00:10:00"
    action:
      - service_template: >
          {% if is_state('group.bedroom_lights', 'on') %}
            input_boolean.turn_on
          {% else %}
            input_boolean.turn_off
          {% endif %}
        entity_id: input_boolean.prev_light_state
      - service: light.turn_on
        data:
          entity_id: group.bedroom_lights
          rgb_color: [255, 0, 0]
          brightness: 255
          transition: 50
      - condition: state
        entity_id: input_boolean.prev_light_state
        state: 'off'
      - service: light.turn_off
        entity_id: group.bedroom_lights
        
## CHANGE LIGHTS TO WHITE ON SUNRISE

  - alias: Auto light change to WHITE
    trigger:
      - platform: sun
        event: sunrise
    action:
      - service_template: >
          {% if is_state('group.bedroom_lights', 'on') %}
            input_boolean.turn_on
          {% else %}
            input_boolean.turn_off
          {% endif %}
        entity_id: input_boolean.prev_light_state
      - service: light.turn_on
        data:
          entity_id: group.bedroom_lights
          rgb_color: [255, 255, 204]
          brightness: 125
      transition: 600
  - condition: state
    entity_id: input_boolean.prev_light_state
    state: 'off'
  - service: light.turn_off
    entity_id: group.bedroom_lights

TURN OFF BEDROOM LIGHTS AT 10:30PM

 - alias: "Bedroom Lights Out"
   trigger:
     - platform: time
       at: '22:30:00'
   action:
     - service: homeassistant.turn_off
       data:
         entity_id:
           - group.bedroom_lights

Adittionally, if possible, I’d like to figure out how to fade the lights in (in the morning, starting from a dim red and fading slowly to a soft white/yellow, like I have now).

Thanks!

I see there are some errors in the formatting but because my account is new it wouldn’t let me edit more than once. Please forgive my errors.

you can use transition to do that.
Some more info here: Light - Home Assistant
Or a example here: https://www.home-assistant.io/cookbook/dim_lights_when_playing_media/

Shold be like this

   action:
     - service: lights.turn_off
       transition: 2
       data:
         entity_id:
           - group.bedroom_lights

But its not tested. Yet :wink:

Let me Edit: But I have read somewhere on the homepage, that transition is not supported by every light platform. May some (exotic?) devices wouldn’t work

Yes, I’m currently using transition in some of the automation already. How would I incorporate transitioning from one color to another? Currently the bulb uses last state, which is okay as last state will generally be red at full brightness … but I’d like to fade from red at low brightness in the morning, slowly to medium brightness in yellow.

I think something like this will do it:

trigger:
  platform: state
  entity_id: your_motion_sensor
  to: 'on'
condition:
  - condition: sun
    after: sunset
  - condition: sun
    before: sunrise
action:
  - service: light.turn_on
    data_template:
      entity_id: group.bedroom_lights
      brightness: 125
      rgb_color: >
        {% if now().hour > 19 or now().hour < 7 %}      
          [255, 255, 204]
        {% else %}
          what ever color
        {% endif %}
  - service: whatever other action you want 

the time condition is only referenced to midnight so “hour > 19 or < 7” is between the hours of 7 pm & 7 am.

   trigger:
     platform: state
     entity_id: your_motion_sensor
     to: 'off'
   action:
     - service: light.turn_off
       entity_id: group.bedroom_lights
     - service: whatever other action you want

I know this turns the light off, but I want to turn them off after x amount if inactivity. Additionaly, would switch.turn_off be better than light.turn_off?

something like this:

trigger:
platform: state
entity_id: your_motion_sensor
to: ‘off’
for:
minutes: 3
action:
- service: light.turn_off
entity_id: group.bedroom_lights
- service: whatever other action you want

This would turn light off after 3 minutes of inactivity, correct? Thank you so much for your help, the rgb “else, endif” makes so much sense seeing it like that.

EDIT: Additionally, could I use the same condition: sunrise and condition: sunset variables for determining when the lights turn red instead of determining, for instance, between 7pm and 7am? Both would be very useful.

That’s what I get for getting in a hurry and copy & paste posting when I’m tired…:sleeping:

you didn’t mention that before but, yes, the:

to: 'off'
for:
  minutes: 3

will take care of that.

and it would be necessary to use “homeassistant.turn_on/homeassistant.turn_off” for the group:

trigger:
  platform: state
  entity_id: your_motion_sensor
  to: 'off'
  for:
    minutes: 3
action:
  - service: homeassistant.turn_off
    entity_id: group.bedroom_lights
  - service: whatever other action you want

You would need to use that service for turning on the group as well. Sorry for the confusion.

Hey, I appreciate you helping me out at all! Thank you … I’ll try this out in a bit. I’m still getting my head around all of this.

1 Like

I’ve just started to make the transition from zha to zigbee2mqtt myself.

I have a Nortek HUSZBZ-1 stick that’s been acting up lately so I decided to try out Zigbee2mqtt. After waiting for over a month for the equipment to get here from China I finally got the stick flashed a couple of days ago. Now I need to start switching my lights over to the new stick.

Maybe this thread can help me out in figuring out how to use MQTT to set the brightness of the light for my own use, which is something I hadn’t even started to look into yet.

So thank you for nudging me in that direction. :smiley:

I’ve used MQTT for a while now for just basic things but never needed to look into passing brightness or color settings via MQTT. I suspect it shouldn’t be too hard tho.

and you’re right. that thing is BRIGHT! :laughing:

May a little late :wink: but because of your light changing question, did you check this

Also nice for your doings

1 Like