Dark Sky Automation - Trouble w/ "for" command

I am attempting to create an automation that turns on some lights when it becomes overcast outside (only during the day). I am using the Dark Sky component and I am currently using the dark_sky_cloud_coverage sensor for the automation trigger. This attribute can have some odd peaks at times, so I want to only trigger when it has been above 70% for the last three polls (polling every 10 minutes). I have attempted to add a “for” statement in the trigger and in the condition. Both return errors in the log file. Here is the automation:

  - alias: 'Overcast Lights On'
    trigger:
      platform: time
      minutes: '/10'
    condition:
      condition: and
      conditions:
        - condition: numeric_state
          entity_id: sensor.dark_sky_cloud_coverage
          above: 70
          for:
            minutes: 29
        - condition: sun
          after: sunrise
          before: sunset
    action:
      - service: scene.turn_on
        entity_id: scene.fade_in_to_bright
      - service: notify.PushBullet
        data:
          message: 'Overcast lights are now on'

And here is the error I receive:

17-01-06 11:33:15 homeassistant.bootstrap: Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->condition->0->conditions->0->for. (See C:\Users\Ryan\AppData\Roaming\.homeassistant\configuration.yaml:291). Please check the docs at https://home-assistant.io/components/automation/

Can anyone point me in the right direction to resolve this?

1 Like

Can’t use for with numeric states like that. The way around it is to create a template sensor for the state your monitoring and then use the state condition on THAT sensor with the for.

I ran into the same issue with my Washing machine notifications automation and that’s how I got around it.

Of course! I forgot about that. I even voted for the feature request to use for with numeric_state. Completely forgot.

Anyway, thank you for the tip! I’ll start working on the change with the template and post my results…

I sometimes forget it in mid script and then go, “Oh, right!” LOL

I have completely reworked this automation and I am now having a different issue. I am using the new Threshold Binary Sensor to set the Overcast state (‘on’ when cloud_cover is above 69). This appears to be working, however I am getting the notification every 11 minutes. I want this to only trigger ONCE after the ‘Overcast’ sensor has been ‘on’ for more than 11 minutes.

(I am polling the dark_sky sensor every 10 minutes, so I am using 11 minutes to ensure I have values for the last two polls)

Sensor:

binary_sensor:
  - platform: threshold
    name: Overcast
    threshold: 69
    type: upper
    entity_id: sensor.dark_sky_cloud_coverage

Automation:

  - alias: 'Overcast Lights On'
    trigger:
      platform: state
      entity_id: binary_sensor.overcast
      state: 'on'
      for:
        minutes: 11
    condition:
      condition: sun
      after: sunrise
      before: sunset
    action:
      - service: scene.turn_on
        entity_id: scene.fade_in_to_bright
      - service: notify.PushBullet
        data:
          message: 'Overcast lights are now on'

How can I prevent the action from firing every 11 minutes?

You could create an input_boolean and store your activation in there so that it does it only once.

- alias: 'Overcast Lights On'
  trigger:
    platform: state
    entity_id: binary_sensor.overcast
    state: 'on'
    for:
      minutes: 11
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunrise
        before: sunset
      - condition: state
        entity_id: input_boolean.overcast_lights
        state: "off"
  action:
    - service: scene.turn_on
      entity_id: scene.fade_in_to_bright
    - service: notify.PushBullet
      data:
        message: 'Overcast lights are now on'
   - service: input_boolean.turn_on
     entity_id: input_boolean.overcast_lights

This should trigger only once. Make sure you have another automation that turns off that input_boolean somewhere else.

I was wondering about something like this. I guess I was just hoping I was doing something wrong. I’ll give this a shot.

For the last few days I’ve been trying to figure out how to store stuff inside of HomeAssistant. Booleans seem like the best way to store things as long as it’s on/off.

I’m also most likely going to be using MQTT to set temporary values so that I can keep the lights on the same brightness after an event. But booleans are way simpler and in your case work just fine.

I used the recommendation from @cdaoust and it is finally working!! I had a small rain shower move in this morning just before sunrise. Once the sun was up, the action was triggered because it was still more than 70% cloud coverage. And I didn’t receive a notice every 11 minutes after it fired the first time. A few hours later, the clouds cleared and the overcast lights were turned off and I received that notification only once as well.

Finally!!

Thanks again, @cdaoust!

Here’s the final code that got this working for me:

input_boolean:
  overcast_lights:
    initial: off

binary_sensor:
  - platform: threshold
    name: Overcast
    threshold: 69
    type: upper
    entity_id: sensor.dark_sky_cloud_coverage

sensor:
  - platform: darksky
    api_key: [hidden API key]
    monitored_conditions:
      - summary
      - icon
      - precip_type
      - precip_intensity
      - precip_probability
      - temperature
      - apparent_temperature
      - wind_speed
      - wind_bearing
      - cloud_cover
      - humidity
      - pressure
      - visibility
      - ozone
      - daily_summary
      - temperature_max
      - temperature_min
      - precip_intensity_max

automation:
  - alias: 'Overcast Lights On'
    trigger:
      platform: state
      entity_id: binary_sensor.overcast
      state: 'on'
      for:
        minutes: 11
    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunrise
          before: sunset
        - condition: state
          entity_id: input_boolean.overcast_lights
          state: 'off'
    action:
      - service: scene.turn_on
        entity_id: scene.fade_in_to_bright
      - service: notify.PushBullet
        data:
          message: 'Overcast lights are now on'
      - service: input_boolean.turn_on
        entity_id: input_boolean.overcast_lights
  - alias: 'Overcast Lights Off'
    trigger:
      platform: state
      entity_id: binary_sensor.overcast
      state: 'off'
      for:
        minutes: 11
    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunrise
          before: sunset
        - condition: state
          entity_id: input_boolean.overcast_lights
          state: 'on'
    action:
      - service: scene.turn_on
        entity_id: scene.all_off
      - service: notify.PushBullet
        data:
          message: 'Overcast lights are now off'
      - service: input_boolean.turn_off
        entity_id: input_boolean.overcast_lights
2 Likes

Don’t forget to turn input_boolean.overcast_lights off at one point, else it will never trigger again (or until next restart).

Actually now that I think about it… instead of having it store in an input_boolean, you could just check if the light is already on.

condition: state
entity_id: <your light's entity_id>
state: 'off"

It’ll turn on only once and won’t bother you if it’s already on.

1 Like

It does. It’s in the second automation; ‘Overcast Lights Off’.

Now that’s a better idea!

@fenster24 - did you refine this any further?

I’ve implemented this because, as you say, sometimes when it’s overcast you need the lights on. But sometimes, like today, the cloud coverage is 84% but it’s still lovely and bright, so I don’t need the lights on but they came on anyway.

I was thinking to add maybe precipitation probability (or something?) as an extra condition, as today it is 0% (so the clouds aren’t dark, so even though there’s lots of cloud it is light, whereas if the clouds were dark because it was going to rain I would probably need the lights on right now).

Any thoughts?

@fenster24 - just wanted to say thanks for starting this thread - I’m having a play with this as it is exactly what I’d like to achieve.

@anon43302295 - did you manage to add the Rain condition? I’m planning to try that once I have the main bit of this working.

No, I had a fleeting thought about it, posted the above message and then forgot all about it tbh! :smile:

I was looking at sun angles and rain likelihood and various ideas but didn’t come up with anything specific for an algorithm. I’ve been redecorating the house so a lot of my lighting automations are switched off so I’ve not thought about it. I guess I’ll come up with something at some point :slight_smile:

So I seem to be falling at the first hurdle here.

I’m trying to combine the ideas above and to not use the input boolean. so my yaml looks like this:

sensor:
  - platform: darksky
    api_key: [hidden]
    monitored_conditions:
      - summary
      - icon
      - cloud_cover
      - wind_speed
      - apparent_temperature

binary_sensor:
  - platform: threshold
    name: Overcast
    threshold: 79
    type: upper
    entity_id: sensor.dark_sky_cloud_coverage

automation old:
  - alias: 'Overcast Lights On'
    trigger:
      - platform: state
        entity_id: binary_sensor.overcast
        state: 'on'
        for:
          minutes: 2
    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunrise
          before: sunset
        - condition: state
          entity_id: light.living_room
          state: 'off'
        - condition: or
          conditions:
          - condition: state
            entity_id: device_tracker.Bob
            state: home
          - condition: state
            entity_id: device_tracker.Alice
            state: home
    action:
      - service: scene.turn_on
        entity_id: scene.livingroom_fadeon
      - service: notify.PushBullet
        data:
          message: "It looks rather cloudy so I've put some lights on"

I’m pretty sure the scene is okay as I use it other automations and I use the sun conditions elsewhere too. I can see the light.living_room is showing as off and I can see the binary sensor is tracking the cloud condition. However the lights do not come on.

I’ve tried removing the presence tracking conditions to test and that doesn’t seem to help.

I’m sure I am missing something obvious and would appreciate any pointers.

Cheers!

Dunno if tired eyes, but I can’t see anything wrong there.

The automation I’m using (still pending refinement as above, obviously!) which is working…

alias: 'Overcast Lights On'
trigger:
  platform: state
  entity_id: binary_sensor.overcast
  to: 'on'
  for:
    minutes: 11
condition:
  condition: and
  conditions:
    - condition: sun
      after: sunrise
      before: sunset
    - condition: state
      entity_id: light.living_room_zwave_dimmer_level
      state: 'off'
    - condition: state
      entity_id: group.all_devices
      state: 'home'
action:
  - service: scene.turn_on
    entity_id: scene.livingroom_normal

Thanks.

I’ll have another look tomorrow. Very tired today and am sure I am missing g something obvious!

I found the could_cover sensor to be too variable for me. Maybe it’s just where I live, but it would jump from 30% to 90% and back down to 20% in a short amount of time quite regularly. So, I ditched the idea after my wife started complaining about the lights going on and off throughout the day.

So instead, I built Ben’s $15 multisensor and then wrote some automations around the light-level sensor. I have to say I’m much happier with the results. Basically it turns the lights on when there is motion in the room and the light level is below a certain point. Here’s what I’ve done:

binary_sensor:
  - platform: threshold
    name: Man Cave Light Level
    threshold: 250
    type: lower
    entity_id: sensor.multisensor_light_level

automation:
  - alias: 'Man Cave Lamp Motion'
    trigger:
      platform: state
      entity_id: sensor.multisensor_motion
      to: 'motion detected'
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: light.man_cave_lamp
          state: 'off'
        - condition: state
          entity_id: binary_sensor.man_cave_light_level
          state: 'on'
    action:
      service: scene.turn_on
      entity_id: scene.man_cave_on

  - alias: 'Man Cave Lamp No Motion'
    trigger:
      platform: state
      entity_id: sensor.multisensor_motion
      to: 'standby'
      for:
        minutes: 30
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: light.man_cave_lamp
          state: 'on'
        - condition: state
          entity_id: sun.sun
          state: 'above_horizon'
    action:
      service: scene.turn_on
      entity_id: scene.man_cave_off
1 Like