Automation yeelight color

Hi, I would like a yeelight bulb to shine in red starting at 9:00 p.m. and to go blank again after 8 p.m. What I need is for the light bulb to appear in one or the other color depending on the schedule, that is, if I get up at night and turn on the light, the color will be red, but during the day when the light is turned on, the color will be white. I have this automation but it does not work for me. I would appreciate help.

  • alias: ‘do not disturb ON’
    initial_state: ‘on’
    • platform: time
      at: ‘21:12:00’
    • service: light.toggle
      entity_id: light.yeelight_rgb_7811dc680fd4
      rgb_color: [ 255, 0, 0 ]
      brightness: 50
      transition: 2
1 Like

A few comments…

First, please format you YAML correctly so we can read it properly. Follow the instructions at the top of the page.

Next, did you mean 8 a.m.?

Next, the light.toggle service does not accept color or brightness. You have to use the light.turn_on service for that.

Next, I don’t have any yeelights, so not sure about their exact behavior, but I think most dimmable/color lights, when turned on (either from HA without specifying color or brightness, or when turned on manually) turn on to whatever color/brightness they were the last time they were on. So, generally speaking, you need to turn the light on with the color/brightness you want, and then turn it back off (at least, if it was already off when the automation runs.)

So, putting that all together, you might want something like this:

- alias: Do not disturb on
    - platform: time
      at: '21:00:00'
    - service_template: >
        {% if is_state('light.yeelight_rgb_7811dc680fd4', 'on') %}
        {% else %}
        {% endif %}
      entity_id: input_boolean.prev_light_state
    - service: light.turn_on
        entity_id: light.yeelight_rgb_7811dc680fd4
        rgb_color: [255, 0, 0]
        brightness: 50
        transition: 2
    - condition: state
      entity_id: input_boolean.prev_light_state
      state: 'off'
    - service: light.turn_off
      entity_id: light.yeelight_rgb_7811dc680fd4

The “Do not disturb off” at 8 a.m. automation would be similar, of course changing the trigger time and the light color/brightness accordingly.

I can’t exactly test this, but hopefully it gives you a possible starting point.

1 Like

Hi, thanks for your help . I have pasted your code in my automation.yaml file and restarted HA. But nothing happens. The color is still white and does not change even though the schedule tells you that starting at 9pm you have to turn it on red.
Thank you.

Well, it’s not a schedule, it’s an automation, which happens at exactly 9 PM. I’m not sure where you are in the world, but if it did not become 9 PM after you did that, then it hasn’t had a chance to run yet. You can improve the automation to run when HA starts and change the color at that time if it’s the correct time period, but you didn’t say you wanted that. Well, I guess, in a way you did. I’ll work on providing you a single automation that takes care of both times (9 PM and 8 AM), as well as when HA starts. Give me a few minutes…

Actually, before I go through that effort, can you first confirm that running those two services (light.turn_on with those data values, then light.turn_off) changes the light so that when it’s turned on afterwards, it does come on red and with the desired brightness?

Hi, I’m from Spain . The problem is that automation does not start. That is, here it is now 0.23 am and when I activate the switch it should turn on red. But it lights up in white or in the last used color.

Yes, that’s because it’s not a schedule, it’s an automation. An automation is something that runs based on a given event. In this case, the time becoming 9 PM. That has not happened yet since you added this automation. The next time it becomes 9 PM, the actions of the automation will run, which will turn on the light to red w/ a brightness of 50 (or 19.6%, since brightness goes from 0 to 255), and then, if the light was off when the automation started running, it will turn the light back off. In theory this should change the light’s internal settings so the next time after that that you turn it on, it will turn on red @ 19.6%.

Before we go any further, can you verify that if you run those services (for example, from the Services page, or at least from the HA frontend’s control for the light) that the light indeed changes in the way I described above (that is, that the next time you turn it on it comes on red at 19.6%)?

I have tried from the frontend and there is no change in color. The bulb turns on but in the color used the last time

I think we’re failing to understand each other. And since I don’t have one of these lights and know its unique characteristics, I think it’s best if someone else try to help you. Sorry I couldn’t solve your problem. If someone doesn’t jump in within the next day or so, I’d suggest starting a new topic. Since this one already has several replies, people might skip over it.

I found it, your script is perfect, what happened is that I had a syntax error. It is already fixed, now it works perfectly. Many thanks . You’re a phenomenon .

Just one last detail. If the light is off, the automation turns it on and changes its color depending on the time. How could I make automation not turn on the light?

You don’t want an automation you want a script attached to an input_Boolean to give you a normal on button and then use the time as a condition not a trigger.

That’s Bobby, is there a script or plugin that does that? I’ve been looking at the forum and I have not found anything. I guess that’s for a much more advanced level than mine.

Thinking about it some more, it’s probably simplest to do two automations, both triggered by the ‘on’ state of the same input_boolean, basically just a switch that doesn’t have any hardware attached to it. Then have the 9pm - 8am as a condition with the action being the settings you want at that particular time and another with 8am - 9pm and the light settings you want for that.

When you’re happy you could look at combining them using if/else statements but I find it easier to start simple and then you also learn how it all works as you develop it.