custom-components / pyscript

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

Retreiving a comma separated list from a state variable attribute crashes the script #447

Closed dominig closed 1 year ago

dominig commented 1 year ago

As I need to store and share between several triggers a list of toogle (0/1), I tried to use a comma separated list that could easily be converted from a string to a list and vice versa. I have discovered (after a long try and error session) that I can store in state variable attribute a string (255 char max) only if that list is using space as a separator and not comma. I could not read my list from Pyscript if this one was comma separated. Using space separator works fine. I do not know if it's a bug or a constrains inherited from HACS but easer way it would be good to get it known.

The following python line radiator_actual_mode_state=state.get('pyscript.radiator_status.radiator_actual_mode') Will work fine is the attribute radiator_actual_mode' of the state variable pyscript.radiator_status is 0 0 0 0 0 0 and crashes it is 0,0,0,0,0,0 As shows the log extract below.

---------------------------  Log extract -------------------------------------
2023-03-10 20:26:45.604 DEBUG (MainThread) [custom_components.pyscript.eval] file.roundrobin.request_heater_off: calling str(<map object at 0xffff8cb6fd90>, {})
2023-03-10 20:26:45.604 DEBUG (MainThread) [custom_components.pyscript.eval] file.roundrobin.request_heater_off: calling info("get_status radiator_actual_mode_state=<map object at 0xffff8cb6fd90>", {})
2023-03-10 20:26:45.604 INFO (MainThread) [custom_components.pyscript.file.roundrobin.request_heater_off] get_status radiator_actual_mode_state=<map object at 0xffff8cb6fd90>
2023-03-10 20:26:45.604 DEBUG (MainThread) [custom_components.pyscript.eval] file.roundrobin.request_heater_off: calling split(",", {})
2023-03-10 20:26:45.604 DEBUG (MainThread) [custom_components.pyscript.eval] file.roundrobin.request_heater_off: calling map(<class 'int'>, ['<map object at 0xffff8cb6fd90>'], {})
2023-03-10 20:26:45.604 DEBUG (MainThread) [custom_components.pyscript.eval] file.roundrobin.request_heater_off: calling list(<map object at 0xffff8da671f0>, {} 
craigbarratt commented 1 year ago

HASS entities (state variables) can store strings with commas. It's likely your code has a bug. From the trace, you appear to be calling split() and map(). Please include your code and the actual error message.

dominig commented 1 year ago

I ended by using an array which has worked. I found out that side issues can easily be induced by persistant variable as there no clear method to change them during the development process. It might have been the origin of my issues. Removing the requirement of persistant data has fixed all issues. Now my code is fully operational. https://github.com/dominig/pyscript_power_load_balance