Changing buttons based on the state

hi all so im fairly new to home assistant and was hoping someone can help me out.

i have 2 buttons to turn my pc on / off.
image

button on

type: vertical-stack
cards:
  type: custom:bubble-card
  card_type: button
  entity: switch.dean_pc
  icon: mdi:laptop
  show_state: false
  button_type: switch
  name: 'On'

button 2 (off)

type: vertical-stack
cards:
type: custom:bubble-card
card_type: button
entity: switch.dean_pc_dean_pc_shutdown
name: 'Off'
icon: mdi:laptop
show_state: false

but i was hoping to try and combine these buttons into 1 button that display changes depending on the computer state is on/off. ie if the pc is off it will show on the on button to allow me to press it to turn on and if its on it will show the off button so that i can turn it off

do you have an entity that reflects the state of the computer? your two switches are momentary? (ie, they are pushbutton and donā€™t retain the on/off state?)

to do this, youā€™ll need something that maintains on/off state.
if you donā€™t already have an existing entity for that , then you could create an input_boolean that toggles and when it turns on/off you call the appropriate switch (create a simple automation for that).

once you have that, you can easily assign it to one of many cards that will toggle state.

it all starts w/ having something that keeps state. have one already?

1 Like

just to add another option, if it works for your situation. you could use a template switch

# Example configuration.yaml entry
switch:
  - platform: template
    switches:
      skylight:
        value_template: "{{ is_state('sensor.skylight', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.skylight_open
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.skylight_close

Hi,

I think you are using a automation and script to add an action to the dashboard buttons?
On = Wake on LAN
Off = script to call windows shutdown

Does Home Assitant know the live status of your PC (On or Off)?
If not, I suggest to use a ping tool in Home Assistant to check the online status of your PC and build an automation based on the ping result to automate the button actions on your dashboard buttons.

Oh nice :slight_smile: didnā€™t saw you comment sorry

1 Like

note that this template switch still relies upon some entity that maintains stateā€¦

so question #1 is ā€œdo you already have something that maintains state?ā€

1 Like

in my config file i have so it seems to keep the state of the button when the computer is on

#Wake On LAN
switch:
  - platform: wake_on_lan
    mac: xx:xx:xx:xx:xx:xx
    name: ā€œDean-PCā€
    host: xxx.xxx.x.xxx

so iā€™m assuming id need a little statement like Frosty linked?

switch:
  - platform: wake_on_lan
    mac: xx:xx:xx:xx:xx:xx
    name: ā€œDean-PCā€
    host: xxx.xxx.x.xxx
  - platform: template
    switches:
      PC:
        value_template: "{{ is_state('cover.PC', 'on') }}"
        turn_on:
          service: cover.PC_ON
          target:
            entity_id: switch.dean_pc
        turn_off:
          service: cover.PC_OFF
          target:
            entity_id: switch.dean_pc_dean_pc_shutdown
        icon_template: >-
          {% if is_state('cover.PC_ON', 'on') %}
            mdi:laptop-account
          {% else %}
            mdi:laptop-off
          {% endif %}

huhā€¦ so you have a swtich that reflects stateā€¦ if thatā€™s the case ā€¦ ignoring how nice it looks does this work as a single button for you (use this in your lovelace dashboard card

but first make sure that your switch.PC does actually reflect state.
turn the pc on (not using HA) and see if switch.PC is on.
turn the pc off (not using HA) and see if switch.PC is off.

try this also looking at your switch.Dean-PC (that entity exists for you right?)

if so, then you can do:

show_name: true
show_icon: true
type: button
tap_action:
  action: toggle
entity: switch.PC

i donā€™t use the custom:bubble card, but iā€™m guessing that this might work:

type: vertical-stack
cards:
  type: custom:bubble-card
  card_type: button
  entity: switch.PC
  icon: mdi:laptop
  show_state: true
  button_type: switch

however since it says itā€™s a ā€œwake_on_lanā€ switch, iā€™m honestly still a little dubious that it reflects stateā€¦ try the test above and seeā€¦

1 Like

so i turned my pc off,
this button you made didnā€™t do anything

when i click on it, it just says

Service cover.dean_pc called service switch.turn_on which was not found.

this button give me a error saying

Failed to call service homeassistant/toggle. Entity ID is an invalid entity ID for dictionary value @ data[ā€˜entity_idā€™]

the 2 bubble buttons i had from before changed though

off state

on state
image

i think i jumped too far forward too fastā€¦ my apologiesā€¦

let me go step by step.

  1. you currently already have this, right?
#Wake On LAN
switch:
  - platform: wake_on_lan
    mac: xx:xx:xx:xx:xx:xx
    name: ā€œDean-PCā€
    host: xxx.xxx.x.xxx

open that up and see the state (you can click on it in your entities page.
turn the computer on and off and see if that state changes.

  1. you posted this yaml:
switch:
  - platform: wake_on_lan
    mac: xx:xx:xx:xx:xx:xx
    name: ā€œDean-PCā€
    host: xxx.xxx.x.xxx
  - platform: template
    switches:
      PC:
        value_template: "{{ is_state('cover.PC', 'on') }}"
        turn_on:
          service: cover.PC_ON
          target:
            entity_id: switch.dean_pc
        turn_off:
          service: cover.PC_OFF
          target:
            entity_id: switch.dean_pc_dean_pc_shutdown
        icon_template: >-
          {% if is_state('cover.PC_ON', 'on') %}
            mdi:laptop-account
          {% else %}
            mdi:laptop-off
          {% endif %}

this indicates to me that you have an entity called cover.PC

do you? this block of code depends upon having cover.PC

  1. it also references cover.PC_ON and cover.PC_OFFā€¦ is that real? or did you make that up? that seems wrong to me.

  2. i think we need to go back to my very first question. do you have any entity in home assistant that currently reflects the state of your PC?

1 Like

1 - correct
it shows on in the entities page while computer is on.

and shows off when the pc is turned off

2/3 - no i donā€™t have a entity called cover.pc
it was more or less a guess on what i needed to put in from the template switch page. but i didnā€™t think it was correct.

4 - the only entities on my HA i have for my pc is switch.dean-pc & the hass agent switch to turn it off switch.switch.dean_pc_dean_pc_shutdown and the only yaml i have in my config file is what i have in 1.

sorry i suck at this :frowning:

  1. great. thatā€™s a good sign! :slight_smile: if you toggle that buttonā€¦ does it do anything?

nope, doesnā€™t do anything when clicking the toggle

okā€¦ since you have that entity then iā€™d go with @Frosty 's pathā€¦ you didnā€™t have all the specific names so youā€™ve got to tweak thisā€¦

switch:
  - platform: template
    switches:
      PC:
        value_template: "{{ is_state('switch.<your_correct_Dean-PC_entity_name>', 'on') }}"
        turn_on:
          service: switch.toggle/turn_on/turn_off
          target:
            entity_id: switch.dean_pc
        turn_off:
          service: switch.toggle/turn_on/turn_off   
          target:
            entity_id: switch.dean_pc_dean_pc_shutdown

for the <your_current_Dean-PC_entity_name> get the correct entity name by hitting this upper right gear icon:

and for the switch.toggle/turn_on/turn_off, you need to get the right callā€¦ it should either be switch.toggle, or switch.turn_on or switch.turn_off

get the right one for turning on, and get the right one for turning off (they might not be the same).

if you dontā€™ know, trial and error is fineā€¦

add the above yaml to your configuration.yaml
restart home assistant !!! (donā€™t forget to do this)

then try this front end config again:

show_name: true
show_icon: true
type: button
tap_action:
  action: toggle
entity: switch.PC

i try your suggestions but it wouldnā€™t work. so i thought of a different way to fix the issue, using css instead to hide/show the buttons from the state of my pc.

type: vertical-stack
cards:
  - type: custom:bubble-card
    card_type: button
    entity: switch.dean_pc
    name: Power On
    icon: mdi:laptop
    show_state: false
    styles: |
      ha-card {
        display: ${state === 'off' ? 'block' : 'none'} !important;
      }
      .switch-button {
        background-color: #37db53 !important;
        color: #282828;
      }
      .button-container {
        border: 3px solid #37db53;
      }
      .icon-container .icon {
        color: unset !important; 
      }
      .icon-container .icon {
        color: #37db53 !important;
      }
  - type: custom:bubble-card
    card_type: button
    entity: switch.dean_pc_shutdown
    name: Shutdown
    icon: mdi:laptop-off
    show_state: false
    styles: |
      ha-card {
        display: ${state === 'off' ? 'block' : 'none'} !important;
      }
      .switch-button {
        background-color: #db4437 !important; 
      }
      .button-container {
        border: 3px solid #db4437;
      }
      .icon-container .icon {
        color: unset !important; 
      }
      .icon-container .icon {
        color: #db4437 !important;
      }

thereā€™s a small delay when the computer powers on between the on button disappearing and the off button appearing but it works.

1 Like