Help with Script checking state before triggering

Thanks treno.

I created separate scripts to check status of the IR devices:

############################################################################
#
# Check TV State And Toggle
#
############################################################################
        
tv_state_on:
  alias: "State Turn TV On"
  sequence:
    - condition: state
      entity_id: switch.tv_power
      state: 'off'
    - service: homeassistant.turn_on
      data:
        entity_id: switch.tv_power
tv_state_off:
  alias: "State Turn TV Off"
  sequence:
    - condition: state
      entity_id: switch.tv_power
      state: 'on'
    - service: homeassistant.turn_off
      data:
        entity_id: switch.tv_power
        
############################################################################
#
# Check AMP State And Toggle
#
############################################################################

amp_state_on:
  alias: "State Turn AMP On"
  sequence:
    - condition: state
      entity_id: switch.amp_power
      state: 'off'
    - service: homeassistant.turn_on
      data:
        entity_id: switch.amp_power
amp_state_off:
  alias: "State Turn AMP Off"
  sequence:
    - condition: state
      entity_id: switch.amp_power
      state: 'on'
    - service: homeassistant.turn_off
      data:
        entity_id: switch.amp_power

############################################################################
#
# Check RGB State And Toggle
#
############################################################################

rgb_state_on:
  alias: "State Turn RGB On"
  sequence:
    - condition: state
      entity_id: switch.rgb_power
      state: 'off'
    - service: homeassistant.turn_on
      data:
        entity_id: switch.rgb_power
rgb_state_off:
  alias: "State Turn RGB Off"
  sequence:
    - condition: state
      entity_id: switch.rgb_power
      state: 'on'
    - service: homeassistant.turn_off
      data:
        entity_id: switch.rgb_power

And then these scripts run above and toggle relevant devices.

############################################################################
#
# Movie Time 
#
############################################################################

movie_time:
  alias: "Movie Time"
  sequence:
    - service: script.tv_state_on
    - service: script.amp_state_on
    - service: script.amp_input_tv

############################################################################
#
# Bed Time 
#
############################################################################

bed_time:
  alias: "Bed Time"
  sequence:
    - service: script.tv_state_off
    - service: script.amp_state_off
    - service: homeassistant.turn_off
      data:
        entity_id: switch.pc_lamp
    - service: homeassistant.turn_off
      data:
        entity_id: switch.pc_screen

Still interested to see if there’s a more eloquent way of doing this, but otherwise this works for now.

1 Like