Automation Question... Newby stuck on if, else and conditions

Home Assistant Scripts and Automations

I’d appreciate some thoughts, on this - my very first ‘complex’ automation (please be kind :slight_smile: )

This automation is meant to run at 09:00, 10:00 and when Person1 or Person2 changes from home to away, or vice versa. It should run Monday through Friday.

I want it to check “occupancy” status - which is essentially the homes ‘mode’ and can be set as ‘normal’, ‘guests’, ‘vacation’ - the idea is that automations do / do not run depending on the mode. This should run when occupancy mode is ‘normal’.

If Person1 or Person2 not at home, I want to execute the script to stop all Sonos speakers. I dont want them on when we are both away.

On Mon, Wed, Fri I want to run a script which plays Radio 2 on the Sonos at 09:00 if Person1 is home and Person2 is away, and at 10:00 if both Person1 and Person2 are home. In other words irrespective on any other trigegr, start the Sonos at 10:00 anyway.

On Tue & Thu I want to run a similar script which plays Capital Dance on the Sonos using exactly the same rule set. Thus the only variance is that I want Capital Dance on Tue & Thu and Radio 2 other days.

I asked Gemini Google to have a look at it and suggest improvements, what it came back with - I couldn’t really understand… I’d appreciate some thoughts please.

Thank you

alias: Test Sonos Automation
description: “”
trigger:

  • platform: time
    at: “09:00:00”
    enabled: true
  • platform: time
    at: “10:00:00”
    enabled: true
  • platform: state
    entity_id:
    • person.person1
    • person.person2
      to: not_home
  • platform: state
    entity_id:
    • person.person1
    • person.person2
      to: home
      condition:
  • condition: time
    after: “08:59:59”
    weekday:
    • mon
    • tue
    • wed
    • thu
    • fri
      before: “17:30:01”
      enabled: true
  • condition: state
    entity_id: input_select.occupancy
    state: Normal
  • condition: state
    entity_id: media_player.all_sonos_speakers
    state: “off”
    action:
  • if:
    • condition: state
      entity_id: person.person2
      state: not_home
    • condition: state
      entity_id: person.person1
      state: not_home
      then:
    • action: script.sonos_stop_all_sonos_speakers
      data: {}
      else:
    • if:
      • condition: and
        conditions:
        • condition: time
          weekday:
          • mon
          • wed
          • fri
            enabled: true
            after: “09:00:00”
            before: “09:59:59”
        • condition: state
          entity_id: person.person1
          state: home
        • condition: state
          entity_id: person.person2
          state: not_home
          then:
      • action: script.sonos_play_radio_2_across_house
        data: {}
        else:
      • condition: time
        after: “10:00:01”
        before: “17:30:00”
      • action: script.sonos_play_radio_2_across_house
        data: {}
    • if:
      • condition: time
        weekday:
        • thu
        • tue
          enabled: true
          after: “09:00:00”
          before: “10:00:00”
      • condition: state
        entity_id: person.person1
        state: home
      • condition: state
        entity_id: person.person2
        state: not_home
        then:
      • action: script.sonos_play_capital_dance_across_house
        data: {}
        else:
      • condition: time
        after: “10:00:01”
        before: “17:30:00”
      • action: script.sonos_play_capital_dance_across_house
        data: {}
        mode: single

In order to help you, we must be able to see your automation with proper indentation. For that you must put it in a code block (three backticks before and after the code. Would you be so kind? Also, please post what you made of it, That is way more useful than trying to make sense of a chatbot.

Than you - I hope this helps

alias: Test Sonos Automation
description: ""
trigger:
  - platform: time
    at: "09:00:00"
    enabled: true
  - platform: time
    at: "10:00:00"
    enabled: true
  - platform: state
    entity_id:
      - person.person1
      - person.person2
    to: not_home
  - platform: state
    entity_id:
      - person.person1
      - person.person2
    to: home
condition:
  - condition: time
    after: "08:59:59"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    before: "17:30:01"
    enabled: true
  - condition: state
    entity_id: input_select.occupancy
    state: Normal
  - condition: state
    entity_id: media_player.all_sonos_speakers
    state: "off"
action:
  - if:
      - condition: state
        entity_id: person.person2
        state: not_home
      - condition: state
        entity_id: person.person1
        state: not_home
    then:
      - action: script.sonos_stop_all_sonos_speakers
        data: {}
    else:
      - if:
          - condition: and
            conditions:
              - condition: time
                weekday:
                  - mon
                  - wed
                  - fri
                enabled: true
                after: "09:00:00"
                before: "09:59:59"
              - condition: state
                entity_id: person.person1
                state: home
              - condition: state
                entity_id: person.person2
                state: not_home
        then:
          - action: script.sonos_play_radio_2_across_house
            data: {}
        else:
          - condition: time
            after: "10:00:01"
            before: "17:30:00"
          - action: script.sonos_play_radio_2_across_house
            data: {}
      - if:
          - condition: time
            weekday:
              - thu
              - tue
            enabled: true
            after: "09:00:00"
            before: "10:00:00"
          - condition: state
            entity_id: person.person1
            state: home
          - condition: state
            entity_id: person.person2
            state: not_home
        then:
          - action: script.sonos_play_capital_dance_across_house
            data: {}
        else:
          - condition: time
            after: "10:00:01"
            before: "17:30:00"
          - action: script.sonos_play_capital_dance_across_house
            data: {}
mode: single

Yes, it does. This I can read. First thing that strikes me is that you try to do everything in one automation, while there are different things you want at different times. That makes it hard to write, hard to read and hard to debug. Also for me.

Once you get better at writing automations you might be able to combine it in ways it remains understandable, but for now I would try to separate out a couple of simple automations instead of one big one.

For one, I would split off the script to one thing for one person. If different days have different actions, you might want to consider to split out those too. Then if it works, decide if you want to incorporate the other case in the same script, or create a different, similar one.

Start from a simple action you want done, and think of what should trigger it starting and what conditions would need to be satisfied. See if you can avoid if in the action part, then you know you isolated a single action.

Some tips: nobody is home can be tested by looking at the state of zone.home which holds a person count. If it goes to 0 the last person left, if it changes from 0 the first person came home. No need to test who. Use the conditions if different persons home lead to different actions. That is a good reason to test for who.

2 Likes

@Edwin_D Thank you very much for your thoughts. I will try to simplify as you suggest.