Light on with conditions

Hello all, I’m still learning this.

I have a light called “lamp”. I’ve managed to get it to turn on 1 hour before sunset - great. Turning off not an issue - Alexa does this whenever we choose it’s bed time.

I’m stuck with how to get it to turn on if it’s cloudy after sun-rise, and before the above sunset setting kicks in. I’ve tried the “cloudiness” setting of yr - but nowhere can I find what the values are - is 0 sunny, 1 cloudy, or a percentage or ??

If someone could give me an example of what to put in which yaml I’d be very grateful.

Even better would be there’s 2 of us with phones - peters_iphone and janes_iphone, If the cloudy condition only kicks in when we’re at home perfect. I have the house/phone location bits working.

To clarify, always come on 1 hour before sunset. If it gets cloudy during the day (and either of us are at home) also come on. Don’t come on if it’s cloudy at night or any time that both of are phones are out. If it stops being cloudy during the day, the lamp should turn off.

I could do this quite easily in C, BASIC, or even COBOL, but yaml takes some getting used to.

Many thanks, Peter.

Group the entities that define if you’re at home, then you can use the state of the group being 'home' to tell if anybody is home.

For night vs day, you can use the state of sun.sun. If it’s 'above_horizon' then it’s daytime, and 'below_horizon' for night.

I don’t have the YR sensor, but looking at the docs show that there’s a low/medium/high clouds sensor. The cloudiness sensor will have an attribute telling you the units if you look at it in the dev-states menu. For this I’ll assume percentage for cloudiness, that the cloudiness sensor is sensor.yr_cloudiness, and that you’ve grouped the trackers in group.my_people:

automation:
  - alias: 'Turn on lamp'
    trigger:
      - platform: numeric_state
        entity_id: sensor.yr_cloudiness
        above: 20
        for:
          minutes: 10
      - platform: state
        entity_id: group.my_people
        to: 'home'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
      - condition: state
        entity_id: group.my_people
        state: 'home'
      - condition: numeric_state
        event: sensor.yr_cloudiness
        above: 20
    action:
      - service: light.turn_on
        data:
          entity_id: light.lamp

  - alias: 'Turn off lamp'
    trigger:
      - platform: numeric_state
        entity_id: sensor.yr_cloudiness
        below: 20
        for:
          minutes: 10
      - platform: state
        entity_id: group.my_people
        to: 'not_home'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
      - condition: state
        entity_id: group.my_people
        state: 'not_home'
      - condition: numeric_state
        event: sensor.yr_cloudiness
        below: 20
    action:
      - service: light.turn_off
        data:
          entity_id: light.lamp

I’ll also add my usual comment that using the sun elevation gives you a more consistent light level through the year than a fixed time offset.

1 Like

Thank-you so much for your reply. I have two simple questions:
1, Do I need the condition rule if it is part of the trigger ?
2, You say 20 and 80 for the yr.cloudiness value. I clicked on the yr symbol on my home page just now, it ranges from 1-4 for today. Could it be 20% is 1 and 4 is 80%, thus 0 is 0% and 5 is 100%. 0% being no cloud cover at all.

Thanks again.

  1. No, though with multiple triggers, you’re probably going to want matching condition checks
  2. I did say For this I’ll assume percentage for cloudiness, that the cloudiness sensor is sensor.yr_cloudiness - since I don’t have that sensor :wink: Just adjust the numbers to suit you

Thanks Tinkerer,

All up and running now. I now just to need to decide if yr is accurate and up to date enough. I feel it might not be. Time will tell.

I bet someone here will suggest a better solution (if you have a suggestion the wife has told me I’ve spent enough on light bulbs, light switched, Broadlink, smart plugs and more) that doesn’t cost.

Oh, I’m in England if it makes a difference.

Cheers, Peter.

Also UK based, and I tend to find that the Met Office and DarkSky sensors are the most accurate overall. I’ve also played with OpenWeatherMap, which is pretty close to those two.

Using this automation i get the following error in configuration:

Invalid config for [automation]: [event] is an invalid option for [automation]. Check: automation->trigger->0->event. (See /home/homeassistant/.homeassistant/configuration.yaml, line 29). Please check the docs at https://home-assistant.io/components/automation/ Invalid config for [automation]: [event] is an invalid option for [automation]. Check: automation->trigger->0->event. (See /home/homeassistant/.homeassistant/configuration.yaml, line 29). Please check the docs at https://home-assistant.io/components/automation/

Maybe, using numeric_state as trigger i must use entity_id and not event?
And if i want to use this automation only from and to some hours of the day, i must use another “time” condition?

I had a similar automation but found it wildly inaccurate so went with a light sensor method. You can get a cheap one and then just configure that.

Currently I use a Xiaomi Gateway for this (and many other things), which has a built-in light sensor which I find much better than trying to guess what 3rd party weather sensors say about clouds.

Me too have xiaomi Gateway, but i have this in a poor light condition so it’s really not reliable as sensor…
I’d like to try the cloudness sensor of darksky but i get that error.

Yeah, not sure where event came from

On conditions, you should use entity_id on numeric_state:

    condition:
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
      - condition: state
        entity_id: group.my_people
        state: 'home'
      - condition: numeric_state
        entity_id: sensor.yr_cloudiness
        above: 20