Light automation turn_on depending on weather

I currently have the following automation set up:

- alias: Couch Lamp On
  trigger:
  - platform: state
    entity_id: sun.sun
    to: below_horizon
    offset: "-00:20:00"
  condition:
  - condition: state
    entity_id: light.saloni
    state: 'off'
  action:
  - service: light.turn_on
    data:
      entity_id: light.saloni
      transition: 600
      hs_color:
      - 180
      - 61
      brightness_pct: 60

wanted to add the condition that if DarkSky says it’s cloudy/partlycloudy/rainy that the light turns on e.g. half an hour earlier

Is there a way to do that in one automation do I need to set multiples ones. I assume some sort of data_template would work but I never worked with them and have no idea how to write one.

Instead of putting all this conditions in an automation i’m using a binary_sensor for this.

binary_sensor:
  - platform: template
    sensors:
      dark_outside:
        value_template: >
          {% if (states.sun.sun.attributes.elevation | int < 0) %}
            true
          {% elif ( (states.sun.sun.attributes.elevation | int < 1) and (states.sensor.dark_sky_cloud_coverage.state | int > 80)) %}
            true
          {% elif ( (states.sun.sun.attributes.elevation | int < 2) and (states.sensor.dark_sky_cloud_coverage.state | int > 90)) %}
            true
          {% else %}
            false
          {% endif %}

The idea came from @anon43302295 a while ago.
Sadly, i can’t link you to his HA repo on github, because he removed it. :+1:

2 Likes

The automation you posted will not work as-is because the state trigger does not have an offset parameter. What you want to use is the sun trigger instead. (See sun trigger and trigger variable for sun trigger.)

One way to do what you want would be to specify two sun triggers: one that triggers 20 minutes before sunset, and another that triggers 50 minutes before sunset. Then you add a condition that checks the offset and DarkSky condition: if the offset was -20 minutes, or if the DarkSky condition is one of the ones you specified, then allow the action to run.

Before I can suggest a complete solution, you’ll have to tell me how you have DarkSky configured, since there are two different ways. Specifically, what is the entity_id of the DarkSky entity that shows the current condition, and is the condition shown as the state, or in an attribute, and if so, which one?

In the meantime, here’s a partial solution:

- alias: Couch Lamp On
  trigger:
  - platform: sun
    event: sunset
    offset: "-00:50:00"
  - platform: sun
    event: sunset
    offset: "-00:20:00"
  condition:
  - condition: template
    value_template: >
      {{ trigger.offset.total_seconds()/60 == -20 or
         states('sensor.darksky') in ('cloudy', 'partlycloudy', 'rainy') }}
  - condition: state
    entity_id: light.saloni
    state: 'off'
  action:
  - service: light.turn_on
    data:
      entity_id: light.saloni
      transition: 600
      hs_color:
      - 180
      - 61
      brightness_pct: 60

@pnbruckner thanks for trying to work that out for me. I’ve got DarkSky configured as a weather platform rather than a sensor and use a custom animated weather card to display the info. So there is only one entity_id: weather.dark_sky and all the info as attributes.

Also, the condition attribute is used as the state for the entity_id

And here is the JSON:

{
  "temperature": 20.5,
  "humidity": 100,
  "ozone": 287.7,
  "pressure": 1010.08,
  "wind_bearing": 207,
  "wind_speed": 9.22,
  "visibility": 10.117,
  "attribution": "Powered by Dark Sky",
  "forecast": [
    {
      "datetime": "2019-10-02T00:00:00",
      "temperature": 28.4,
      "templow": 17.4,
      "precipitation": 0.1,
      "wind_speed": 5.45,
      "wind_bearing": 192,
      "condition": "partlycloudy"
    },
    {
      "datetime": "2019-10-03T00:00:00",
      "temperature": 27.2,
      "templow": 17.9,
      "precipitation": null,
      "wind_speed": 9.17,
      "wind_bearing": 200,
      "condition": "sunny"
    },
    {
      "datetime": "2019-10-04T00:00:00",
      "temperature": 26.3,
      "templow": 15,
      "precipitation": 0.8,
      "wind_speed": 11.81,
      "wind_bearing": 186,
      "condition": "partlycloudy"
    },
    {
      "datetime": "2019-10-05T00:00:00",
      "temperature": 24.8,
      "templow": 15.3,
      "precipitation": 2.6,
      "wind_speed": 9.22,
      "wind_bearing": 277,
      "condition": "rainy"
    },
    {
      "datetime": "2019-10-06T00:00:00",
      "temperature": 24.6,
      "templow": 14.4,
      "precipitation": 0.1,
      "wind_speed": 9.77,
      "wind_bearing": 304,
      "condition": "sunny"
    },
    {
      "datetime": "2019-10-07T00:00:00",
      "temperature": 23.3,
      "templow": 14.1,
      "precipitation": 4.1,
      "wind_speed": 6.58,
      "wind_bearing": 60,
      "condition": "rainy"
    },
    {
      "datetime": "2019-10-08T00:00:00",
      "temperature": 19.5,
      "templow": 14.9,
      "precipitation": 17.4,
      "wind_speed": 9.14,
      "wind_bearing": 95,
      "condition": "rainy"
    },
    {
      "datetime": "2019-10-09T00:00:00",
      "temperature": 19,
      "templow": 15,
      "precipitation": 16.1,
      "wind_speed": 9.07,
      "wind_bearing": 78,
      "condition": "rainy"
    }
  ],
  "friendly_name": "Dark Sky"
}

I just don’t understand how to write templates like that myself

Ok, let’s assume the first entry in the forecast list is always for today. Then that would translate to:

- alias: Couch Lamp On
  trigger:
  - platform: sun
    event: sunset
    offset: "-00:50:00"
  - platform: sun
    event: sunset
    offset: "-00:20:00"
  condition:
  - condition: template
    value_template: >
      {{ trigger.offset.total_seconds()/60 == -20 or
         state_attr('weather.dark_sky', 'forecast')[0].condition
         in ('cloudy', 'partlycloudy', 'rainy') }}
  - condition: state
    entity_id: light.saloni
    state: 'off'
  action:
  - service: light.turn_on
    data:
      entity_id: light.saloni
      transition: 600
      hs_color:
      - 180
      - 61
      brightness_pct: 60

When one of the two triggers fires, it will create a variable named trigger that can be used in the condition and action parts of the automation. For a sun trigger it will contain a field named offset that will contain the value of the offset option of the trigger that fired. It will be a Python datetime.timedelta object. I used the total_seconds() method of the timedelta object to get the equivalent number of seconds for that offset and divide it by 60 to get minutes. If the value is -20 then the trigger that fired is the one for 20 minutes before sunset. In that case the condition will be true (because at 20 minutes before sunset we don’t care what the forecast is) so the action will run. If it’s not -20, then it must be the other trigger fired, the one for 50 minutes before sunset. In that case we only want the action to run if the forecast is cloudy, partlycloudy or rainy. So we use the state_attr() function to get the value of the forecast attribute of the weather.dark_sky entity. This is a list, where each element of the list is a Python dict that contains the key condition whose value is the forecast condition for that day. So we take the first element ([0]) of the list and reference the condition field and check if its value is one of the values we want. If it is the action will run. If it’s not, it won’t.

The only slight downside to this exact implementation is that if the light is turned on 50 minutes before sunset, then it will be turned on again at 20 minutes before sunset if it had been turned off during that 30 minute window. I suspect that’s not an issue, but just thought I’d mention it.

I’m confused by how the thread has progressed…

I thought that the OP wanted to always trigger the action but they wanted the sun offset value to be different (50 minutes vs 20 minutes) depending on if it’s dark outside as reported by the darksky sensor?

I may not be following the latest post explanation correctly but this -

doesn’t seem like it matches this -

??

Yes, you’re confused. :laughing:

But seriously, the way I understood it, he wants the light to be turned on 50 minutes before sunset if it’s dark (i.e., the current weather condition is one of the ones listed), or 20 minutes before sunset regardless of conditions (but in either case, only if the light is currently off.) That’s what the automation does (or, at least, that’s what it’s intended to do.)

I think maybe you missed this:

“If the [sunset offset] value is -20 then the trigger that fired is the one for 20 minutes before sunset. In that case the condition will be true (because at 20 minutes before sunset we don’t care what the forecast is) so the action will run. If it’s not -20, then …”

1 Like

Yeah that is basically what I wanted more or less. I didn’t have any offset in general at first but noticed that it wouldn’t hurt to turn on a bit earlier. And then I can adjust the timing of the offset if need to.

Thanks for taking the time to write the automation. Will set it up now and see if it works tomorrow evening :blush:

1 Like

you’re right. I was.

Just read through it again and now I see that it’s actually triggering twice. the first time at t-50 minutes. And then checking to see if it’s “dark”, if so turn on the light. if not then wait till the next trigger and check to see if that one was the t-20 minutes (or if it’s still dark but that one doesn’t matter) if so turn on the light.

Got it.

I got lost at the “the action won’t run” part but now I see that was an explanation of the t-50 minute condition and not the whole automation.

I think I just needed some sleep.

2 Likes

I do have to say it’s not the best solution but it’s the only way to get it to work right now.

At some point I need a light detector so it turns on below a set brightness (more accurate)

and since I’ve also set to to dim down to 5% after midnight as a night light a pir sensor would also be great to brighten it in case there is movement at night

Agreed, a light sensor is best. FWIW, I used a custom integration for quite a while that estimates outdoor light based on weather conditions and time of day until I got a light sensor. If you’re at all interested:

It still supports Weather Underground, but now also supports YR & Dark Sky.

1 Like