Sunset, motion and time config.... Help

- alias: Turn on livingroom lights when movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001236123
    to: 'on'
condition:
  condition: or
  conditions:
  - condition: sun
    after: sunset
    after_offset: "-00:45:00"
  - condition: sun
    before: sunrise
    before_offset: "00:30:00"
action:
  service: homeassistant.turn_on
  entity_id: group.tvrum_lampor

Im new to all this, in the config as works. I would like to add time instead of sunrise. I want it to be light after sunset, but only to 23:59 I ex.
Had domoticz before and it was so much easier, but wanna learn new stuff.
Is there also a way to use virtual switches as well?

Hi,

i have something similiar, I want (exterior) lights to be on from sunset to 11pm and act motion-triggered until sunrise.

The “virtual switch” you mention could be a input_boolean component that is set on at sunset and switched off at 11pm

configuration.yaml:

input_boolean:
  permanent_light_phase:

automations.yaml:

- alias: Control permanent light phase
  trigger
  - platform: sun
    event: sunset
  - platform: time
    at: '23:00:00'
  condition: []
  action:
  - data:
      entity_id: input_boolean.permanent_light_phase
    service_template: input_boolean.turn_{% if trigger.platform == 'time' -%}off{%-
          else -%}on{%- endif %}

This above controls the input_boolean.permanent_lights_phase state. Since sunset will turn it on and time will turn it of, we can easily handle both triggers by one automation.

Next, the input_boolean is used as a condition in your light rule. Remember that all conditions must pass to make an action happen:

- alias: Turn on livingroom lights when movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001236123
  condition:
  - condition: state
    entity_id: input_boolean.permanent_light_phase
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - data:
      entity_id: group.tvrum_lampor
    service_template: homeassistant.turn_{{ trigger.to_state.state | lower }}

Btw: {{ trigger.to_state.state}} will result in ‘on’ or ‘off’, depending on the motion’s sensor state. We can use this so simplify the service call using service_template.

I did however discard your sunrise/sunset offsets, feel free to adjust my proposition to your needs.

Hope that helps

m0wlheld

1 Like

Thanks for the code! Now I got a “virtual switch” that turns of at the time I want.
But what do ill need to add to make the group.tvrum_lampor not to turn on when permanent_light_phase is set to off? Cause now the do! Ill think my head is missing something :stuck_out_tongue:

And a something something so the lights are on for xx minutes.
Thanks

The condition part should prevent this. Without further AND or OR sub-condition all conditions must pass to make the action happen. Here, the trigger handling should stop, if input_boolean.permament_light_phase is not ‘off’

  condition:
  - condition: state
    entity_id: input_boolean.permanent_light_phase
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'

To let the lights on for some time on motion, it depends on the options of the device. I have Homematic motion sensors (popular in Germany), that can be configured with respect to how long they report movement. So I do this “in hardware” without automation.

For other devices, you could copy the above automation into two and change the trigger parts of them:

For turning the lights on:

- alias: Turn on livingroom lights when movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001236123
    to: 'on'

For turning the lights off 5minutes after the sensor reported no movement any more:

- alias: Turn off livingroom lights when movement ended
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001236123
    to: 'off'
    for: 
      minutes: 5
1 Like

So this should be correct?
But there is some error, cause the config says error on something in this part.

   - alias: Turn on livingroom lights when movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001236123
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.permanent_light_phase
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - data:
      entity_id: group.tvrum_lampor
    service_template: homeassistant.turn_{{ trigger.to_state.state | lower }

Check indendation. YAML is a fucked up format, expressing context by indentation. trigger, condition and action must be on the same level as the alias, so it should be

- alias: Turn on livingroom lights when movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001236123
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.permanent_light_phase
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - data:
      entity_id: group.tvrum_lampor
    service_template: homeassistant.turn_{{ trigger.to_state.state | lower }