Newbie trying to utilize a while loop in an automation

Hello,
I am trying to write my first non-UI automation. I have some Tuya Bluetooth controlled shutters. I can control these in my dashboard. They do not always respond on the first try however. Why, is another topic to be explored at a later date. Now I am testing an automation that will keep trying to close the shutters (position 0) until they are closed with an interval of 10 seconds between each attempt. The automation is unavailable due to a formatting error (?) or ?. The message in the UI is:
Automation is unavailable
Actions: required key not provided @ data[0][‘repeat’][‘sequence’]

Any guidance here is appreciated. Thank you.

- id: "9999999"
  alias: manual_test
  description: ""
  trigger:
    - platform: time
      at: "13:00:00"
  condition:
    - condition: or
      conditions:
        - condition: device
          device_id: 7442a7149e9f6a5399a88ed306947fd1
          domain: cover
          entity_id: f928165478a51a7d7d6f7213de6cf389
          type: is_position
          above: 40
          below: 100
  action:
    - repeat:
        while:
          - condition: device
            device_id: 7442a7149e9f6a5399a88ed306947fd1
            domain: cover
            entity_id: f928165478a51a7d7d6f7213de6cf389
            type: is_position
            above: 0
            below: 100
      sequence:
        - service: cover.set_cover_position
          metadata: {}
          data:
            position: 0
            target:
              device_id: 7442a7149e9f6a5399a88ed306947fd1
          -delay:
            seconds: 10
  mode: single

the sequence block needs to be indented one more. needs toline upwith the “while” if the "while is going to control the sequence.

might be more, but that’s certainly and issue and i didn’t look past that first…

1 Like

In addition to what’s mentioned above, you need a space between the hyphen and “delay”, and the or condition isn’t doing anything.

- id: "9999999"
  alias: manual_test
  description: ""
  trigger:
    - platform: time
      at: "13:00:00"
  condition:
    - condition: device
      device_id: 7442a7149e9f6a5399a88ed306947fd1
      domain: cover
      entity_id: f928165478a51a7d7d6f7213de6cf389
      type: is_position
      above: 40
      below: 100
  action:
    - repeat:
        while:
          - condition: device
            device_id: 7442a7149e9f6a5399a88ed306947fd1
            domain: cover
            entity_id: f928165478a51a7d7d6f7213de6cf389
            type: is_position
            above: 0
            below: 100
        sequence:
          - service: cover.set_cover_position
            metadata: {}
            data:
              position: 0
            target:
              device_id: 7442a7149e9f6a5399a88ed306947fd1
          - delay:
              seconds: 10
  mode: single
1 Like

another thing to help you on your journey on writing more interesting code… avoid using device_id’s. use friendly entity id’s. it’ll be a big help to you…

more info on why:

1 Like

@Didgeridrew @armedad Thank you both for your replies. They are appreciated! I am using the Studio Code Server addon and it seemed like it was flagging my spacing errors but I guess not all of them. I don’t code in Phyton so “spacing as syntax” is not something I am used to. I made the changes as best I understood them but still getting an error I don’t understand.

Automation is unavailable
Actions: offset None should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[0][‘repeat’][‘sequence’][1][‘delay’]

  alias: manual_test
  description: ""
  trigger:
    - platform: time
      at: "13:00:00"
  condition:
    - condition: or
      conditions:
        - condition: device
          device_id: 7442a7149e9f6a5399a88ed306947fd1
          domain: cover
          entity_id: f928165478a51a7d7d6f7213de6cf389
          type: is_position
          above: 40
          below: 100
  action:
    - repeat:
        while:
          - condition: device
            device_id: 7442a7149e9f6a5399a88ed306947fd1
            domain: cover
            entity_id: f928165478a51a7d7d6f7213de6cf389
            type: is_position
            above: 0
            below: 100
        sequence:
          - service: cover.set_cover_position
            metadata: {}
            data:
              position: 0
            target:
              device_id: 7442a7149e9f6a5399a88ed306947fd1
          - delay:
            seconds: 10
  mode: single

Incorrect:
image

Correct:
image

seconds needs to be indented.

If you haven’t seen this already, and you are just starting with YAML configuration, I would highly recommend the follow blog post:

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

1 Like

@armedad Yes, device ids are ugly and not desirable. I tried using a template(?) to get rid of them but I was making some kind of mistake. I decided to first get a syntactically correct automation first. I will come back to the ids.

@Didgeridrew has the direct answer.

something i’d really encourage you to do… because using us to debug your code is going to be a long slow road… use the ui. take your code, paste it into a blank automation, then flip it to the UI and see if it does what you want.

you would have seen that the delay time went to 0. it rejected your 10. then when you put 10 in and flip it back to the yaml, you’d see that it got indented.

the UI will find issues for you and will teach you at the same time.

you don’t use a template to get rid of it. you use the entity_id friendly names. like light.living_room and switch.heater

it makes things way more readable to you. also as a bonus, home assistant will autocomplete for you when you type light. you’ll never go back once you start converting over.

and it will catch errors for you too…

2 Likes

@Didgeridrew Thank you! The automation no longer throws an error.

1 Like

@armedad I use the UI but in this case by automation had too many errors for it to even display it. :frowning:

@armedad The documentation says not to edit the automations.yaml file that has the UI automations so I have them in another folder. Apparently that means I cannot edit them in the UI:

The idea of having all my automations in one file goes against my better organizational judgement.

I go to the UI and edit it in full yaml mode.

I almost never edit the automations.yaml directly unless I’m doing a mass search and replace… Too easy to screw something up accidentally. Also no real benefit if you are just working on one yaml

btw there are lots of good reasons to do the programming in the UI, even if you are doing yaml only. For example The ui will recognize a bunch of changes and update it for immediate use, whereas if you do the automations.yaml you have to manually go reload it.

@armedad Well I tried using the friendly names for the entiy_id and now I have an error that I don’t know how to fix. I change the entity name for one of the entities and there was no complaints.
None of the automations show an error.
When I run the checks in the developer tools, the YMAL configuration reloading.
All Yaml Configuration gives me this error:


Check all the others below give me the green check mark.
The “Check Configuration” give me what looks like the same error just written differently.

Is this error caused by changing the name of an entity? Or? How do I trouble shoot this?

Thank you.

it’s not likely caused by using entity id’s. more likely that you fat fingered something along the way. did you do a mass change across all config? or did you just change this one function?

i’m also guessing you might have done direct edit to the .yaml file instead of going into the ui? (again, the ui will largely protect you against stuff like this)…

If all the items underneath are green, I would primarily suspect something in configuration.yaml.

So isolate that I would remove (save away) big blocks of the configuration.yaml file and retry the reload until I found the culprit…

And I would use the check configuration button first before you try reloading.

I only edited one file that has now three automations in it.
I edited the entity name here:

I cannot figure out how to do a while in the UI. The UI will no let me edit those automations. I would happily edit in the UI but it won’t let me.


How can I use the UI and not be forced to have every automation in one unwieldly file?

Well it the automations file is source of the error.
But before I commented them out the automations list did not show any errors.
I guess I will work on it another day, up way too late already.

You do a while in the UI builder by choosing repeat. Then choose ‘while’ as the type of repeat.

But what I also mean is that if you want to do pure yaml, go into the UI and then choose ‘edit in yaml’