Noob starting off with HA - copy/paste from cookbook not working

Hi all,

I have just started with HA after dropping it due to lack of time.
I am running HA 0.99.3 via HASS.IO which I just installed fresh.

I have added some devices which are fairly straight forward.

However, I am having issues with automation and scripts.

Specifically, I am following this link to allow my remote control to dim a light:

I have copied the script code directly into my scripts.yaml and the automation code into automations.yaml.

However, the HA log shows the following:

Invalid config for [automation]: [initial_state] is an invalid option for [automation]. Check: automation->action->1->initial_state. (See /config/configuration.yaml, line 13). Please check the docs at https://home-assistant.io/components/automation/
Invalid config for [script]: [light_bright] is an invalid option for [script]. Check: script->script->script->light_bright. (See /config/configuration.yaml, line 14). Please check the docs at https://home-assistant.io/components/script/

Here is my scripts.yaml:

# Nursery Light Dimming Script
script:
    light_bright:
      sequence:
        - service: light.turn_on
          data_template:
            entity_id: light.nursery_night_light
            brightness: >-
              {% set current = state_attr('light.nursery_night_light', 'brightness')|default(0)|int %}
              {% set step = states('input_number.light_step')|int %}
              {% set next = current + step %}
              {% if next > states('input_number.light_maximum')|int %}
                {% set next = states('input_number.light_maximum')|int %}
              {% endif %}
              {{ next }}

        - service_template: >
            {% if state_attr('light.nursery_night_light', 'brightness')|default(0)|int < states('input_number.light_maximum')|int %}
              script.turn_on
            {% else %}
              script.turn_off
            {% endif %}
          data:
            entity_id: script.light_bright_pause

    light_bright_pause:
      sequence:
        - delay:
            milliseconds: 1
        - service: script.turn_on
          data:
            entity_id: script.light_bright

    light_dim:
      sequence:
        - service: light.turn_on
          data_template:
            entity_id: light.nursery_night_light
            brightness: >-
              {% set current = state_attr('light.nursery_night_light', 'brightness')|default(0)|int %}
              {% set step = states('input_number.light_step')|int %}
              {% set next = current - step %}
              {% if next < states('input_number.light_minimum')|int %}
                {% set next = states('input_number.light_minimum')|int %}
              {% endif %}
              {{ next }}

        - service_template: >
            {% if state_attr('light.nursery_night_light', 'brightness')|default(0)|int > states('input_number.light_minimum')|int %}
              script.turn_on
            {% else %}
              script.turn_off
            {% endif %}
          data:
            entity_id: script.light_dim_pause

    light_dim_pause:
      sequence:
        - delay:
            milliseconds: 1
        - service: script.turn_on
          data:
            entity_id: script.light_dim

Here is my automations.yaml
Note that the code from the linked articled is at the buttom of the automations.yaml due to the automations above being created from the GUI:

- id: '1563276632859'
  alias: Josh Lamp Switch - Single Press
  trigger:
  - event_data:
      event: 1002
      id: br_josh_lamp_sw
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.br_josh_lamp
    service: switch.toggle
- id: '1563276776776'
  alias: Josh Lamp Switch - Long Press
  trigger:
  - event_data:
      event: 1001
      id: br_josh_lamp_sw
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.br_lisa_lamp
    service: switch.toggle
- id: '1563277912398'
  alias: Doorway Switch - Single Press
  trigger:
  - event_data:
      event: 1002
      id: br_doorway_sw
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.br_josh_lamp
    service: switch.toggle
  - data:
      entity_id: switch.br_lisa_lamp
    service: switch.toggle
  initial_state: 'on'
- id: '1563278930333'
  alias: Lisa Lamp Switch - Single Press
  trigger:
  - event_data:
      event: 1002
      id: br_lisa_lamp_sw
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.br_lisa_lamp
    service: switch.toggle
- id: '1563360509634'
  alias: Lisa Lamp Switch - Long Press
  trigger:
  - event_data:
      event: 1001
      id: br_lisa_lamp_sw
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.br_josh_lamp
    service: switch.toggle
- id: '1569822858230'
  alias: Nursery Night Light - On/Off SW
  trigger:
  - event_data:
      event: 1002
      id: nursery_remote
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: light.nursery_night_light
    service: light.toggle

# Nursery Light Dimming via Remote

  - alias: 'Make the lights go bright'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: deconz_event
        event_data:
          event: 2001
          id: nursery_remote
    action:
      - service: script.turn_on
        data:
          entity_id: script.light_bright

  - alias: 'Stop the bright just there'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: deconz_event
        event_data:
          event: 2003
          id: nursery_remote
    action:
      - service: script.turn_off
        data:
          entity_id: script.light_bright
      - service: script.turn_off
        data:
          entity_id: script.light_bright_pause

  - alias: 'Make the lights go dim'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: deconz_event
        event_data:
          event: 3001
          id: nursery_remote
    action:
      - service: script.turn_on
        data:
          entity_id: script.light_dim

  - alias: 'Stop the dim just there'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: deconz_event
        event_data:
          event: 3003
          id: nursery_remote
    action:
      - service: script.turn_off
        data:
          entity_id: script.light_dim
      - service: script.turn_off
        data:
          entity_id: script.light_dim_pause

I hope someone can shed some light as I have merely copy and pasted from the official HA site and replaced the entities with my own.

Thanks in advance.

The indention is incorrect for the automations.yaml file.

Starting after the line “#Nursery Light Dimming via Remote”, everything is indented two spaces too much.

# Nursery Light Dimming via Remote

  - alias: 'Make the lights go bright'
    initial_state: 'on'

This way the automation thinks that - alias: 'Make the lights go bright' still belongs to the action portion of the automation above it.

I think it could be the same problem with the scripts.yaml.
Try to indent everything two spaces less and see if you still get an error.

hmmm, thanks.

I think it might be the indentation, because when I started removing some indentation in the automations.yaml I got different errors in the log.

Unfortunately, its not easy for me to remove just two indents automatically as when I hit the delete key twice per line, some lines remove all indents automatically for some reason.

Note: I am using the Configuration Editor addon to make changes to my files.

In regards to the scripts.yaml, this was a straight copy paste…

Which begs the question, why is the documentation incorrect?
Why would the incorrect indentation be in the code in the documentation?

I am asking because surely other noobs attempting to get HA working would find this frustrating that copying code from official documentation would not work.

Do you know of an easier way to remove incorrect indentation?

Cheers

It might also be another problem with the scripts.yaml, maybe the indention is correct, that’s why I wanted that you try it first. In case the documentation is incorrect, we can contact the author afterwards to get it corrected.

I don’t know anything about the configuration editor. I use Sublime Text for editing my YAML files and there it is easy to indent to the left or right.

OK, I’ll give Sublime Text a shot and report back.

Thank you for pointing me in the right direction.

Using Sublime Text editor I was easily able to Unindent the automations.yaml code and now I don’t have any errors in the log for that file.

I tried the same for the scripts.yaml and I got no such luck.

I still get:

Invalid config for [script]: [light_bright] is an invalid option for [script]. Check: script->script->script->light_bright. (See /config/configuration.yaml, line 14).

Any ideas?

Forgive me for butting in here but the thing that stands out for me is that you have a scripts file (probably script.yaml) so that was included under an include from your configuration.yaml (I use packages so I’m groping here) from memory that include has a script: header, why are you repeating that header here.?

I think @Mutt is right.

In your configuration.yaml you probably have something like:

script: !include config/scripts.yaml

If you have this, then you need to remove the first line ‘script:’ in your scripts.yaml file and probably you have to adjust the indentation as well afterwards. Due to the include in the configuration.yaml file, the system already knows that there are scripts in the scripts.yaml file and therefore the heading ‘script’ is not needed.

@Mutt thank you for butting in!

You were absolutely right, it was the additional “script:”. I blatantly copied the code without thinking that it was already in the configuration.yaml include.

My apologies, noob mistake.

So the fix for clearing out the errors was to fix the indentation and remove the redundant “script:”.

Now I have to figure out why the script is not doing what it’s supposed to.
The automation triggers the script no problem, but the script just turns the light off instead of increasing the brightness.
Even when executing the script from the Overview page, the same issue.

I tested the “light.turn_on” service via the service dev tool and specified brightness and it makes the change immediately, so the light is performing as it should.

The linked doco, mentions “input_number” however:
a) I don’t know if this is needed
b) It doesn’t specify what file I need to put this in (scripts.yaml, configuration,yaml, automation.yaml ?)

Sigh… it would be nice if you could just point a dimmer remote to a light or set of lights and it would just do its thing.

I am learning how much effort it takes to get simple things like a dimmer control to dim a light in HA. :slight_smile:

Appreciate all your help.
I will keep working on getting this working.

You need the input_number, otherwise this automation will not work.

In your light.turn_on script the brightness will be increased by the number chosen in the input_number, so if you don’t have the input_number, the light will just be turned on to the same brightness. You also need the other two input_numbers to define a maximum and a minimum, otherwise you’ll probably get an error because if you are at full brightness and you want to increase it more, it doesn’t work.

        - service: light.turn_on
          data_template:
            entity_id: light.nursery_night_light
            brightness: >-
              {% set current = state_attr('light.nursery_night_light', 'brightness')|default(0)|int %}
              {% set step = states('input_number.light_step')|int %} #the step to increase comes from input_number.light_step
              {% set next = current + step %}
              {% if next > states('input_number.light_maximum')|int %}
                {% set next = states('input_number.light_maximum')|int %}
              {% endif %}
              {{ next }}

About where to put the input_numbers, I’d suggest to read this:

You can put it in the configuration yaml, or you can put it in it’s own package or you can do the same like you did for the scripts and add input_number: !include config/input_number.yaml to your configuration.yaml and create a new file input_number.yaml. It all depends on how you want to organize your system.

Thank you for the information, I will give it a go and report back.

Thanks for assisting a beginner.
Sometimes it’s not obvious what to do and it helps when people point you in the right direction.

Sorry, but what doesn’t help a beginner is having 16 different ways of doing the same dang thing.
With different indenting required for different configurations.
I recommend that you go the packages route
EVERYTHING I have is based on packages and EVERYTHING is in a common format.
I could send you ‘my package for an alarm clock’ (ficticious example) and everything you need is in the same package, the only thing you’d need to change is the name of the lamp/radio/pnumatic bed tipper that it acts upon.

@Mutt , I am very interested in any way I can standardise my configuration as I get confused at the best of times.

I googled “home assistant packages” and found the main article, however, I am still having trouble understanding how they work.

Could you explain in layman’s terms how packages work?
Ie. Do I still need to put code into configuration.yaml, scripts.yaml, automation.yaml etc?
Are you saying that packages have one set of indentation rules?

Does HA have a “best practice” way of doing things to keep things clean and tidy? or is it a bit of the wild west where anything goes?

Sorry for all the questions…

A package combines multiple sensors, scripts whatever into one file.
E.g. you have an office with a media player and a door sensor. Without packages you would add this to your configuration.yaml

sensor:
  - platform: template
    name: Office Door
    .....

media_player:
  - platform: yamaha_musiccast
    name: Yamaha Receiver
    .....

Now if you have more rooms and more sensors, media players etc. you add them to the configuration.yaml and it grows and grows and becomes more and more unorganized.

With packages you can separate the configuration.yaml file into multiple files. For the above example you could create a new folder called ‘packages’ and put a file called office.yaml there. Add this to the office.yaml file:

sensor:
  - platform: template
    name: Office Door
    .....

media_player:
  - platform: yamaha_musiccast
    name: Yamaha Receiver
    .....

And add the following to your configuration.yaml

packages: !include_dir_named packages

Now if you have a new room, you just create a new file in the packages folder and add your sensors etc. there. This way your configuration.yaml stays small and organized. You don’t need to separate it by room, you can separate it however you like. You can have a package that includes everything related to your security system and call it security.yaml or whatever you like.

There’s no “best practice” for this, however a lot of people put their config into packages as I described above.

I splitted up my conifguration into packages if you want to get some inspiration you can check out my GitHub.

Burningstone, a nice example and description.
But Joshua, there’s as many ways to organise packages as anything else, so you may not want to do it by room. It doesn’t matter though as they all co-exist. And you’ll probably change things as time goes by.
It’s up to you and you don’t have to follow our recommendation, after all, its just our opinion :smiley_cat:

OK, so attempting to get packages up and running…

Added the following line into configuration.yaml:

packages: !include_dir_named packages

I created a directory called “packages” under config.

I created a new yaml file called dining-room.yaml inside the packages directory:


# Heater Thermostat
ecobee:
 api_key: !secret ecobee_key

Now I get the following in the log:

Component error: packages - Integration packages not found.

I also tried changing the dining-room.yaml to the following:

dining-room:

# Heater Thermostat
  ecobee:
    api_key: !secret ecobee_key

I thought maybe it needs a package name or something.

However, I get the same error in the log.

Any ideas?

Is the dining-room.yaml in the same directory as the configuration.yaml file?

If yes, try to change

packages: !include_dir_named packages

to

packages: !include_dir_named packages/

No, its in the /config/packages directory.

The configuration.yaml file is in the /config directory.

I’m sorry, that was what I meant, my mistake.

Then please try my suggestion of chaning the part in configuration.yaml to

packages: !include_dir_named packages/

I have tried, but the same error occurs in the log.