Set lux condition depending on the wether outside (condition template)

Hi Forum,
How do I formulate a condition template that sets the condition threshold depending on the weather outside.
I have an “sensor.yr_symbol” which have different values depending on the weather.

What I want is that:

If the “sensor.yr_symbol” has the value of either “1” or “2” it should set the Lux threshold from my “sensor.office_lux” to be e.g. <= 20000

And if the “sensor.yr_symbol” is not neither “1” or “2” it should set the threshold to <= 15000.

How do I create a condition template taking care of this?

Thank you in advance.

//Rasmus

What exactly is the threshold and how do you use it?
it looks like you’re trying to create a template sensor? (because you can’t “really” assign a value to a sensor)
if so its value template would look like this:

- platform: template
  sensors:
    office_lux:
      value_template: >
        {% if states.sensor.yr_symbol.state == "1" or states.sensor.yr_symbol.state == "2"%}
        20000
        {% else %}
        15000
        {% enfif %}

Oh wait.

By threshold I mean the value of the lux where the condition should set a true. When the symbols are either 1 or 2 the least lux level should be higher in order to give the true.

I would like to add this to multiple automations and be able to set individual threshold for the least lux value to set true. I could make a rough though of my idea:

- alias: 'turn on the light faster when it is raining'
  trigger:
    platform: state
    entity_id: sensor.office_motion_sensor
    to: 'on'
  condition:
    condition: template
    value_template: >-
      {% if states.sensor.yr_symbol.state == "1" or states.sensor.yr_symbol.state == "2" %}
        {% if ( states.sensor.office_lux.state | int ) < 18000 %}
          true
        {% endif %}
      {% else %}
        {% if ( states.sensor.office_lux.state | int ) < 14000 %}
          true
        {% endif %}
      {% endif %}
  action:
    - service: light.turn_on
      entity_id: light.officelamp

I know this can be done with multiple automations but I’m seeking for a solution where I could minimize the amount of automations - by condition templates like the above. :slight_smile:

try this:

- alias: 'turn on the light faster when it is raining'
  trigger:
    platform: state
    entity_id: sensor.office_motion_sensor
    to: 'on'
  condition:
    condition: template
    value_template: >-
      {% if (states.sensor.yr_symbol.state == "1" or states.sensor.yr_symbol.state == "2") and (states.sensor.office_lux.state | int ) < 18000 %}
      true
      {% elif ( states.sensor.office_lux.state | int ) < 14000 %}
      true
      {% endif %}
  action:
    - service: light.turn_on
      entity_id: light.officelamp

not tested but think it should work

1 Like

can shorten that to:

states.sensor.yr_symbol.state | int in range(1,3)

or

states.sensor.yr_symbol.state in ['1','2']
2 Likes

Thank you guys. I’ve tested the automation and it works - nice!

If someone for some reason is searching for the same solution I will post the full automation below:

- alias: 'turn on the light faster when it is not sunny'
  trigger:
    platform: state
    entity_id: sensor.office_motion_sensor
    to: 'on'
  condition:
    condition: template
    value_template: >-
      {% if (states.sensor.yr_symbol.state in ['1','2']) and (states.sensor.office_lux.state | int ) < 16000 %} ## 1+2 are conditions for sunny and slightly cloudy
        true
      {% elif ( states.sensor.office_lux.state | int ) < 22000 %} ## The threshold of lux when it's not sunny
        true
      {% endif %}
  action:
    - service: light.turn_on
      entity_id: light.officelamp
1 Like

Unfortunately it’s not working… Now the YR sensor has the value of two and my automation is not being triggered at all… Hm…

just to confirm, the automation does not trigger when the YR sensor changed its value (to 2 or other), it only triggers when sensor.office_motion_sensor goes from anything other than on to on

Also you now say the YR sensor has the value of two, so that’s “two” and not “2” anymore?