pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

Is it possible to use multiline strings in state trigger decorators?

Open roughwood opened this issue 1 year ago • 1 comments

As an example:

_state_sensor = 'sensor.office_state'
@state_trigger(f"""
{_state_sensor} in ['recently occupied', 'unoccupied', 'dormant', 'unavailable'] and 
(sensor.octoprint_actual_tool0_temp == 'unavailable' or float(sensor.octoprint_actual_tool0_temp) <= 110) and 
(sensor.octoprint_actual_bed_temp == 'unavailable' or float(sensor.octoprint_actual_bed_temp) <= 110)
""", state_hold_false=0)
def should_turn_off():
    _light_off()

This does not seem to work at all, which is pretty disappointing. Am I doing something wrong?

roughwood avatar Jul 10 '24 23:07 roughwood

Remove the starting space, as from my tests it is the main cause. Also, if you want your complex triggers to be more readable, try playing with formatting instead:

@state_trigger(
    f"{LAUNDRY_VENTS} == 'off'"
    f" and {entity_exists(LAUNDRY_TEMPERATURE)}"
    f" and ( "
    f"  float_({LAUNDRY_TEMPERATURE}) <= {TEMPERATURE_LOW} "
    f"  or float_({LAUNDRY_TEMPERATURE}) > 30"
    f" )",
    watch=[LAUNDRY_TEMPERATURE],
    state_hold=HOLD_2H,
    kwargs={
        'disable_notification': True,
        'target': TELEGRAM_CHAT_ALERT_HA,
    },
)

Don't mind the constants in this example, just the formatting. For you it's

_state_sensor = 'sensor.office_state'
@state_trigger(
    f"{_state_sensor} in ['recently occupied', 'unoccupied', 'dormant', 'unavailable']"
    f" and (sensor.octoprint_actual_tool0_temp == 'unavailable' or float(sensor.octoprint_actual_tool0_temp) <= 110)"
    f" and (sensor.octoprint_actual_bed_temp == 'unavailable' or float(sensor.octoprint_actual_bed_temp) <= 110)", 
    state_hold_false=0
)
def should_turn_off():
    _light_off()

or

_state_sensor = 'sensor.office_state'
@state_trigger(
    f"{_state_sensor} in ['recently occupied', 'unoccupied', 'dormant', 'unavailable']"
    f" and ("
    f"   sensor.octoprint_actual_tool0_temp == 'unavailable'"
    f"   or float(sensor.octoprint_actual_tool0_temp) <= 110"
    f" )"
    f" and ("
    f"  sensor.octoprint_actual_bed_temp == 'unavailable'"
    f"  or float(sensor.octoprint_actual_bed_temp) <= 110"
    f" )",
    state_hold_false=0
)
def should_turn_off():
    _light_off()

ALERTua avatar Jul 11 '24 07:07 ALERTua