Could this automation have been done smoother / more efficient?

We use these “modes” which are triggered by the state of a boolean to set the mode. Like “home”, “away”, “movietime” (in example), “night” and "vacation.

Could I have done this automation smoother / faster / leaner / more efficient? I am not skilled so please dont suggest anything to complicated.

        ##########################################################
        ## Kveldskos
        ##########################################################

- alias: Kveldskos

  trigger:
    - platform: state
      entity_id: input_boolean.kveldskos
      to: 'on'
      from: 'off'

  condition:
    - condition: state
      entity_id: input_boolean.gjestemodus
      state: 'off'

  action:
    - service: light.turn_off
      entity_id:
        - light.hyllelys
        - light.takspotter_kjokken
        - light.taklampe_kjeller
        - light.takspotter_stue
    - service: light.turn_on
      data:
        entity_id: light.taklys_kjokken
        transition: 2
        brightness_pct: 20
    - service: light.turn_on
      data:
        entity_id: light.benkebelysning
        transition: 2
        brightness_pct: 20
    - service: light.turn_on
      data:
        entity_id: light.bordlampe
        transition: 2
        brightness_pct: 50
    - service: light.turn_on
      data:
        entity_id: light.lampe_ved_ovn
        transition: 2
        brightness_pct: 50
    - service: light.turn_on
      data:
        entity_id: light.lampe_pa_skjenken
        transition: 2
        brightness_pct: 20
    - service: light.turn_on
      data:
        entity_id: light.lys_vinduskarmen
        transition: 2
        brightness_pct: 20
    - service: light.turn_on
      data:
        entity_id: light.trappelys
        transition: 2
        brightness_pct: 20
    - service: switch.turn_on
      entity_id:
        - switch.mikro
        - switch.tv
        - switch.media
        - switch.daniel_nattbord
    - service: switch.turn_off
      entity_id:
        - switch.gulvlampe
        - switch.vifte_treningsrom
    - service: homeassistant.turn_off
      entity_id:
        - input_boolean.godnatt
        - input_boolean.gaar_hjemmefra
        - input_boolean.kjorer_hjemmefra
        - input_boolean.hjemme
        - input_boolean.feriemodus

        ##########################################################
        ## Bedtime Guest Mode
        ##########################################################


        ##########################################################
        ## Reset Bedtime Switch
        ##########################################################

# - alias: Hjemme - Reset Switch

#   trigger:
#     - platform: state
#       entity_id: input_boolean.hjemme
#       from: 'off'
#       to: 'on'
#       for:
#         hours: 0
#         minutes: 6
#         seconds: 0

#   action:
#     - service: homeassistant.turn_off
#       entity_id: input_boolean.hjemme

I’m having a little trouble, since English is my primary language, but it looks like you have an input_boolean set up for all the different modes your house can be in. And, each mode, when turned on, turns off all the other input_booleans. And this works.

But, if in the future you decide to add a “Super Party Mode” or something, you’ll have to edit every one of these automations so that they also turn off that input boolean.

For me, for modes of which there can only be one at a time, I use an input_select. input_booleans are used for things that can be on in any mode.

Aside from the comment above I think you can also change the action to the following and it should work…I think…:

  action:
    - service: light.turn_off
      entity_id:
        - light.hyllelys
        - light.takspotter_kjokken
        - light.taklampe_kjeller
        - light.takspotter_stue
    - service: light.turn_on
      entity_id: 
        - light.taklys_kjokken
        - light.benkebelysning
        - light.lampe_pa_skjenken
        - light.lys_vinduskarmen
        - light.trappelys
      data:
        transition: 2
        brightness_pct: 20
    - service: light.turn_on
      entity_id: 
        - light.bordlampe
        - light.lampe_ved_ovn
      data:      
        transition: 2
        brightness_pct: 50
    - service: switch.turn_on
      entity_id:
        - switch.mikro
        - switch.tv
        - switch.media
        - switch.daniel_nattbord
    - service: switch.turn_off
      entity_id:
        - switch.gulvlampe
        - switch.vifte_treningsrom
    - service: homeassistant.turn_off
      entity_id:
        - input_boolean.godnatt
        - input_boolean.gaar_hjemmefra
        - input_boolean.kjorer_hjemmefra
        - input_boolean.hjemme
        - input_boolean.feriemodus

Do you have multiple automations, one for each mode?

If you do then there may be an opportunity to use Scenes to reduce possible duplication of code in the automations.

Thanks for suggestions, i will experiment further with all of them.
For starters, scenes looks very useful. With this I dont need to have 6-7 different modes for the house, I can cram it down to “home”, “away” and “vaction”. And have scenes “underneath” each mode. If Im thinking out loud it could look something like this:

Home:

  • Morning
  • Day
  • Evening
  • Movietime

Away:

  • Walking (leaving the car in the garage)
  • Driving (taking the car, need the garage to open and then close itself after X min)

Vacation:

  • Same as away, but with certain “vacation-features”.

Personally, I don’t like Scenes. While their syntax is shorter and easier than a script, they don’t do anything a script can’t, but there are lots of things a script can do that a scene can’t (multiple service calls on a single entity, delay, condition, wait_template, event fires, etc). So I make a script for each “mode” named appropriately so I can template a call to them.

Like…

kitchen_cooking
kitchen_breakfast
kitchen_homework
kitchen_dinner
kitchen_sleep

living_tv
living_movie
living_party
living_quiet
living_sleep
living_full

Then I have automations that look like this:

- alias: living_occupied_movie_noguest
  trigger:
    - platform: state
      entity_id: binary_sensor.living_occupied
      to: "on"
  condition:
    - condition: template
      value_template: >-
        {{
        is_state('input_select.living_mode', 'movie') and
        is_state('input_boolean.guest', 'off')
        }}
    - action:
        service: script.living_movie

- alias: living_occupied_guest
  trigger:
    - platform: state
      entity_id: binary_sensor.living_occupied
      to: "on"
  condition:
    - condition: template
      value_template: >-
        {{
        is_state('input_boolean.guest', 'on')
        }}
    - action:
        service: script.living_full

It’s very verbose, but it offers me the most flexibility.

I’m slowly converting to a python_script for each room to handle the logic, because it’s simpler, I think, and I understand python. So, instead, it looks like this:

- alias: living_occupied
  trigger:
    - platform: state
      entity_id: binary_sensor.living_occupied
      to: "on"
    - action:
        service: python_script.living_scene

## living_scene.py
def setScene(hass):
  living_occupied = hass.states.get('binary_sensor.living_occupied').state == "on"
  guest = hass.states.get('input_boolean.guest').state == "on"
  living_mode = hass.state.get('input_select.living_mode').state
  current_hour = int(hass.state.get('sensor.hour').state)

  if not living_occupied:
    hass.services.call('script','living_unoccupied')
    return

  if guest:
    hass.services.call('script', 'living_full')
    return

  if living_mode == "movie":
    hass.services.call('script', 'living_movie')
    return

  if living_mode == "sleep":
    hass.services.call('script', 'living_sleep')
    return  

  if current_hour < 9 or current_hour > 20:
    hass.services.call('script', 'living_quiet')
    return  

  hass.services.call('script', 'living_full')
  return

setScene(hass)

This allows me to put all the logic for which input_booleans override which room mode selectors. It also allows my setup to get very complicated without having to have 100 different automations for each possible scenario, and just one easily changeable script powers each “scene”.

The other benefit to scripts over scenes is that they are reloadable without restarting HASS. So, you can tweak it, trigger it, adjust it, trigger it again etc, until you get it the way you like it.

… and then you jumped the shark with python_scripts. :wink:

Home Assistant offers something for everyone. For this simple application, a Scene is adequate. For more truly complex requirements there everything from scripts, python_scripts, all the way to AppDaemon. How nice to have the right tool for every job.

Absolutely agreed. There is something for every level. The point I was trying to make was, at whatever level a person is at, a scene doesn’t really add much and so, for most people, a script is probably a better choice. Even if they are only doing things a scene could accomplish, a script is just as easy and much more “future proof”.

But, yes, a scene will work just fine if the OP just really wants to learn/use scene despite already knowing how to use script since it’s exactly the same as the “action” part of an automation and therefore the OP likely already knows how to use script.