Lights, more issues

I have been having issues with my setup and was wondering if somebody could help me please.

Devices are Sonoff, Basic and T4EU1C light switches (number of them)
All of them have been Tasmotised to 8.3.1
Connected via MQTT to HA.

I have 2 sets of conditions. Lights ON/OFF when I am Home, & Lights ON/OFF when I am NOT Home.

Both options work fine independently.

What I want to do is, when I am away,

  • Lights come on as programmed.
  • Depending on what time I return, if any lights are on, they turn off.
  • If any lights are scheduled to turn on, their automation stops from happening.
  • Lights should work as I am home.

I have tried doing this but the automation doesn’t stop as I want. Script below turns the light on when no one is home.

- id: '1592821885507'
  alias: 3020A - Ayesha Lamp Boolean ON/OFF when NO ONE is at Home
  description: Turns Ayesha Lamp Boolean ON/OFF when NO ONE is at Home
  trigger:
  - event: sunset
    offset: '1'
    platform: sun
  - entity_id: binary_sensor.somebody
    for: 00:05:00
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: binary_sensor.somebody
      state: 'off'
    - condition: state
      entity_id: input_boolean.lamp_ayesha
      state: 'off'
    - after: '17:00:00'
      before: '22:00:00'
      condition: time
    - after: sunset
      after_offset: '1'
      condition: sun
  action:
  - delay: 00:{{ '{:02}'.format(range(1,15) | random | int) }}:00
  - data: {}
    entity_id: input_boolean.lamp_ayesha
    service: input_boolean.turn_on
  - data:
      message: Ayeshas Bedroom Lamp Boolean just changed to {{ (states('input_boolean.lamp_ayesha'))
        }} when No One is at Home.
    service: notify.mobile_app_iphone
  mode: single

Script below stops the automation:

- id: '1595607660349'
  alias: 4001 - Turn All Lights OFF when Arriving Home
  description: ''
  trigger:
  - entity_id: binary_sensor.somebody
    for: 00:01:00
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: light.lamp_ayesha
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.lamp_divya
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_ayesha
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_bathroom
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_cloakroom
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_diningroom
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_divya
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_ensuite
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_lounge
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_mums
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_study
    service: homeassistant.turn_off
  - data: {}
    entity_id: light.light_utility
    service: homeassistant.turn_off

I hope the above two bits of code makes sense.

I don’t see where there is anything in the automation (not a script - automations have triggers, scripts don’t) that turns on any light. It only turns on an input_boolean and sends a notification.

The automation doesn’t stop any other automation. It only turns off all the listed lights all at the same time.

On a side note (or two) you don’t need to run different services for every light you want to turn off. you can list them all under the same service call using a entity_id “list”. And usually the service call to operate lights is the “light.turn_on(off)” service:

action:
  - data: {}
    entity_id: 
      - light.lamp_ayesha
      - light.lamp_divya
      - light.light_ayesha
      - light.light_bathroom
      - light.light_cloakroom
      - light.light_diningroom
      - light.light_divya
      - light.light_ensuite
      - light.light_lounge
      - light.light_mums
      - light.light_study
      - light.light_utility
    service: light.turn_off

I’m not really sure what the two automations you posted aren’t actually doing that you want them to do since they don’t seem to be closely related.

Hi, thank you for your message.

Yes, forgot to mention, the first automation turns the input_boolean on, which in turn switches the light(s) on. If the light(s) are on and when you arrive home, I want to turn all lights off (second part of the script which turns all lights off, individually). This part is not working.

If that is the only part that isn’t working (turning the lights off) then can you trigger the automation manually and cause the actions to run (the lights turn off)?