Script "Choose" based upon text_input field not "choosing" properly

I’m most likely doing this wrong, but here goes:

I created an input_text value to store some states based upon what button on a dashboard someone taps (in a Lovelace dashboard). For example, if they pick the “Watch TV” button, a script sets the text_input to “Watching TV” or if they pick the “Play a game” button, a script also updates the text_input to “Playing a game”. These changes to the text_input are used as triggers for a few Node Red flows and ARE working properly.

Now, I’m trying to conditionally turn off one device (my home-theater-PC) only if the value of the input_text is “Playing a game” when someone taps the “Power off” button (once again, a Lovelace button), but the script I created isn’t checking the field properly, or I have it misconfigured. Here is what my script looks like:

alias: TV Off
sequence:
  - service: script.turn_on_tv
    data: {}
  - service: script.turn_on_soundbar
    data: {}
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.media_state
            state: Playing a game
        sequence:
          - service: remote.send_command
            target:
              entity_id: remote.living_room_remote
            data:
              device: htpc
              command: power_off
  - service: input_text.set_value
    data:
      value: PowerOff
    target:
      entity_id: input_text.media_state
mode: single

TLDR: I only want to send the power_off command to the PC if it’s on (based upon the state of the text_input), otherwise (since it’s a power toggle and not a discreet “power_on” or “power_off” command) it turns the PC on if you tap the Lovalace “Power_off” button; this is what I’m trying to avoid.

From the script trace, it seems it’s ignoring the condition entirely every time it runs, and I’ve used the developer tools to check the state of the input_text value before and after it gets to the “choose” sequence, and that value is correctly updated.

I’ve even played with placement of the “choose” sequence (as in moving it first in the sequence, etc.) with no luck, so I have to be doing something wrong. Any insigts into where I should look?

Thanks in advance!

I might not be thinking straight, but why are you using choose if there’s only 1 option?

Just add a regular condition & action if this is indeed the case.

Also, to ensure that state: Playing a game is indeed correct, check the Dev tools for what is being reported when it’s in this actual state.

I originally tried an “if/then” in the script, but it did the same thing and basically stepped over the entire condition.

As a debug/test, I removed the entire

- service: input_text.set_value

sequence at the end so the text_input wouldn’t be updated, and in each case, the input_text.media_state value shows “Playing a game” in the developer_tools.

This also didn’t work:

tv_off:
  alias: TV Off
  sequence:
  - service: script.turn_on_tv
    data: {}
  - if:
    - condition: state
      entity_id: input_text.media_state
      state: Playing a game
    then:
    - service: remote.send_command
      metadata: {}
      data:
        device: htpc
        command: power_off
      target:
        entity_id: remote.living_room_remote
  - service: script.turn_on_soundbar
    data: {}
  mode: single

Same point applies with if/then. You only have 1 option, so using if/then or choose is not required.

Please read up on conditions and actions. You only need 1 condition (playing a game) and 2 actions (power off & set text value to power off).

Choose is used when you have multiple conditions, while you only have a single one

Ah! Let me dig in deeper in the docs, then.

I’m so used to conventional languages (I’m a Golang architect by profession) and need to break that conventional mindset.

1 Like

Try putting it in quotes. Are you sure there’s not a trailing space?

Perhaps use a template condition which you can also test in developer tools / templates.

{{ states('input_text.media_state') == 'Playing a game' }}

If that doesn’t work then the state isn’t set correctly.

There’s a shortcut to the docs for each section (trigger/condition/action, before they were renamed) right in the UI on the automation page. Click on the ? Icon on the right of each section & it’ll redirect you to the docs

ok, so I went back to the drawing-board and ended up with this (which now works successfully for either condition):

alias: TV Off
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.media_state
            state: Playing a game
        sequence:
          - service: remote.send_command
            target:
              entity_id: remote.living_room_remote
            data:
              device: htpc
              command: power_off
          - service: remote.send_command
            target:
              entity_id: remote.living_room_remote
            data:
              device: Sony Soundbar
              command: power
          - service: remote.send_command
            target:
              entity_id: remote.living_room_remote
            data:
              device: TV
              command: Power
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - service: input_text.set_value
            data:
              value: PowerOff
            target:
              entity_id: input_text.media_state
            enabled: true
      - conditions:
          - condition: state
            entity_id: input_text.media_state
            state: Watching TV
        sequence:
          - service: remote.send_command
            target:
              entity_id: remote.living_room_remote
            data:
              device: TV
              command: Power
          - service: remote.send_command
            target:
              entity_id: remote.living_room_remote
            data:
              device: Sony Soundbar
              command: power
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - service: input_text.set_value
            data:
              value: PowerOff
            target:
              entity_id: input_text.media_state
            enabled: true
mode: single
icon: mdi:power

Unless I completely misunderstood the docs, it looked to me that when using a script, the “condition” would stop the script from running unless that condition was met. So, I opted to use a “choose” in concert with those “condition/actions” and just set the tasks I needed for each choice in the “choose”. From what I understood, the only real other option was to use 2 automations (1 for TV, one for Gaming), which would bork my button and force having 2 buttons for powering off. Again, with more more conventional development tendencies to try and keep things “DRY” (Don’t Repeat Yourself), I was originally thinking that duplication the action tasks was a bit redundant, but it looks like in this case, I once again need to shed that mindset.

So, overall: Thank you all for the suggestions and guidance! I’ll be sure to more fully understand the docs before I start on my next project with HA!