Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roughwood opened this issue Jul 10, 2024 · 1 comment
Open

Comments

@roughwood
Copy link

roughwood commented Jul 10, 2024

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?

@ALERTua
Copy link
Contributor

ALERTua commented Jul 11, 2024

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants