Fibaro trigger after dark

Trying to get this to only trigger efter sunset or even at given times wit ‘condition’, but I dont get it to work. Using this atm, but then its triggered 24/7; might as well keep the light on!

 alias: Light on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'on'
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

works great, but stops working if I sneak in:

    condition:
      - condition: sun
        after: 'sunset'
        offset: '-01:00:00'

and why is that? :confused:

The example in the docs is formatted as shown below. Note the “after_offset” difference and no single quotes around sunset:

condition:
  condition: sun
  after: sunset
  after_offset: "-01:00:00"

Tried it like this, still same error:


- alias: Lights on/off based on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'on'
      condition:
      condition: sun
      after: sunset
      after_offset: "-01:00:00"
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

Could you please format your code correctly. It’s impossible to see indentation problems.

Sorry, I don’t even know how to formate the code here correctly :smiley:

Thanks. I can now see that you have your condition between the trigger statements. This is not correct.

Try it like this:

- alias: Lights on/off based on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'on'
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  condition:
    condition: sun
    after: sunset
    after_offset: "-01:00:00"
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

I’m up for a bathroom-break (sleeping) and tried that pice of code; does not work, since it’s placed to control the off-function? But result is that it never turns on instead. Gives no error, simple just wont flip the switch.

The condition shown above is for the whole automation, not just the trigger immediately above it. Look at the indentation of the condition statement it is at the same indentation level as the trigger and action.

So your lights will only turn on or off from one hour before sunset until sunrise midnight (see @pnbruckner comment below). There is the slight possibility that your off condition wont run - i.e. if the last time it was turned on was within 1m30s of sunrise midnight.

If you want a condition applied to only one of your triggers (the on trigger) you will have to split it into two separate automations, one automation for on, one automation for off.

Note: you can place conditions in the actions section but not in the trigger section.

Actually, it will only be from one hour before sunset until midnight the way it is written.

1 Like

Right, well since I’m getting older…I use it to trigger light when going to bathroom at night :slight_smile:

If you want it to run only from, say, one hour before sunset to one hour after sunrise, then the condition should be:

  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-01:00:00"
      - condition: sun
        before: sunrise
        before_offset: "01:00:00"

I dont want the light to trigger at all when it’s light outside, only want it to trigger when it’s dark. I’m trying to achieve that with the automation; only trigger when dark.

Exactly. That’s why we’re suggesting a condition that will only allow the actions to run between sunset and sunrise. Feel free to adjust the offsets if you want a smaller time window.

Right, so it now works, but there is a delay in switching on; I would say it’s about 2secs, and require sensor to be triggered with two blinks, which I assume is 2 times.
I will now try to get back to sleep for an hour or two!

- alias: Lights on/off based on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'on'
    - platform: state
      entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-01:00:00"
      - condition: sun
        before: sunrise
        before_offset: "01:00:00"
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

Again I must warn that as written, the off trigger will not occur if the movement sensor was triggered within 1m30s of sunrise (plus your offset). Unlikely but possible.

You would be much better off splitting the automation into two, one automation for turning the light on with the condition that it must be dark, and one automation to turn the light off with no darkness condition. Instead a condition that the light actually be on could be used to prevent unnecessary commands being sent.

Like so:

- alias: Lights on based on motion
  trigger:
    platform: state
    entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "01:00:00"
      - condition: sun
        before: sunrise
        before_offset: "-01:00:00"
  action:
    service: homeassistant.turn_on
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

And also:

- alias: Lights off based on no motion
  trigger:
    platform: state
    entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
    to: 'off'
    for:
      minutes: 1
      seconds: 30
  condition:
    condition: state
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

This second automation would prevent your lights being left on at any time.

Ah I see what you did there Tom, smart. Also just figured that my editor made tabs; which made my code…well less workable. Maybe I’m doing most of my automations backwards, I’ll have a look; this code got smart when divided.
Thanks again for your help, I’ll know the workings after sundown :smiley:

So now it’s night again, light does not turn on from motion, but it does turn off according to timer if I turn it on manually.

Please post your automation(s) as it/they currently exist(s.)

- alias: Lights on based on motion
  trigger:
    platform: state
    entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "01:00:00"
      - condition: sun
        before: sunrise
        before_offset: "-01:00:00"
  action:
    service: homeassistant.turn_on
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3
- alias: Lights off based on no motion
  trigger:
    platform: state
    entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
    to: 'off'
    for:
      minutes: 1
      seconds: 30
  condition:
    condition: state
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: switch.neo_coolcam_power_plug_12a_switch_3

Can you confirm that it is after an hour after sunset and before an hour before sunrise?