New to automation looking for help

Hello, I’m new to automation scripting and trying to get familiar with the syntax.

I’d like to trigger an app to start every time my TV is off and stays off for 30secs. Below was my best attempt, which doesn’t work. Any help would be greatly appreciated. THX

BTW The app launch command works and was tested under developer tools.

- alias: Livingroom Idle Launch Tinycam
  trigger:
  - entity_id: media_player.samsung_qn95ba_85_3
    platform: state
  condition:
  -  condition: or
     conditions:
     - condition: state
       to: 'off'
  action:
  - data:
      entity_id: media_player.android_tv_192_168_0_123
      command: "am start -a android.intent.action.VIEW -d -n com.alexvas.dvr.pro/.MainActivity"
    target:
      entity_id: media_player.samsung_qn95ba_85_3
  - wait_for_trigger:
      - platform: state
        entity_id:
          -  media_player.samsung_qn95ba_85_3
        to: "on"
    timeout:
      seconds: 30
  - if:
      - condition: state
        entity_id: media_player.samsung_qn95ba_85_3
        state: "off"
    then:
      - service: androidtv.adb_command

If you’re new, start with the UI Automation editor. Build an automation with the editor, which will minimize the kinds of mistakes you can make, then learn from the configuration it produces.

Critical Issues:

  1. The State condition does not have a defined entity and uses to, which is not a valid key for a State condition.
  2. The command you claim works, looks like a Service Call but does not have a defined service and uses two different entity IDs.
  3. The service call in the then has no data.

Non-critical Issues:

  1. The or condition isn’t doing anything.
  2. Without a to or from, your trigger will fire on every state and attribute change of the TV entity.
  3. Without a for in either the trigger or condition, there’s nothing checking for the TV being off for 30 seconds.

If your plan was to call the same command if the TV didn’t turn on, use a Repeat Until action. Make sure to include a short delay inside the repeat to allow for the transmission of the command and update to the state of the device.

References:
Service Calls
Or Condition
State Trigger
State Condition
Repeat Until Action

2 Likes

Thanks for the reply, I tried the UI and feel more comfortable with the editor. There are limitations to the UI, I’m also an experienced software developer…

It’s the syntax and command structure I’m finding confusing. I was unable to find a doc with available commands and formatting.

When I stated code works I was specifically speaking of the ADB “template” command package works when manually tested under developer tools.

At issue is my knowledge of Home Assistant automation commands and procedures, if this was written in a standard programing language I’d be fine.

Growing pains of new things, any specific help would be great.

True, but there is nothing in your example that cannot be done in the UI editor.

There are no commands or procedures… it’s just configuration.

Intro to YAML
Automation YAML
Script Syntax

There is nowhere in Developer tools that what you posted would work… it is missing critical configuration components.

A specific question would help get a specific answer.

The “problem” is that the yaml for scrips and automations is not scripting language as such. Yaml is essentially only structured data. The data is interpreted as triggers, conditions and actions. As such there is no language description. So you need to dig into the documentation, where each trigger type, condition type and action type defines it’s own data structure in order to understand what you need to put in. As a result similar constructs often have slightly different data field names.

The limitations in the GUI are for a large part imposed by the data structure. There’s not that much the automation editor can’t do, except for jinja2 templates. So being an experienced programmer will only get you so far. So it is reading and following the documentation for each trigger, condition or action type, or learning from the editor as suggested above. It is helping you enter the required data in the proper data format, so it is not bad advice.

Thanks for the links, my google results did not yield the automation yaml page. This is exactly what I needed!!

I understand as it stands it is a configuration file. It’s a matter of semantics, I simply meant at some point the script and its specific characteristics are parsed into actionable commands.

When I tested the below in the service developer tools UI it worked, can you let me know where I went wrong with this command? THX

service: androidtv.adb_command
data:
  entity_id: media_player.android_tv_192_168_0_123
  command: "am start -a android.intent.action.VIEW -d -n com.alexvas.dvr.pro/.MainActivity"

Thanks, I’m getting the hang of it… I’ll give the UI another try.

It is a temporary measure to learn the data structure. After that you’ll be able yo go to the editor. But when you make mistakes, don’t expect clear errors, but hints where the data is not as expected.

In the above automation, the state trigger is incomplete and the or condition is used incorrectly. All conditions need to be inside the or, nothing before it. As it is you are or-ing just one thing, making the or useless.

Compare what you tested in the Service tool to the automation action in your original post:

  action:
  - data:
      entity_id: media_player.android_tv_192_168_0_123
      command: "am start -a android.intent.action.VIEW -d -n com.alexvas.dvr.pro/.MainActivity"
    target:
      entity_id: media_player.samsung_qn95ba_85_3
  - wait_for_trigger:

As state in my original post noted as Critical Error #2, it looks like a Service Call but does not have a defined service and uses two different entity IDs, one defined directly under data and another (which, IIRC, will overwrite the former) defined under target.

I admittedly have no clue how to script automations… only the actions I’ve tested via developer tools.

Thanks again for all the help, the links are very helpful :slight_smile:

To add to the above, this is one page, I find very useful as well, as it directly has influence on the automations and scripts you write, as templates come in handy very often. :slight_smile:

1 Like