My first and short attempt at YAML not going so well

It’s through the “New Script” function in the browser in YAML mode.

Then that’s very wrong, because that’s not a script, that’s an automation :wink:

And as Taras said, you can’t paste it in there like that.

You need to:

  1. Use the automation editor
  2. Don’t use YAML
  3. Create two automations entirely in the UI

Automation one:

  • Trigger: Sunset + 1 hour
  • Action: Turn on the switch

Automation two:

  • Trigger: Time of 22:30
  • Action: Turn off the switch

Though, I always recommend the sun elevation over a time offset.

Your reply only adds to the confusion because the example you posted contains two automations, not two scripts.

If you used the Script Editor to create a new script, in YAML mode, and then entered code for two automations,
that’s a mistake and the cause of the error message. Delete it.


EDIT

If you used the Automation Editor to create a new automation, in YAML mode, you entered code for two automations (plus an automation key) and that’s also a mistake (it only allows for entering code to create a single automation, without a leading automation: key).

you most likely have duplicate automation: key inside the automation.yaml file as well as in configuration.yaml.

configuration.yaml should look like this

automation: !include automations.yaml

automations.yaml should look like this:

- alias: ...
  triggers:
    ... etc ...

Notice how automation.yaml does not have the automation: key.

EDIT: @123 already said this above with less words. FYI

1 Like

For the novice, Home Assistant (HA) is A riddle wrapped in a mystery inside an enigma Let us not forget that. @Yaml-Stumped, the HA learning curve is very steep. here are some links that may be helpful:

This link, whille brief, will help you make your requests for help look pretty:

1 Like

Ok, so here’s the deal. I’ve set this thing up on an RPi 5, and added 5 devices in test. 4 switches, and one temperature sensor that never seemed to work right on Hubitat. (works fine now by the way).

Once I got those set up, I put some of them on a dashboard. Works fine.

Now I’m trying to get ONE automation working so that I can see it work as sort of a POC before I start moving all the live devices over from Hubitat.

The problem is setting up automations on this thing is like using a protractor to increase the odds of conception. It’s ridiculously complicated. But that’s ok, I’ve worked with IBM products before. They hold the title for needless complication. The thing that makes Home Assistant different is there’s actually something worthwhile at the end of the rainbow. So I’m giving it a shot.

I came here today to determine what was specifically wrong with a very short script. I need an answer, not a library. I’m happy to surf the learning curve once I can determine this is a worthwhile migration. So far it looks like a headache factory. But that’s ok. I don’t mind working to overcome the challenge in the long term. But I do mind doing it to prove a concept.

Right now I have two pieces left to make the decision. This script, and Alexa integration. The second connected fine but can’t find a single device. I’ll continue to work through that. If I can’t solve the problem by the end of the Nabu trial, HA is off the table.

But when it comes to automations, the “visual editor” is not an option. Mostly because it is no more intuitive than text. So learning anything about it is pointless, to me anyway. That’s why I’m starting with the YAML code. Because if I keep this thing I won’t be using anything else until another option presents itself. If I can understand why a basic timer isn’t working, that would cause me to believe I can work through any issue. That’s all I’m after.

So, whatever these things are called in this community, in the HA browser interface I went to Settings> Automations & Scenes> +Create Automation> Create New Automation> Top right “Edit in YAML”. There I put in the following, which is erroring out on save…

[Begin YAML] (line 1 is automation:")

automation:

  • alias: “Turn on switch an hour after sunset”
    trigger:
    platform: sun
    event: sunset
    offset: “01:00:00”
    action:
    service: switch.turn_on
    target:
    entity_id: switch.z_wave_outdoor_smart_plug

  • alias: “Turn off switch at 10:30 PM”
    trigger:
    platform: time
    at: “22:30:00”
    action:
    service: switch.turn_off
    target:
    entity_id: switch.z_wave_outdoor_smart_plug

[End of YAML]

What’s wrong? Yes, I already removed the “automation:” line and tested. No joy.

See the very first response — FORMAT YOUR CODE SO WE CAN READ IT.

You’ve tried to save the code for two automations at once.

automation:
  - alias: "Turn on switch an hour after sunset"
    trigger:
      platform: sun
      event: sunset
      offset: "01:00:00"
    action:
      service: switch.turn_on
      target:
        entity_id: switch.z_wave_outdoor_smart_plug

  - alias: "Turn off switch at 10:30 PM"
    trigger:
      platform: time
      at: "22:30:00"
    action:
      service: switch.turn_off
      target:
        entity_id: switch.z_wave_outdoor_smart_plug


Try:

alias: "Turn on switch an hour after sunset"
triggers:
  - trigger: sun
    event: sunset
    offset: "01:00:00"
conditions: []
actions:
  - action: switch.turn_on
    target:
      entity_id: switch.z_wave_outdoor_smart_plug

Then do the other one in the same way.

1 Like

Hi, The reason it’s not working is because of this line right here:

As said before, remove that.

What’s wrong is what I explained in my previous post.

If you used the Automation Editor to create a new automation, in YAML mode, you entered code for two automations (plus an automation key) and that’s also a mistake (it only allows for entering code to create a single automation, without a leading automation: key).

You entered YAML for two automations into the Automation Editor that is designed to create one automation at a time.


By avoiding the use of the Automation Editor in Visual mode, and diving straight into YAML mode, you’ve chosen the most challenging way to learn it.

I suggest you consider using Visual mode to compose an automation, then switch it into YAML mode to review the generated code. It will prove to be more educational and productive than the path you’ve chosen.


EDIT

Are you copying these automations from somewhere? The reason why I ask is because they use older automation syntax. The older syntax is still valid but, given that you’re just learning it, it may mislead you to conclude that it is the current way of doing it.

BOTH problems fixed in the same hour! I don’t know if it’s a good thing for you guys but now you’re stuck with me. Alexa is working fine as well!(Until I can figure out a solid off-grid solution. Bezos telling me when I need toner creeps me out!)

THANK YOU!!! I’ll report back to confirm after 10:30PM.

It’s “fixed” because that’s YAML for a single automation, not two automations like what you tried. As per my explanations above, the Automation Editor is for creating a single automation (it will reject YAML code containing multiple automations, which is what you did).

The fact you pasted invalid code into the Automation Editor, supports my suggestion that you should first use Visual mode and observe the resulting generated YAML code. There’s no way Visual mode allows you to compose multiple automations at once.

It prevents you from making fundamental errors, like attempting to paste code for two automations into a place designed for just one.

In fact, I viewed the UI’s Edit-as-YAML interpretation of one of my hand-written automations in order to check the indentation levels needed for pasting in there.

1 Like

Should you get to a point where you have multiple automation, it is advisable (IMO) to reduce the number of automations and trying to use synergy effects.

You can make both automations into one quite easily:

alias: Turn Switch On/Off Based on Time
trigger:
  - trigger: sun
    event: sunset
    offset: "01:00:00"
  - trigger: time
    at: "22:30:00"
condition: []
action:
  - choose:
      - conditions:
          - condition: sun
            after: sunset
            after_offset: "01:00:00"
          - condition: time
            before: "22:30:00"
        sequence:
          - action: switch.turn_on
            target:
              entity_id: switch.your_switch
      - conditions:
          - condition: time
            at: "22:30:00"
        sequence:
          - action: switch.turn_off
            target:
              entity_id: switch.your_switch
mode: single

Yeah but learning the YAML code is probably the easiest for someone like me that can remember strings better than images. I can still remember the phone number of the house I grew up in until I was 5, but couldn’t tell you what color it was with any degree of certainty.

As far as the one automation, I don’t have too many in my Hubitat environment anyway. Maybe two dozen. So knowing I can do it here leaves me no doubt about moving the devices that aren’t attached to any rules/automations. The remainder will trickle over as I get their automations set up in test.

I can’t wait to have an actual “smart” home. Like I tell people now with Hubitat. I don’t have a “smart” home. I have a home that dropped out of beauty school and took a few correspondence courses in advanced cashiering. :rofl:

By starting this topic, you have empirically proven it’s not easier; you misused the Automation Editor in YAML mode and employed outdated automation syntax. Use Visual mode to learn how valid YAML is generated; walk first, then run.

I also recommend you review the documentation’s glossary. You appear to have used terms like script and automation interchangeably but they’re two different concepts in Home Assistant. It makes it more challenging to understand what you did if you use the wrong terminology.

Fair enough

35 devices and counting. I’m now on to automations like this…

alias: Holiday Lights regular lights on at 10:30PM
description: Outside lights on at 10:30PM, and then off again at sunrise.
triggers:
  - trigger: time
    at: "22:30:00"
conditions: []
actions:
  - target:
      entity_id: light.back_wall_light
    data:
      brightness_pct: 6
    action: light.turn_on
  - target:
      entity_id: light.front_wall
    data:
      brightness_pct: 6
    action: light.turn_on
  - target:
      entity_id: light.front_recessed
    data:
      brightness_pct: 3
    action: light.turn_on
  - wait_for_trigger:
      - trigger: sun
        event: sunrise
        offset: 0
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id:
        - 55d2f116af38775dd0627f28cd70fef8
        - a3558423ffac555caaf4ab45eafc20c7
        - 70e6aa23ab5f6173224b3602cc7715c7
mode: single

Thanks again!!!