Simpel script not working | Switch light on when time is between and lightsensor is below

Hi

I have created a script to switch on lights when it is after 07:45 and before 23:15 AND when the light sensor is below 25lx.
Switch light off when it is between 23:15 and 07:45 OR light sensor is above 25.
Below the code I created:

f:
  - condition: time
    after: "07:45:00"
    before: "23:15:00"
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.garage_buiten_sensor_achter_illuminance
        below: 25
        attribute: light_level
then:
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 45
    target:
      device_id:
        - 88c688151db90f2456a6217798c28db3
        - 1febb3c9b5b0088b708b8bad8b6bbf46
  - action: switch.turn_on
    target:
      device_id: d39bdedd4722ea0bdd11186bc9f487dc
    data: {}
else:
  - condition: or
    conditions:
      - condition: time
        after: "23:15:00"
        before: "07:45:00"
      - condition: numeric_state
        entity_id: sensor.garage_buiten_sensor_achter_illuminance
        above: 25
        attribute: light_level
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id:
        - 1febb3c9b5b0088b708b8bad8b6bbf46
        - 88c688151db90f2456a6217798c28db3
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      device_id: d39bdedd4722ea0bdd11186bc9f487dc

When I manually run the script it seems to work but when conditions are met lights do not turn on or off. I have no clue on why this is not working HA.
I had the same script running for many years in Domoticz without any issue.

What am I not doing correct in HA to get this simple script to work? I also tried it as automation but that did not work as the Trigger would turn lights on in the evening, but not in morning as automation was not triggered, also it did not switch lights off.

Thank you for the support.
Bernard

  • A script is a set of actions that are performed when the script is executed.

  • An automation waits for specific events to occur (triggers) and then automatically executes its actions.

So if you made a script, it won’t do anything unless you, or another automation or script, execute it. That’s why your script did nothing until you manually executed it.

You need to create an automation with Numeric State Triggers that detect when light level is above or below 25. It will also need Time Triggers for 7:45 and 23:15.

Thanks for that, I did create an automation like that with a trigger, but that does not work for me, lights go on in the afternoon when light value changes to below 25, but not off and in the morning when the light is still below the trigger value my lights are not turned on. This is what I created:

alias: Woonkamer_aan_schemerschakelaar
description: Spotjes woonkamer voor en achter aan tussen 07:45 en 23:15 als het donker is.
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.garage_buiten_sensor_achter_illuminance
    attribute: light_level
    below: 25
conditions:
  - condition: and
    conditions:
      - condition: time
        after: "07:45:00"
        before: "23:15:00"
actions:
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 45
    target:
      device_id:
        - 88c688151db90f2456a6217798c28db3
        - 1febb3c9b5b0088b708b8bad8b6bbf46
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id: d39bdedd4722ea0bdd11186bc9f487dc
  - if:
      - condition: or
        conditions:
          - condition: time
            after: "23:15:00"
            before: "07:45:00"
          - type: is_illuminance
            condition: device
            device_id: 4ee6b91736bf60636b8bced6b1f70858
            entity_id: fda24d16084c8e69092d1804dce844e4
            domain: sensor
            above: 25
    then:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          device_id:
            - 88c688151db90f2456a6217798c28db3
            - 1febb3c9b5b0088b708b8bad8b6bbf46
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          device_id: d39bdedd4722ea0bdd11186bc9f487dc
mode: single

Any thoughts on improving this automation?

Thanks.

Here are two automations, the first is designed to turn on the light and the second turns it off.

They can be combined into a single automation but I have purposely separated them in order to simplify the triggers and conditions so you can understand the operating principle it relies upon (matching triggers and conditions).

alias: Example turn on
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.garage_buiten_sensor_achter_illuminance
    attribute: light_level
    below: 25
  - trigger: time
    at: "07:45:00"
conditions:
  - condition: time
    after: "07:45:00"
    before: "23:15:00"
  - condition: numeric_state
    entity_id:
      - sensor.garage_buiten_sensor_achter_illuminance
    attribute: light_level
    below: 25
actions:
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 45
    target:
      device_id:
        - 88c688151db90f2456a6217798c28db3
        - 1febb3c9b5b0088b708b8bad8b6bbf46
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id: d39bdedd4722ea0bdd11186bc9f487dc
mode: single
alias: Example turn off
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.garage_buiten_sensor_achter_illuminance
    attribute: light_level
    above: 25
  - trigger: time
    at: "23:15:00"
conditions:
  - condition: or
    conditions:
      - condition: time
        after: "23:15:00" 
        before: "07:45:00"
      - condition: numeric_state
        entity_id:
          - sensor.garage_buiten_sensor_achter_illuminance
        attribute: light_level
        above: 25
actions:
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id:
        - 88c688151db90f2456a6217798c28db3
        - 1febb3c9b5b0088b708b8bad8b6bbf46
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      device_id: d39bdedd4722ea0bdd11186bc9f487dc
mode: single

This is a very common design pattern, typically used for events that occur within a specific time period.

Hi Taras,

Thank you so much! So I need to chop it up in to an on and off automation.
The automation to switch it on I understand. This now will switch on the lights when it is between 07:45 and 23:15 and the light sensor is below 25lx.
The switch of part I do not get. It triggers when light sensor is above 25lx at 23:15 (my assumption is that that will never happen as is dark).
The aim to switch off is during day time when light sensor is above 25lx and between 23:15 and 07:15 the lights are switched off.

When I have it right I should put in a trigger on time switch off at 23:15 and a trigger on the light being above 25lx. Then switch of lights.

I am going to puzzle with that. Thank you for you support.

You don’t have to. I did it to make it easier to understand.

That’s my mistake. I overlooked the fact that in your example it logically ORed the two conditions. I will fix the second example posted above.

EDIT

Actually, the second example probably doesn’t need any conditions. Without conditions, it will simply turn off the light at 23:15 or at any time the light level is above 25.

Thanks I am somewhat of a noob on this HA automation stuff.
It has great potential, but I am not used to how this works in HA yet.

Will check on the conditions for the second example.

Up to the next challenge getting notifications when the water- and gasmeter are not reporting for over 15 minutes, see my topic:
water- & gasmeter alive check