-
Notifications
You must be signed in to change notification settings - Fork 47
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
Bug with multiple state-triggers #157
Comments
This is correct behavior, even if unexpected.
There are various solutions:
|
These are three separate triggers: Light should start when either the sensors detect motion or when I come home. I mean, if I'd like to evaluate all my triggers in one expression, I would write them as one expression. I thought that was the benefit of offering multiple parameters to state_trigger... |
Hmm, good point. Perhaps they should be implemented as separate triggers, although that would be a breaking change... |
You could allow the use of multiple state-trigger-decorators for one function. This would be backwards compatible and very flexible. I don't know if python has any restrictions on that...(?) |
I just pushed a change 7171c4f that allows multiple state/time/event/mqtt trigger decorators per function. I agree that was the cleanest way to support multiple independent triggers, and it allows optional parameters like Each |
That sounds good. Let me introduce a further idea. What do you think of the possibility to pass a specific payload to the trigger-definition that will be passed as parameter to the function. See the example: @state_trigger("binary_sensor.bad_bewegung=='on'", data=240)
@state_trigger("binary_sensor.wohnzimmer_bewegung='on'", data=120)
@state_trigger("person.rico == 'home'", data=720)
def nightligh(data):
# ...
task.sleep(data)
#... In this case the payload would be the amount of seconds the nightlight should stay on; but as arbitrary object it would pose an easy way to pass trigger-specific data to the function. It would need to be implemented on every type of trigger, but in my eyes that could be a very useful feature. |
If this "payload" feature is going to happen, I think it would be best to either use ONE kwarg named the same as the function signature (i.e. call it That being said, I find it just as clean to do this:
Alternately, if
|
@dlashua: I first used the name payload; then found out, that mqtt-trigger uses this name already and switched to data. As you found out, I forgot to rename one occurrence. The solution with 3 trigger-functions and one parametrized main funnction is my current implementation. But I'd say, that a version with one function, multiple triggers and a payload per trigger would be more readable... |
The trigger function already gets called with several keyword arguments, including the entity name whose change caused the trigger to become True, and its prior and new values. That should allow it to figure out what caused the trigger. That said, I agree it could be helpful to allow trigger-dependent arguments to be passed to the trigger function. Perhaps optional keyword arguments |
Yes, this sounds ideal. |
That sounds like a great way to increase the flexibility without any backwards compatibility issues... |
…t that can be set to a dict of keyword/value paris that are passed to the trigger function; see #157.
Implemented |
I don't know, if we should check for naming collisions between kwargs and regular transmitted arguments of the trigger. If I would pass kwargs={"payload": "1"} for an mqtt_trigger which also creates a payload-kwarg -> will one be preferred or does it result in an error? |
The optional |
Ah yes, I just found it in the modified docs, commendable :-) |
Question: Will you put these changes into a new release soon? |
Hopefully I'll do a new release in the next week or so. |
I just tried to use multiple state triggers as described with one decorator:
@state_trigger("binary_sensor.bad_bewegung=='on'","binary_sensor.wohnzimmer_bewegung=='on'", "person.rico == 'home'")
But, as long as the third trigger is included (which is always true, but should not trigger, because it does not change), the function triggers on every binary_sensors change, not only when changing to "on" (as stated)
binary_sensor.wohnzimmer_bewegung: {'trigger_type': 'state', 'value': 'on', 'old_value': 'off', 'context': Context(user_id=None, parent_id=None, id='466747cb06fdd2a162b1813d5e60dcce')}
binary_sensor.wohnzimmer_bewegung: {'trigger_type': 'state', 'value': 'off', 'old_value': 'on', 'context': Context(user_id=None, parent_id=None, id='24eeef4f530f9b24657ad7b4205d3c32')}
The text was updated successfully, but these errors were encountered: