Check dark condition in motion sensor

Hello community, I migrated all my Xiaomi sensors on conbee/raspbee, and now I have to move every automations from Xiaomi GW to HA. One basic automation I have to export is:
Trigger: detected motion
Condition: Dark = true
Action: Turn on light

Actually I tried with the Lux value (need to test it anyway), but I prefer to use Dark True/false,

- id: porta_aperta_accendi_ingresso
  alias: Porta aperta accendi Ingresso
  trigger:
  - entity_id: binary_sensor.presence_20
    platform: state  
    from: 'off'
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sensor.lightlevel_19
    below: 400  
  action:
  - service: light.turn_off
    entity_id: light.ingres1, light.ingres2

How can I change my condition using Dark state?
Thank you

Create a template binary sensor:

binary_sensor:
- platform: template
  sensors:
    dark_outside:
      entity_id: sensor.lightlevel_19
      friendly_name: "Dark Outside"
      value_template: "{{ states('sensor.lightlevel_19')|int < 400 }}"

Then you can use the following condition in your automations:

  condition:
  - condition: state
    entity_id: binary_sensor.dark_outside
    state: 'on'

Wait… the dark state is native from the sensor, I don’t need to create a template.

I only need to understand how to put the Dask state into a condition :wink:
So for example, if dark = true then action…

Oh ok, sorry, missed that. You have two choices.

  1. Use a template condition:
  condition:
  - condition: template
    value_template: "{{ state_attr('sensor.lightlevel_19', 'dark') }}"
  1. Use a binary template sensor and a state condition:
binary_sensor:
- platform: template
  sensors:
    dark_outside:
      entity_id: sensor.lightlevel_19
      friendly_name: "Dark Outside"
      value_template: "{{ state_attr('sensor.lightlevel_19', 'dark') }}"

Condition remains the same as previous:

  condition:
  - condition: state
    entity_id: binary_sensor.dark_outside
    state: 'on'

The advantage of the 2nd option is a simpler condition and that the template only gets evaluated once (not in every automation you use it in) and you can add it to the front end as a Lovelace entity to see what is going on. None of these are huge advantages. There’s a custom card to display attributes and as the template is in a condition it is only evaluated when the automation runs. Up to you which you use.

Thank you very much Tom, I believe I’ll go with the 2nd option, btw, there is something is not clear with the 1st one:

  condition:
  - condition: template
    value_template: "{{ state_attr('sensor.lightlevel_19', 'dark') }}"

how can this condition distinguish between true and false? :thinking:

Because the attribute it is looking for is dark. Is it dark?

Yes = True
No = false

Conveniently the attribute ‘dark’ contains the string true or false so that’s all we need.

The template returns the contents of the ‘dark’ attribute. So if dark contains ‘true’ the template evaluates to:

value_template: "{{ 'true' }}" 

So, the above code means:
IF DARK = TRUE THEN ACTION

So this is the TRUE condition, and what about if I have to check the FALSE one?
I mean if in another condition I have something like:
IF DARK = FALSE THEN ACTION

not state_attr…

1 Like

Thank you all, now is all clear :slight_smile:

Does anyone know what light level is considered dark in this instance?