The trouble with Home Assistant “terms” is that they don’t always work outside of home assistant. Like the word “template” in “template trigger”. There is no “template” in pyscript’s version of a “template trigger”.
In pyscript, you have a piece of code like this:
binary_sensor.test == 'on'
This is an expression in Python. It will evaluate to “True” or “False”, a lot like the template part of a template trigger. You can use that code anywhere, including in a state_trigger
. But it could be anywhere. For instance, say you wanted to turn on a light when you see motion but only when it’s dark. Just like in a Home Assistant Automation, there are MANY different ways to do this in pyscript. But here’s one:
@state_trigger('binary_sensor.motion == "on"')
def turn_on_if_dark():
if binary_sensor.dark == 'on':
light.turn_on(entity_id='light.front_porch')
But, like I said, there are many ways to do the same thing, just like in Home Assistant. So you could also write it like this:
@state_trigger('binary_sensor.motion == "on" and binary_sensor.dark == "on"')
def turn_on_if_dark():
light.turn_on(entity_id='light.front_porch')
Of course, just like in Home Assistant Automation, these two ARE slightly different. In the second version, if there is motion and THEN it gets dark while the motion is still happening, the light will still turn on. In the first example, if it’s not already dark when the motion starts, then the light will not turn on.
Or maybe you just want the light on when it’s dark, regardless of motion:
@state_trigger('binary_sensor.dark == "on"')
def turn_on_if_dark():
light.turn_on(entity_id='light.front_porch')
Pyscript handles template triggers, state triggers, and numeric state triggers all with @state_trigger
. Event triggers are handled with @event_trigger
and any of the time based triggers (like the sun trigger, time trigger, and time_pattern trigger) are handled with @time_trigger
.
The “action” part of your home assistant automation is what follow the def whatever():
portion in pyscript. This is just a python function that will be run when the trigger happens.
Of course, you can do some fairly complex things pretty easily when you don’t have to write it in YAML.
Here’s a real world example (i.e. I actually use this) of a pyscript automation. It adjusts an input_number representing an away temperature based on an input_number that is the desired temperature as well as the distance away from my home that the closest person is. The idea is, the farther away from home we are, the hotter/colder we can adjust the temperature since, as we drive closer the temperature will adjust back down and be exactly what we want when we finally arrive at home.
@state_trigger('True or climate.downstairs or proximity.all or input_number.desired_temperature or input_number.away_temperature')
@time_trigger('startup')
def climate_away_diff():
desired_temp_entity = 'input_number.desired_temperature'
set_entity = 'input_number.away_temperature'
cool_base = 3
heat_base = -4
max_diff = 10
min_diff = -10
cool_coeff = (1 / 10)
heat_coeff = (-1 / 10)
distance = float(proximity.all)
desired_temp = float(state.get(desired_temp_entity))
away_temp = float(state.get(set_entity))
climate_state = climate.downstairs
if climate_state == 'cool':
diff = cool_base + (cool_coeff * distance)
elif climate_state == 'heat':
diff = heat_base + (heat_coeff * distance)
else:
diff = 0
if diff > max_diff:
diff = max_diff
if diff < min_diff:
diff = min_diff
new_temp = desired_temp + round(diff)
if new_temp == away_temp:
log.debug(f'Away Temp already set to {new_temp}')
return
log.info(f'Setting Away Temp to {new_temp}')
input_number.set_value(
entity_id = set_entity,
value = new_temp
)
I could try to write this in a Home Assistant Automation, but it would require some painful amounts of template code that just isn’t fun to read or write. But, if you’re having a hard time understanding what it’s doing, I’d be happy to try writing the Home Assistant equivalent so you can see the difference.