Help with if/then/else

Hi,

I am relatively new to Home Assistant and would greatly appreciate some help with something I think is pretty basic. I haven’t been able to find much in the documentation.

I’m using the androidtv integration to monitor the status of my Android TV device.

I’m trying to figure out how to write an automation do to the following:

If media_player.androidtv app is launcher
   or media_player.androidtv status is off
   or media_player.androidtv status is standby
then
   call a rest command
else
   call a different rest command

I am doing something similar with several different automations but it is proving somewhat unreliable, for example:

alias: AndroidTV_app_Launcher
description: ''
trigger:
  - platform: template
    value_template: >-
      {{ (state_attr('media_player.android_tv', 'app_id')) ==
      'com.google.android.apps.tv.launcherx' }}
condition: []
action:
  - service: rest_command.androidtv_app_idle
    data: {}
mode: single

and

alias: androidTV_status_off
description: ''
trigger:
  - platform: state
    entity_id: media_player.android_tv
    to: 'off'
condition: []
action:
  - service: rest_command.androidtv_app_idle
    data: {}
mode: single

Thank you for any help you can provide!

The if portion of your pseudo-code is easy to implement. However, it’s the else portion that I find to be odd. You want it to trigger when any other androidtv app is active? Because that’s what that else portion will do.

Hi @123 – yes that’s exactly what I want to do. If Launcher is active, or if the device is in off or standby mode then I want a certain action. For any other state (i.e. an app is loaded) then I want another action.

If you can provide me with a yaml example I would be grateful. From there I could change things as needed.

Thank you for your help!

There’s no concept of else in triggers.

Simple example:

“Look for red cars”
It will trigger when it sees a red car and not when it sees “anything else” like a white car or yellow truck.

“Look for vehicles.”
It will trigger when it sees anything that fits the definition of a vehicle (any color of car or truck). After it has triggered, you could use a condition to filter it or, within the action, determine what kind of vehicle was spotted and proceed to handle it accordingly.

In your case, the pseudo-code’s else obliges to you to create a trigger that monitors for any change to media_player.android_tv regardless of what that change may be. That’s a simple State Trigger like this (it’s triggered by any changes to the entity’s state or attributes):

trigger:
- platform: state
  entity_id: media_player.android_tv

Then it’s left to the action to determine what to do:

alias: AndroidTV_app_Launcher
trigger:
- platform: state
  entity_id: media_player.android_tv
action:
- choose:
  - conditions: >
      {{ state_attr('media_player.android_tv', 'app_id') == 'com.google.android.apps.tv.launcherx' 
         or trigger.to_state.state in ['off', 'standby'] }}
    sequence:
    - service: rest_command.androidtv_app_idle
  default:
  - service: rest_command.androidtv_something_else

1 Like

@123 Thank you!
Is there a way to add an elseif statement (different conditions)?

That’s what choose does. In the example I posted, if the first conditions section within choose is not met then it will execute the default portion. You can have more than one conditions section.

For more information see: Choose a group of actions

1 Like

Thank you for your help! Works well!

1 Like

Glad to hear it solved the issue you posted.

Please consider marking my post (containing the suggested automation) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. Effectively it indicates that the problem is resolved and helps users find answers to similar questions.

1 Like