Button card on off call script

Hello everybody:
How can I configure a custom: button-card to execute a script according to the state of a media player?
If it is in standby mode call script on
If it is on call script off
Thanks

   - type: custom:button-card
     entity: media_player.playstation_4
     name: Playstation            
     action: call-service
      ??? service: automation.encender_ps4 or apagar_ps4

Scripts

  encender_ps4:
    alias: 'Encender PS4'
    sequence:
    - data:
        entity_id: media_player.playstation_4
      service: media_player.turn_on

  apagar_ps4:
    alias: 'Apagar PS4'
    sequence:
    - data:
        entity_id: media_player.playstation_4
      service: media_player.turn_off  

Hi! Can you give us more information regarding media_player.playstation_4 entity?

You can go to Developer Tools → States (Tab) → Find/Type “media_player.playstation_4” → There you will find state & state attribute.

You can copy and paste the available data there. It is important to know how we can get the data to know the status of “media_player.playstation_4”.

I don’t think you can. You can however make a script that does call on or the other depending on the state. Simply add a chooser to the script :slight_smile:

The state of the media player is standby or idle.
What I need to know is if I can at the moment of clicking on the card call a script or the other script

Sin embargo, puede hacer un script que llame a uno u otro dependiendo del estado

That is what I need. According to the idle or standby state that calls the script that turns on the playstation or the script that turns it off

What do you want to happen if the states is playing or off?

I want that if I click on the card button and media_player.playstation_4 is in standby mode, it calls the script that turns on the play.
If it is on idle and I press the button card, turn it off

Thanks

Hello! Can you try if this works for you? You need to use this YAML to create a script. Then, for your button card, you select this script as the target.

alias: Playstation Script
sequence:
  - service: >-
      {% if states('media_player.playstation_4') == 'standby' %}
        media_player.turn_on
      {% elif states('media_player.playstation_4') == 'idle' %}
        media_player.turn_off
      {% endif %}
    target:
      entity_id: media_player.playstation_4
mode: single

Below is the better approach by septillion-

alias: Playstation Script
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: media_player.playstation_4
            state: standby
        sequence:
          - service: media_player.turn_on
            target:
              entity_id: media_player.playstation_4
      - conditions:
          - condition: state
            entity_id: media_player.playstation_4
            state: idle
        sequence:
          - service: media_player.turn_off
            target:
              entity_id: media_player.playstation_4
    default: []
mode: single
1 Like

Excellent friend.
It worked perfect. That is what I needed.
Thank you very much for the help

1 Like

Does it work if the state of the Playstation is in “playing” or “off”?

Maybe an error shows up in the logs?

Works without errors

That’s basically what I told you :wink:

And yeah, that script will throw an error is the state isn’t standby or idle because it will try to call an empty service. So I would just throw everything in a chooser with an empty default

alias: Playstation Script
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: media_player.playstation_4
            state: standby
        sequence:
          - service: media_player.turn_on
            target:
              entity_id: media_player.playstation_4
      - conditions:
          - condition: state
            entity_id: media_player.playstation_4
            state: idle
        sequence:
          - service: media_player.turn_off
            target:
              entity_id: media_player.playstation_4
    default: []
mode: single
1 Like

OK, thanks a lot. I’m going to prove it.
I just tried it in standby mode. Don’t test it off.
In the event that the play is off, I don’t think I can turn it on. I have the installation supervised and I can’t get it to work GitHub - dhleong/ps4-waker: Wake your PS4 over LAN (with help from the Playstation App)

1 Like

Goodluck :slight_smile:

Hope you can get it working.

1 Like