Only affect light if not turned on inside a script

Hi,

I have a script that depending on the light out side switch on the light and sets the brightness to 10% or if it´s brighter outside don´t switch on the light.

However I would like to add that if the light is already on it should leave the light as it is, is there somebody having an idea how to solve it within a script. It should be like IF light is on DO nothing ELSE “and then my existing template”

sequence:
    - service: light.turn_on
      entity_id: light.takbelysning_badrum
      data_template:
        brightness: >
          {% if states.sensor.ljus.state | float < 100 %}
            10
          {% else %}
            0
          {% endif %}

Add this:

- condition: state
  entity_id: light.takbelysning_badrum
  state: 'off'

I never had to use a condition in any of my scripts, but it should work. This will make the script run only if the light is off.

I have not tried it but when I read about as I understood it will stop the script completely, I have other things below this switching on other lights and switches that I would like to be executed.

If you dont mind, can you post the whole script?

Ofcourse :slight_smile:

wakeup_workday:
      sequence:
        - service: light.turn_on
          entity_id: light.takbelysning_badrum
          data_template:
            brightness: >
              {% if states.sensor.ljus.state | float < 100 %}
                10
              {% else %}
                0
              {% endif %}
        - service_template: >
            {% if (states.sensor.ljus.state | float < 1500) and (states.sensor.ljus.state | float >= 100) %}
              light.turn_on
            {% else %}
              light.turn_off
            {% endif %}
          entity_id: light.spegelbelysning_badrum
        - delay: '00:04:00'
        - service: switch.turn_on
          entity_id: switch.info_display_kok
        - service_template: >
            {% if states.sensor.ljus.state | float < 400 %}
              light.turn_on
            {% else %}
              light.turn_off  
            {% endif %}
          entity_id: light.takbelysning_kok
        - service_template: >
            {% if states.sensor.ljus.state | float < 400 %}
              light.turn_on
            {% else %}
              light.turn_off
            {% endif %}
          entity_id: light.takbelysning_tvattstuga

I was afraid you would have delays on your script… OK, here is what you can do…

wakeup_workday:
      sequence:
        - service: automation.trigger
          entity_id: automation.**lightturnonwhatevernameyoulike**

And create an automation that will turn on the light using a condition.
That way, the script will run no matter what, but the extra automation will decide based on the conditions if the light will turn on or not.

1 Like

Great thanks, I will try it