Skip to content

Comparing Pyscript to Home Assistant Automations

Daniel Lashua edited this page Oct 16, 2020 · 9 revisions

Triggers

State Trigger

In Home Assistant:

- alias: some automation
  trigger:
    platform: state
    entity_id: binary_sensor.test
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.test

In Pyscript:

@state_trigger('binary_sensor.test == "on"')
def turn_on():
  homeassistant.turn_on(entity_id='switch.test')

State Trigger with "from"

In Home Assistant:

- alias: some automation
  trigger:
    platform: state
    entity_id: binary_sensor.test
    to: 'on'
    from: 'off'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.test

In Pyscript:

@state_trigger('binary_sensor.test == "on" and binary_sensor.test.old == "off"')
def turn_on():
  homeassistant.turn_on(entity_id='switch.test')

State Trigger without "to" or "from"

In Home Assistant:

- alias: some automation
  trigger:
    platform: state
    entity_id: binary_sensor.test
  action:
    - service: homeassistant.turn_on
      entity_id: switch.test

In Pyscript:

@state_trigger('True or binary_sensor.test')
def turn_on():
  homeassistant.turn_on(entity_id='switch.test')

Template Trigger

In Home Assistant:

- alias: some automation
  trigger:
    platform: template
    value_template: "{{ is_state('binary_sensor.test', 'on') }}"
  action:
    - service: homeassistant.turn_on
      entity_id: switch.test

In Pyscript:

@state_trigger('binary_sensor.test == "on"')
def turn_on():
  homeassistant.turn_on(entity_id='switch.test')

Conditions

Template Condition

In Home Assistant:

- alias: some automation
  trigger:
    platform: state
    entity_id: binary_sensor.test
    to: "on"
  condition:
    condition: template
    value_template: "{{ is_state('input_boolean.test', 'on') }}"
  action:
    - service: homeassistant.turn_on
      entity_id: switch.test

In Pyscript:

@state_trigger('binary_sensor.test == "on"')
@state_active('input_boolean.test == "on"')
def turn_on():
  homeassistant.turn_on(entity_id='switch.test')