Help with coding

Hi guys. I am trying to turn on the lights in my house when I trigger a motion detector (PIR) at entry.

I only want this PIR to turn on all lights if I have been away for minimum 5 minutes (Status: “not_home”).

I tried this code, but it did not work.

- id: lenge_nok_borte
  alias: Lenge nok borte PIR gang
  trigger:
  - platform: state
    entity_id: group.tracker_group_borge
    to: not_home
    for:
      minutes: 5
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.borte_lenge_nok

- id: sla_paa_lys_pir_gang
  alias: Lys Gang på via PIR
  trigger:
    platform: state
    entity_id: input_boolean.borte_lenge_nok
    to: 'on'
  condition:
    - condition: state
      entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_sensor_2
      state: 'on'
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.borte_lenge_nok
  - service: scene.turn_on
    entity_id: scene.stuekjokkengang_medium

What did I mess up? :slight_smile:

So, what’s the point of the input_boolean? It seems like you are just turning that on and off for the second automation to fire. But to me, it seems like this can be simplified into a single automation. Is there a reason for having 2 automations?

This is what a single automation would look like:

- id: lenge_nok_borte
  alias: Lenge nok borte PIR gang
  trigger:
  - platform: state
    entity_id: group.tracker_group_borge
    to: not_home
    for:
      minutes: 5
  condition:
    - condition: state
      entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_sensor_2
      state: 'on'
  action:
  - service: scene.turn_on
    entity_id: scene.stuekjokkengang_medium

The input_boolean must be there, I think. Since when I enter my home zone it will say status “home”, and not “not_home”, so it would not work.

I’m not following you. I don’t see how home and not_home have anything to do with the input boolean. All the boolean was doing in your automation is turning on then turning off.

I am sure you are right. My point is that with your code I will not be able to trigger the PIR, since when I am back at my home again my gps status will say “home” - and therefor it will not turn on the lights.

I was just thinking I could use a input boolean to make something tell the system that I have been away and was not returning - so now it should activate the light when I trigger the PIR.

I am sorry that my enligsh is not very good.

Hi,

you should change the trigger in the second automation.
This should be the PIR, not the status.
As you said, the scene should be triggered when the PIR get motion, under the condition, you are away for > 5 minutes:

  alias: Lenge nok borte PIR gang
  initial_state: true
  trigger:
  - platform: state
    entity_id: group.tracker_group_borge
    to: not_home
    for: 00:05
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.borte_lenge_nok

- id: sla_paa_lys_pir_gang
  alias: Lys Gang på via PIR
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_sensor_2
    to: 'on'
  condition:
    - condition: state
      entity_id: input_boolean.borte_lenge_nok
      state: 'on'
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.borte_lenge_nok
  - service: scene.turn_on
    entity_id: scene.stuekjokkengang_medium
1 Like

Thank you @yan.

I tried your code, in the second automation I get a validation error. Could not figure out what was wrong.

Here is the error log:
Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->trigger->0->state. (See /config/configuration.yaml, line 118). Please check the docs at https://home-assistant.io/components/automation/

I changed the coding above.
I made a change by copying the atuomation condition to the trigger :slight_smile:

1 Like

Thank you so much! I will try it tomorrow when I get back home.

Thank you so much @yan. Your code worked perfect!! :slight_smile: