nielsfaber / scheduler-component

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

Ensure scheduler state (eg. manual run with checking time and conditions) #369

Open fpytloun opened 4 months ago

fpytloun commented 4 months ago

Checklist

Proposal

If there's some issue with entities scheduler is calling, there is no simple way to find out or fix the state. Eg. if scheduler triggers a light off but it remains on for any reason. Possible solutions:

  1. I would expect repeat_type to repeat actions, so basically retry, but it does not seem to happen or there's some longer period, not able to configure. UPDATE: based on this, repeat happens every 10 minutes https://github.com/nielsfaber/scheduler-component/blob/061d882f686083cedf1ea4a50333ca030f305840/custom_components/scheduler/switch.py#L181
  2. Have automation with scheduler.run_action to re-try actions but it will skip time checks so it also does not solve the issue.

Ideal solution would be to have action eg. scheduler.ensure_state or it would probably be same as what would happen after scheduler.edit as I think scheduler fires if updated 🤔

Additional info

[some example of how the new functionality should look like]

nielsfaber commented 4 months ago

If there's some issue with entities scheduler is calling, there is no simple way to find out or fix the state. Eg. if scheduler triggers a light off but it remains on for any reason.

Scheduler checks if an entity is unavailable, if this is the case the action is postponed until the entity becomes available, otherwise the action is executed directly. There is a maximum applicable to the amount of time that can be postponed (for a schedule using timeslots without start+end time), this could be the 9 minutes you are referring to.

I would expect repeat_type to repeat actions

No, this property relates to the repetition of the schedule itself, so whether a schedule should loop to the next day after ending, it has nothing to do with execution of actions.

Ideal solution would be to have action eg. scheduler.ensure_state

No idea what you mean by this.

Note that scheduler is only designed to execute a given action at given time and day. If an entity changes its state afterwards, this is not detected nor reacted upon. The behaviour you can expect from scheduler is equivalent of creating an automation that is triggered by time.

pdcastro commented 4 months ago

Ideal solution would be to have action eg. scheduler.ensure_state

No idea what you mean by this.

I may have some idea. Some definitions to make the discussion more precise:

My impression of the meaning of an “ensure state” service is that, given a Controlled Entity ID, it would compare the current actual state of the Controlled Entity with its Implied State as defined above, and if these states differed, an action would be executed to change the current actual state so that it matches the Implied State. In other words: If a light switch was supposed to be turned on at 8pm and turned off at 10pm, and now it’s 9pm and the light switch is turned off, then turn it on!

If so, it’s something that in the past I had myself wished, not in the context of an unavailable entity, but in the context of the home assistant server being “down for maintenance” for a period of time. When the server was up again, such an “ensure state” service could “correct” the state of entities for which scheduled actions were missed while the server was down.

A less ambitious, perhaps pre-requisite feature would be a “scheduler.get_implied_state” service that, given a Controlled Entity ID such as “switch.bedroom” and a time such as “9pm”, would return the Controlled Entity’s Implied State such as “turned on”. A user could then setup an automation that ran periodically to compare a Controlled Entity’s current state with its Implied State, correcting the current state as needed.

fpytloun commented 4 months ago

Scheduler checks if an entity is unavailable, if this is the case the action is postponed until the entity becomes available, otherwise the action is executed directly.

So this will be probably my issue. Because some entities are not unavailable but changing it's state might not succeed (in my case Z-Wave devices). So one addition might be to wait a while and check if state was actually changed and if not then retry.

But what I wanted was to have option to know if actual state is matching scheduled state (eg. by having some sensor or attribute on scheduler entities) and option to enforce scheduled state (by service similar to scheduler.run_action but that will respect all scheduler settings).

Floyd-Schramm commented 3 months ago

+1 on this feature.

In my case, it would be nice to be sure a switch changed states successfully. I've got a schedule for switching on and off our pool pump to circulate the water. On hot summer days, this is really important, so no part of the schedule should be missed. The pump is controlled by a Zigbee smart plug, which will be switched by the scheduler. If there is an error in the Zigbee network and the packet gets lost, or a Timeout occurs, the smart plug is not switched and continues in the same state as before.

So it would be handy to have the scheduler being able to check if the state change really happened and, if not, try to switch the entity again. I would go even further and implement a notification feature, so the user could be notified if:

  1. The entity change was not successful the first time, but it was able to switch the entity by the x-th try
  2. The entity change was not successful after x tries

The timeline should look like this: image

github-actions[bot] commented 2 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days

fpytloun commented 2 months ago

Bump

xThrodx commented 2 months ago

Hi, I have the same problem with an on-off Zigbee switch. The entity is never unavailabe in Home Assistant, but sometimes the switch does not respond to the "turn on" or "turn off" action and stays in the same state as before.

In my case I use the scheduler to run some appliances at night, when electricity is cheaper, but yesterday the appliance did not turn off in the morning.

nielsfaber commented 2 months ago

To summarize (if I understand correctly), the feature requested here is: after scheduler has triggered an action, a check if performed whether the targeted entity indeed changes state. If not, some retry is performed.

I understand this can be desirable in some cases, however I'm reluctant such complexity should be added to the scheduler code. The main issue I have with this request is the problem caused by the device being poor, not by scheduler. The problem will also exist with a regular automation or even manual controlled action, so why should the solution be provided specifically by scheduler? I feel more for having a generic solution provided by HA, e.g. by additional parameters that can be provided to a service/action call, that will trigger internal check&retry mechanisms.

Some other concerns I have:

pdcastro commented 2 months ago

I was now re-reading the scheduler’s README and it gives me the impression that the “ensure state” feature requested here has nearly been implemented already, in the form of tracked conditions — the track_conditions boolean attribute of the Timeslot object.

Consider a switch controlled by the scheduler. The idea is to setup Timeslots where the tracked condition is the state of the controlled switch itself:

Wouldn’t this substantially deliver the state diagram shared by @Floyd-Schramm, and the action retry originally asked by @fpytloun? Instead of “wait 1-2 minutes” and “try up to 5 times”, the above tracked conditions would trigger whenever the switch state changed away from what it should be, an unlimited number of times within each time slot.

However, after some brief playing with the scheduler card UI, if I got it right, setting up the tracked conditions above is not currently possible because:

New schedule Track conditions

 

But if we established that these were “merely” UI limitations, the focus could shift to tweaking the UI. (I’d have some further comments in that case...)

Hint to help understanding what's going on behind the scenes: Check the contents of your Home Assistant config/.storage/scheduler.storage JSON file after you make changes through the scheduler card UI.

Regarding @xThrodx’s Zigbee switch that “sometimes does not respond to the "turn on" or "turn off" action and stays in the same state as before,” I suspect that a tracked condition would not trigger in the absence of a state change (e.g. in the case where the switch state remained ‘off’ after a ’turn on’ action was executed). But we could consider enhancing the tracked conditions feature. The Condition object could have a new attribute evaluation_interval_sec that would be an integer number of seconds after which the condition would be re-evaluated, repeatedly. For example, if evaluation_interval_sec was 60, then every 60 seconds the condition would be re-evaluated; if the condition was true (e.g. switch state is not ‘on’), then the Timeslot action would be executed again (e.g. switch turn on).

pdcastro commented 2 months ago

[...] I understand this can be desirable in some cases, however I'm reluctant such complexity should be added to the scheduler code.

That’s fair. Short of the scheduler itself “ensuring the state,” the scheduler could make it easier for users to write their own automations or scripts, as follows. A common pattern in such automations or scripts is to repeatedly perform the following steps:

  1. Get the target state: Should the switch be ‘on’ or ‘off’ at this time?
  2. Get the current state: Is the switch ‘on’ or ‘off’ at this time?
  3. If the current and the target states don’t match (e.g. should be ‘on’ but is ‘off’), execute a corrective action (e.g. turn it ‘on’).
  4. Repeat the previous steps (e.g. every 2 minutes).

Say the user is using the scheduler to control a switch, e.g. turn ‘on’ at 8pm and turn ‘off’ at 10pm. The user would be doing this because the scheduler card UI is so nice and powerful! 😍 But the switch is not reliable 😣 and the user decides to complement the scheduler with an automation or script coded as above. It turns out that the trickiest part of such an automation or script is the first step: Should the switch be ‘on’ or ‘off’ at the time when the automation is executed? The scheduler has the data to answer this question. If the switch was scheduled to turn on at 8pm and turn off at 10pm, then the answer would be ‘on’ at 8:33pm, or ‘off’ at 10:17pm.

[...] _Scheduler has no knowledge of what state change is to be expected after triggering an action. For example, calling service climate.settemperature may result into the climate entity starting to heat (or not), it depends on its internal logic. There are also various entity types that don't have any state (e.g. button, script, notify).

An automation/script could figure out the required state if a scheduler service returned previous and next Timeslot action lists. For example, given the following excerpt from my config/.storage/scheduler.storage file:

  "data": {
    "schedules": [
      {
        "schedule_id": "c1b0f7",
        "timeslots": [
          {
            "start": "05:30:00",
            "stop": null,
            "conditions": [],
            "condition_type": null,
            "track_conditions": false,
            "actions": [
              {
                "service": "climate.set_temperature",
                "entity_id": "climate.living_room",
                "service_data": {
                  "hvac_mode": "heat",
                  "temperature": 18.5
                }
              }
            ]
          }
        ],
      ...
      },
      {
        "schedule_id": "b29c0f",
        "timeslots": [
          {
            "start": "07:00:00",
            "stop": null,
            "conditions": [],
            "condition_type": null,
            "track_conditions": false,
            "actions": [
              {
                "service": "climate.set_temperature",
                "entity_id": "climate.living_room",
                "service_data": {
                  "hvac_mode": "heat",
                  "temperature": 19.5
                }
              }
            ]
          }
        ],
      ...

The scheduler component could provide a scheduler.get_actions service that, given the following input (timestamp provided in ISO format):

{ "entity_id": "climate.living_room",
  "timestamp": "2024-09-22T05:37:08",
}

... produced the following output:

{
  "previous": {
    "start": "05:30:00",
    "stop": null,
    "actions": [
      {
        "service": "climate.set_temperature",
        "entity_id": "climate.living_room",
        "service_data": {
          "hvac_mode": "heat",
          "temperature": 18.5
        }
      }
    ]
  },
  "next": {
    "start": "07:00:00",
    "stop": null,
    "actions": [
      {
        "service": "climate.set_temperature",
        "entity_id": "climate.living_room",
        "service_data": {
          "hvac_mode": "heat",
          "temperature": 19.5
        }
      }
    ]
  }
}

Given the above output, the automation or script could figure out that the target temperature state is 18.5 at 05:37 (and since 05:30) and expected to next change to 19.5 at 07:00. Similarly, an automation or script could figure that a target switch state is ‘on’ if the previous action list included "service": "switch.turn_on".

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days

fpytloun commented 1 month ago

Not stalled.

fahlsen commented 3 weeks ago

My schedule do not run at all when I add a condition.

I added the following condition based on a calendar:

Condition = IS

STATE ON

I checked that this condistion was met on the time the schedule was supposed to run, and it was. If I remove the condition, the schedule runs fine.