custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
874 stars 46 forks source link

state_trigger triggering for non-acceptable value #480

Open dy-nz opened 1 year ago

dy-nz commented 1 year ago

I have a triggers, for example: @state_trigger("input_select.zm_media_source in ['Cast','Cast HD','Server','Freeview','Phono','PS3']")

Its function has the kwarg variable, "value", which would normally be any of the values listed above. But sometimes the function is triggered by the kwarg value==" ", which is listed in the config.yaml definition for input_select.zm_media_source, but not in the state_trigger (above).

I have tried re-doing the state_trigger as a series of '==' conditions, but still had the same problem.

My work-around has been to also specify the trigger condition(s) as an if statement in the function, and "return None" for a non-acceptable value.

craigbarratt commented 1 year ago

Does that function have other @state_trigger decorators, or is that the only one?

Two things to try is to set the debug level to debug and look at the log messages, and to also log the value of the var_name parameter.

dy-nz commented 1 year ago

Hi Craig, still testing. But my environment is now ok. Current environment is one function with one @state_trigger decorator using the two state variables in the form of a condition. (For the sake of brevity I've left out a few dictionary items. Note: I have two zones 'zm' and 'zk'.) The state variables aren't used in any other @state_trigger decorator -

@state_trigger("input_select.zm_media_source in ['All Off','Off','Cast','Phono'] or input_select.zk_media_source in ['All Off','Off','Cast','Phono']") def play(var_name=None, value=None, old_value=None):

My errored .py file used the state variables in three different @state_trigger decorators/conditions -

@state_trigger("input_select.zm_media_source in ['Cast','Phono'] or input_select.zk_media_source in ['Cast','Phono']") def play(var_name=None, value=None, old_value=None):

@state_trigger("input_select.zm_media_source == 'Off' or input_select.zk_media_source == 'Off'") def zone_off(var_name=None, value=None, old_value=None):

@state_trigger("input_select.zm_media_source == 'All Off' or input_select.zk_media_source == 'All Off'") def all_off(var_name=None, value=None, old_value=None):

I will post again once I've finished my testing.

Thanks, David

dy-nz commented 1 year ago

Hi Craig, sorry to have wasted your time. All tested ok. All I can think that I would have done was use '=' in the @state_trigger decorator instead of '==' for the conditions.

dy-nz commented 1 year ago

Hi Craig, Reopened. I managed to cause the error. Refer to my post two before this - it refers to: def play.

I triggered "play" (Phono) for both zones, one after the after. I then triggered "All Off" for the (2nd) zk zone. This caused the error I described earlier - the "All Off" debug starts on line 865. home-assistant.log cmds.py.txt (PS apologies for not yet replacing sync telnet with async. Bit more work to do. Interim solution is to open telnet, issue one or two commands for max 6 seconds, then close telnet.)

Thanks again.