Condition problems with automation

I’ve got an automation to turn my lamps on when the cloud coverage reported from dark sky goes above 49% and it’s before sunset or after sunrise. For some reason this automation is never triggered when the sensor goes above 49%. I suspect the problem stems from the conditions. There are no related errors in the log. Any ideas? The automation looks like this:

- id: Lamps_on_when_cloudy
  alias: Lamps on when its cloudy
  initial_state: True
  hide_entity: False
  trigger:
    - platform: numeric_state
      above: 49
      entity_id: sensor.dark_sky_cloud_cover
  action:
    service: homeassistant.turn_on
    entity_id: group.all_lamps
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunrise
      - condition: sun
        before: sunset

The only thing I can see is that I would put a ‘-’ before the above so … - service: homeassistant.turn_on

I would use the following condition:

  condition:
    - condition: state
      entity_id: sun.sun
      state: 'above_horizon'

The action itself works fine, I can trigger it manually without an issue. AFAIK the ‘-’ is only necessary with lists.

Thanks for the suggestion, what’s the thinking behind this?

If I’m not mistaken, triggers will only happen the first time that the value crosses the threshold amount. Are you seeing the cloud cover go from below 49 to above? Or is it already above 49 when you start HA? Have you tried the code without the conditions to rule out an issue there?

I use Dark Sky, combined with some other elements, to create a binary sensor that answers the question “Is it dark enough that I should probably turn on a light?” Then I use that sensor as a trigger for my automation.

sensor:
  - platform: template
    sensors:
      turn_on_indoor_lights:
        friendly_name: 'Turn On Indoor Lights'
        value_template: >
          {% if (states.sun.sun.attributes.elevation | int < 30) %}true
          {% elif ( (states.sun.sun.attributes.elevation | int < 40) and (states.sensor.dark_sky_cloud_coverage.state | int > 50)) %}true
          {% elif (states.sensor.dark_sky_cloud_coverage.state | int > 90) %}true
          {% else %}false
          {% endif %}

and the automation:

- alias: Turn on indoor lights based on darkness
  trigger:
    - platform: state
      entity_id: sensor.turn_on_indoor_lights
      to: 'true'
      from: 'false'
  action:
    - service: scene.turn_on
      entity_id: scene.indoor_auto_on
    - service: notify.scott_notifier
      data_template: 
        message: "Auto indoor lights. Sun angle is {{states.sensor.solar_angle.state}}."

- alias: Turn off indoor lights based on darkness
  trigger:
    - platform: state
      entity_id: sensor.turn_on_indoor_lights
      to: 'false'
      from: 'true'
  action: 
    service: scene.turn_on
    entity_id: scene.indoor_auto_off
1 Like

Thanks for the reply! Yes, this morning the cloud coverage sensor was in the ~20s, it went up to the ~70s as the day went on without the automation being triggered. I do think the issue has to do with the conditions, but I’m not sure how those simple conditions could be causing this problem.

I like the way you’ve set this up using a sensor, I might try that if I can’t get my current method to work.

I’ve a similar setup, try this:

- id: Lamps_on_when_cloudy
  alias: Lamps on when its cloudy
  initial_state: 'on'
  trigger:
    platform: numeric_state
    above: 49
    entity_id: sensor.dark_sky_cloud_cover
  condition:
    condition: state
    entity_id: sun.sun
    state: 'above_horizon'
  action:
    - service: homeassistant.turn_on
      entity_id: group.all_lamps

and then test it by manually overwriting the sensor value

1 Like

I will try that, thank you!

Do you think the issue was the multiple conditions?

I’m now realizing that this is why @arsaboo suggested something similar earlier.

No, I sometimes use multiple conditions as well, and the ones you used look okay. Another problem could be the value extraction from the sensor. Check this to use a value_template.

On a second look, am I wrong or does this automation trigger on every state change above 49, even if the lights are already on, in this case either add another condition or use a sensor setup similar @ih8gates.

Yes, this does not check to see if the lights are already on, if that’s what you mean. And I don’t know, I assume this just triggers as soon as the sensor value goes above 49.

Also, I’m not sure how to overwrite a sensor value. Would this be a “customization”?

To overwrite, go to the developer tools and then states tab

Then select your entity

Scroll up again and change and set the state

Thanks. After setting it to a value below 49 it then updated to the current actual value pretty quickly afterward, which is 71. The automation did not trigger. So maybe it is the value extraction problem.

Try this:

trigger:
  platform: template
  value_template: '{{ states.sensor.dark_sky_cloud_cover.state  >= 49 }}'

Will this work with “above:” if it’s not a numeric_state?

Edit, I see that you edited your comment, hold on.

Ok, it’s not a value extraction problem or a condition problem.

It was a very simple mistake. The sensor is actually:

sensor.dark_sky_cloud_coverage

I was using:

sensor.dark_sky_cloud_cover

Sorry about that, I really appreciate all of the help with diagnosing this problem in this thread.