nielsfaber / scheduler-component

Custom component for HA that enables the creation of scheduler entities
GNU General Public License v3.0
634 stars 41 forks source link

Error starting, "Task exception was never retrieved" #172

Closed xconverge closed 2 years ago

xconverge commented 2 years ago

Checklist

Expected behavior

Not error at startup

Actual behavior

Error doing job: Task exception was never retrieved

Traceback (most recent call last):
  File "/config/custom_components/scheduler/timer.py", line 108, in async_reload_data
    await self.async_start_timer()
  File "/config/custom_components/scheduler/timer.py", line 117, in async_start_timer
    [current_slot, timestamp_end] = self.current_timeslot()
  File "/config/custom_components/scheduler/timer.py", line 470, in current_timeslot
    (next_slot_end, val) = sorted(
IndexError: list index out of range

Steps to Reproduce

I am on the latest commit, 652e3e

nielsfaber commented 2 years ago

How can I reproduce this?

xconverge commented 2 years ago

I am not really sure, I rolled back to the previous commit (7575b89142a601229f4faa158633937b032d1fd8) and am good again.

Storage file still said version 2, so perhaps the change in the migration was causing a problem?

xconverge commented 2 years ago

I only have 1 schedule that has a condition, and I dont remember exactly what/why there is an "or" in there, this is the only thing I could see causing a problem with the related commit?

{
                "schedule_id": "ab6806",
                "timeslots": [
                    {
                        "start": "08:10:00",
                        "stop": null,
                        "conditions": [
                            {
                                "entity_id": "input_boolean.away_from_home",
                                "attribute": "state",
                                "value": "off",
                                "match_type": "is"
                            }
                        ],
                        "condition_type": "or",
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "switch.turn_on",
                                "entity_id": "switch.on_off_plug_in_unit_5",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "workday"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "repeat",
                "name": "",
                "enabled": true
            },
nielsfaber commented 2 years ago

Better to use a released version than latest commit. I still have to perform tests on the migration part before it is ready for use. However these changes should not impact timer behaviour, only affect conditions. Your timeslot configuration looks as basic as they get, so I don't see any clue for the error.

xconverge commented 2 years ago

Ok I will stick to released versions, I have a cron job that was pulling a few submodules for a few custom components but I will change it to use tagged releases for both this and the card

Figure this is the first time in months I have reached an issue so figure I would be able to give back some help before the release anyways 🤷

xconverge commented 2 years ago

Heres the full storage file incase something else pops out:

{
    "version": 2,
    "minor_version": 1,
    "key": "scheduler.storage",
    "data": {
        "schedules": [
            {
                "schedule_id": "ab6806",
                "timeslots": [
                    {
                        "start": "08:10:00",
                        "stop": null,
                        "conditions": [
                            {
                                "entity_id": "input_boolean.away_from_home",
                                "attribute": "state",
                                "value": "off",
                                "match_type": "is"
                            }
                        ],
                        "condition_type": "or",
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "switch.turn_on",
                                "entity_id": "switch.on_off_plug_in_unit_5",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "workday"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "repeat",
                "name": "",
                "enabled": true
            },
            {
                "schedule_id": "ac3ce8",
                "timeslots": [
                    {
                        "start": "08:40:00",
                        "stop": null,
                        "conditions": [],
                        "condition_type": null,
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "switch.turn_on",
                                "entity_id": "switch.on_off_plug_in_unit_2",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "daily"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "repeat",
                "name": null,
                "enabled": true
            },
            {
                "schedule_id": "ebeb14",
                "timeslots": [
                    {
                        "start": "22:30:00",
                        "stop": null,
                        "conditions": [],
                        "condition_type": null,
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "switch.turn_off",
                                "entity_id": "switch.on_off_plug_in_unit_2",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "daily"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "repeat",
                "name": null,
                "enabled": true
            },
            {
                "schedule_id": "d5a881",
                "timeslots": [
                    {
                        "start": "05:40:00",
                        "stop": null,
                        "conditions": [],
                        "condition_type": null,
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "switch.turn_on",
                                "entity_id": "switch.on_off_plug_in_unit_5",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "daily"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "pause",
                "name": "",
                "enabled": false
            },
            {
                "schedule_id": "ec6f66",
                "timeslots": [
                    {
                        "start": "22:00:00",
                        "stop": null,
                        "conditions": [],
                        "condition_type": null,
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "fan.turn_on",
                                "entity_id": "fan.core200s",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "daily"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "repeat",
                "name": "",
                "enabled": true
            },
            {
                "schedule_id": "07cce3",
                "timeslots": [
                    {
                        "start": "09:00:00",
                        "stop": null,
                        "conditions": [],
                        "condition_type": null,
                        "track_conditions": false,
                        "actions": [
                            {
                                "service": "fan.turn_off",
                                "entity_id": "fan.core200s",
                                "service_data": {}
                            }
                        ]
                    }
                ],
                "weekdays": [
                    "daily"
                ],
                "start_date": null,
                "end_date": null,
                "repeat_type": "repeat",
                "name": null,
                "enabled": true
            }
        ],
        "tags": []
    }
}
xconverge commented 2 years ago

Also just a heads up that it is 100% a problem with the migration commit to v3 of the storage file. I can flip back and forth between that commit and 3.2.6 over and over again and 3.2.6 works and the v3 storage file commit does not, and fails to upgrade I am guessing since I haven't made a backup/restore of my storage file and is staying at v3. I am guessing the log message is just a bit of a red herring, and there is an out of bounds access attempt in that migration code

Let me know if anything jumps out at you and I am happy to test any changes/print statements, etc you would want

nielsfaber commented 2 years ago

Thanks for updating me on this. I didn't have the time to investigate it further (the condition fix in the migration seems pretty straight-forward on the first look). I will try to reproduce this and tackle it, at least you helped preventing this problem from becoming part of a release đź‘Ť

xconverge commented 2 years ago

The migration code is stripping out all of my timeslot info. You tested the edge case (has_unequal_number_conditions returns true) and I am guessing you just need to test the normal-non edge case now when has_unequal_number_conditions returns false

I put a print at the beginning and the end of the "if old_version < 3:" block in store.py

2022-01-27 21:43:34 WARNING (MainThread) [custom_components.scheduler.store] data before
2022-01-27 21:43:34 WARNING (MainThread) [custom_components.scheduler.store] {'schedules': [{'schedule_id': 'ab6806', 'timeslots': [{'start': '08:10:00', 'stop': None, 'conditions': [{'entity_id': 'input_boolean.away_from_home', 'attribute': 'state', 'value': 'off', 'match_type': 'is'}], 'condition_type': 'or', 'track_conditions': False, 'actions': [{'service': 'switch.turn_on', 'entity_id': 'switch.on_off_plug_in_unit_5', 'service_data': {}}]}], 'weekdays': ['workday'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': '', 'enabled': True}, {'schedule_id': 'ac3ce8', 'timeslots': [{'start': '08:40:00', 'stop': None, 'conditions': [], 'condition_type': None, 'track_conditions': False, 'actions': [{'service': 'switch.turn_on', 'entity_id': 'switch.smart_plug_5', 'service_data': {}}]}], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': None, 'enabled': True}, {'schedule_id': 'ebeb14', 'timeslots': [{'start': '22:30:00', 'stop': None, 'conditions': [], 'condition_type': None, 'track_conditions': False, 'actions': [{'service': 'switch.turn_off', 'entity_id': 'switch.smart_plug_5', 'service_data': {}}]}], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': None, 'enabled': True}, {'schedule_id': 'd5a881', 'timeslots': [{'start': '05:40:00', 'stop': None, 'conditions': [], 'condition_type': None, 'track_conditions': False, 'actions': [{'service': 'switch.turn_on', 'entity_id': 'switch.on_off_plug_in_unit_5', 'service_data': {}}]}], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'pause', 'name': '', 'enabled': False}, {'schedule_id': 'ec6f66', 'timeslots': [{'start': '22:00:00', 'stop': None, 'conditions': [], 'condition_type': None, 'track_conditions': False, 'actions': [{'service': 'fan.turn_on', 'entity_id': 'fan.core200s', 'service_data': {}}]}], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': '', 'enabled': True}, {'schedule_id': '07cce3', 'timeslots': [{'start': '08:30:00', 'stop': None, 'conditions': [], 'condition_type': None, 'track_conditions': False, 'actions': [{'service': 'fan.turn_off', 'entity_id': 'fan.core200s', 'service_data': {}}]}], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': None, 'enabled': True}], 'tags': []}
2022-01-27 21:43:34 WARNING (MainThread) [custom_components.scheduler.store] data after
2022-01-27 21:43:34 WARNING (MainThread) [custom_components.scheduler.store] {'schedules': [{'schedule_id': 'ab6806', 'timeslots': [], 'weekdays': ['workday'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': '', 'enabled': True}, {'schedule_id': 'ac3ce8', 'timeslots': [], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': None, 'enabled': True}, {'schedule_id': 'ebeb14', 'timeslots': [], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': None, 'enabled': True}, {'schedule_id': 'd5a881', 'timeslots': [], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'pause', 'name': '', 'enabled': False}, {'schedule_id': 'ec6f66', 'timeslots': [], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': '', 'enabled': True}, {'schedule_id': '07cce3', 'timeslots': [], 'weekdays': ['daily'], 'start_date': None, 'end_date': None, 'repeat_type': 'repeat', 'name': None, 'enabled': True}], 'tags': []}

notice all of my 'timeslots' are now 'timeslots': [] after the migration attempt!

nielsfaber commented 2 years ago

It should be fixed by commit https://github.com/nielsfaber/scheduler-component/commit/c4b5c771468788c8108a65ce08eed925be461f95.

xconverge commented 2 years ago

Looks good to me, nice! I am not familiar but what will increment my scheduler.storage version from 2 to 3 permanently so that this code doesnt "need" to run at startup everytime, is that part of the custom integration or HA/storage callbacks?

Edit: I see now, any change that is made while on the new version will cause it to update.

nielsfaber commented 2 years ago

The migration function is executed every time HA restarts, but modifications are only made if the storage file has an older version, so normally it’s executed only once. The storage file is updated once the user makes a change to any schedule (migration itself doesn’t update the file). The storage file is always saved with the version defined in the code (in this case version=3).