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'
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.
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
- 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