Automation Assistance Required

Hi Everyone

I have an automation that shuts down my Plex Server and NAS at 22:05 each evening, unless there is a stream active. Option 3 of the Automation is supposed to check between 22:05 - 00:00 and if not stream is active for 15 minutes then shutdown. I can not get this last past to work. Could someone point out the errors of my way? :slight_smile:

description: ""
triggers:
  - at: "22:05:00"
    id: Shut Down
    trigger: time
  - trigger: state
    entity_id:
      - sensor.plex_media_server
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: No Activity
    to: "0"
  - trigger: webhook
    allowed_methods:
      - POST
      - PUT
      - HEAD
      - GET
    local_only: false
    webhook_id: watch-plex-activity-shutdown-xxxxxxxxxxxxxxxxxxxxxxxx
    id: Webhook
conditions:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: "off"
    enabled: true
  - condition: state
    entity_id: input_boolean.guest_mode
    state: "off"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Shut Down
              - Webhook
          - condition: numeric_state
            entity_id: sensor.plex_media_server
            below: 1
        sequence:
          - data: {}
            action: shell_command.nas_shutdown
          - data: {}
            action: rest_command.nuc_shutdown
      - conditions:
          - condition: trigger
            id:
              - Shut Down
              - Webhook
          - condition: numeric_state
            entity_id: sensor.plex_media_server
            above: 0
        sequence:
          - metadata: {}
            data:
              message: Plex shutdown overridden due to current activity.
            action: notify.notify
      - conditions:
          - condition: trigger
            id:
              - No Activity
          - condition: and
            conditions:
              - condition: time
                after: "22:05:00"
                before: "00:00:00"
              - condition: state
                entity_id: sensor.plex_media_server
                for:
                  hours: 0
                  minutes: 15
                  seconds: 0
                state: "0"
        sequence:
          - data: {}
            action: shell_command.nas_shutdown
          - data: {}
            action: rest_command.nuc_shutdown
mode: single

What does the automation trace tell you?

Also see how “No” is highlighted in your config above. It may be being interpreted as a boolean value. Try enclosing it in quotes.

  - trigger: state
    entity_id:
      - sensor.plex_media_server
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: "No Activity"
    to: "0"

and

      - conditions:
          - condition: trigger
            id:
              - "No Activity"
1 Like

I’ll check that tonight, my Trace History seems to get lost when HA restarts?

I will try this too. Thanks for the tips.

EDIT: The quotes did not remain in Automation when I saved and went back to YAML?

Ah. The UI. Try using an underscore instead of quotes. id: No_Activity

1 Like

Done. If I have issues tonight I’ll capture the trace.

No go @tom_l . I have captured the Traces, should I DM them or is the info ok to be posted publicly?

Here is fine. The trace wont have any more revealing information than your automation config already posted.

1 Like

22:05 was the ignore shutdown due to streaming active. 22:06 was the stream stopped and then I waited for 15 minutes.

{
  "trace": {
    "last_step": "action/0/choose/1/sequence/0",
    "run_id": "8c9dfe6e683a27d295966cebb69d079c",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-10-19T12:05:00.426316+00:00",
      "finish": "2024-10-19T12:05:01.832766+00:00"
    },
    "domain": "automation",
    "item_id": "1714043373892",
    "trigger": "time",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-10-19T12:05:00.426620+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.watch_plex_activity_shutdown",
              "state": "on",
              "attributes": {
                "id": "1714043373892",
                "last_triggered": "2024-10-19T11:44:44.042111+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Watch Plex Activity - Shutdown"
              },
              "last_changed": "2024-10-19T02:26:32.803087+00:00",
              "last_reported": "2024-10-19T11:44:44.044214+00:00",
              "last_updated": "2024-10-19T11:44:44.044214+00:00",
              "context": {
                "id": "01JAJ9G608J78TCPVS07K9650Y",
                "parent_id": "01JAJ9G606C7J3B4YM544N2B3M",
                "user_id": null
              }
            },
            "trigger": {
              "id": "Shut Down",
              "idx": "0",
              "alias": null,
              "platform": "time",
              "now": "2024-10-19T22:05:00.425663+10:00",
              "description": "time",
              "entity_id": null
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-10-19T12:05:00.426793+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "condition/0/entity_id/0": [
        {
          "path": "condition/0/entity_id/0",
          "timestamp": "2024-10-19T12:05:00.426896+00:00",
          "result": {
            "result": true,
            "state": "off",
            "wanted_state": "off"
          }
        }
      ],
      "condition/1": [
        {
          "path": "condition/1",
          "timestamp": "2024-10-19T12:05:00.427107+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "condition/1/entity_id/0": [
        {
          "path": "condition/1/entity_id/0",
          "timestamp": "2024-10-19T12:05:00.427191+00:00",
          "result": {
            "result": true,
            "state": "off",
            "wanted_state": "off"
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-10-19T12:05:00.428022+00:00",
          "changed_variables": {
            "context": {
              "id": "01JAJAN9WACRHNAGG12928APWG",
              "parent_id": null,
              "user_id": null
            }
          },
          "result": {
            "choice": 1
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2024-10-19T12:05:00.428305+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2024-10-19T12:05:00.428429+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/conditions/1": [
        {
          "path": "action/0/choose/0/conditions/1",
          "timestamp": "2024-10-19T12:05:00.428574+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/1/entity_id/0": [
        {
          "path": "action/0/choose/0/conditions/1/entity_id/0",
          "timestamp": "2024-10-19T12:05:00.428667+00:00",
          "result": {
            "result": false,
            "state": 1,
            "wanted_state_below": 1
          }
        }
      ],
      "action/0/choose/1": [
        {
          "path": "action/0/choose/1",
          "timestamp": "2024-10-19T12:05:00.428943+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/1/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0",
          "timestamp": "2024-10-19T12:05:00.429047+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/1/conditions/1": [
        {
          "path": "action/0/choose/1/conditions/1",
          "timestamp": "2024-10-19T12:05:00.429168+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/1/conditions/1/entity_id/0": [
        {
          "path": "action/0/choose/1/conditions/1/entity_id/0",
          "timestamp": "2024-10-19T12:05:00.429255+00:00",
          "result": {
            "result": true,
            "state": 1
          }
        }
      ],
      "action/0/choose/1/sequence/0": [
        {
          "path": "action/0/choose/1/sequence/0",
          "timestamp": "2024-10-19T12:05:00.429938+00:00",
          "result": {
            "params": {
              "domain": "notify",
              "service": "notify",
              "service_data": {
                "message": "Plex shutdown overridden due to current activity."
              },
              "target": {}
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1714043373892",
      "alias": "Watch Plex Activity - Shutdown",
      "description": "",
      "triggers": [
        {
          "at": "22:05:00",
          "id": "Shut Down",
          "trigger": "time"
        },
        {
          "trigger": "state",
          "entity_id": [
            "sensor.plex_media_server"
          ],
          "for": {
            "hours": 0,
            "minutes": 0,
            "seconds": 0
          },
          "id": "No_Activity",
          "to": "0"
        },
        {
          "trigger": "webhook",
          "allowed_methods": [
            "POST",
            "PUT",
            "HEAD",
            "GET"
          ],
          "local_only": false,
          "webhook_id": "watch-plex-activity-shutdown-b6oTRz2t2fb4ed9D91nje1mz",
          "id": "Webhook"
        }
      ],
      "conditions": [
        {
          "condition": "state",
          "entity_id": "input_boolean.vacation_mode",
          "state": "off",
          "enabled": true
        },
        {
          "condition": "state",
          "entity_id": "input_boolean.guest_mode",
          "state": "off"
        }
      ],
      "actions": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": [
                    "Shut Down",
                    "Webhook"
                  ]
                },
                {
                  "condition": "numeric_state",
                  "entity_id": "sensor.plex_media_server",
                  "below": 1
                }
              ],
              "sequence": [
                {
                  "data": {},
                  "action": "shell_command.nas_shutdown"
                },
                {
                  "data": {},
                  "action": "rest_command.nuc_shutdown"
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": [
                    "Shut Down",
                    "Webhook"
                  ]
                },
                {
                  "condition": "numeric_state",
                  "entity_id": "sensor.plex_media_server",
                  "above": 0
                }
              ],
              "sequence": [
                {
                  "metadata": {},
                  "data": {
                    "message": "Plex shutdown overridden due to current activity."
                  },
                  "action": "notify.notify"
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": [
                    "No_Activity"
                  ]
                },
                {
                  "condition": "and",
                  "conditions": [
                    {
                      "condition": "time",
                      "after": "22:05:00",
                      "before": "00:00:00"
                    },
                    {
                      "condition": "state",
                      "entity_id": "sensor.plex_media_server",
                      "for": {
                        "hours": 0,
                        "minutes": 15,
                        "seconds": 0
                      },
                      "state": "0"
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "data": {},
                  "action": "shell_command.nas_shutdown"
                },
                {
                  "data": {},
                  "action": "rest_command.nuc_shutdown"
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01JAJAN9WACRHNAGG12928APWG",
      "parent_id": null,
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Watch Plex Activity - Shutdown",
      "message": "triggered by time",
      "source": "time",
      "entity_id": "automation.watch_plex_activity_shutdown",
      "context_id": "01JAJAN9WACRHNAGG12928APWG",
      "domain": "automation",
      "when": 1729339500.427487
    }
  ]
}
{
  "trace": {
    "last_step": "action/0/choose/2/conditions/1/conditions/1/entity_id/0",
    "run_id": "831cac0fb5a143539e6ce947988ea7d9",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-10-19T12:06:09.967740+00:00",
      "finish": "2024-10-19T12:06:09.971962+00:00"
    },
    "domain": "automation",
    "item_id": "1714043373892",
    "trigger": "state of sensor.plex_media_server",
    "trace": {
      "trigger/1": [
        {
          "path": "trigger/1",
          "timestamp": "2024-10-19T12:06:09.968012+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.watch_plex_activity_shutdown",
              "state": "on",
              "attributes": {
                "id": "1714043373892",
                "last_triggered": "2024-10-19T12:05:00.427528+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Watch Plex Activity - Shutdown"
              },
              "last_changed": "2024-10-19T02:26:32.803087+00:00",
              "last_reported": "2024-10-19T12:05:01.832208+00:00",
              "last_updated": "2024-10-19T12:05:01.832208+00:00",
              "context": {
                "id": "01JAJAN9WACRHNAGG12928APWG",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "No_Activity",
              "idx": "1",
              "alias": null,
              "platform": "state",
              "entity_id": "sensor.plex_media_server",
              "from_state": {
                "entity_id": "sensor.plex_media_server",
                "state": "1",
                "attributes": {
                  "Trial_Master - Plex for iOS": "Lonely Planet (2024)",
                  "unit_of_measurement": "watching",
                  "friendly_name": "Plex Media Server"
                },
                "last_changed": "2024-10-19T11:58:47.645851+00:00",
                "last_reported": "2024-10-19T11:58:47.645851+00:00",
                "last_updated": "2024-10-19T11:58:47.645851+00:00",
                "context": {
                  "id": "01JAJA9XTX81CWS2RFPY38RZ67",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.plex_media_server",
                "state": "0",
                "attributes": {
                  "unit_of_measurement": "watching",
                  "friendly_name": "Plex Media Server"
                },
                "last_changed": "2024-10-19T12:06:09.966958+00:00",
                "last_reported": "2024-10-19T12:06:09.966958+00:00",
                "last_updated": "2024-10-19T12:06:09.966958+00:00",
                "context": {
                  "id": "01JAJAQDSE4BWE8NEPVQRNAK3H",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": 0
              },
              "attribute": null,
              "description": "state of sensor.plex_media_server"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-10-19T12:06:09.968131+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "condition/0/entity_id/0": [
        {
          "path": "condition/0/entity_id/0",
          "timestamp": "2024-10-19T12:06:09.968225+00:00",
          "result": {
            "result": true,
            "state": "off",
            "wanted_state": "off"
          }
        }
      ],
      "condition/1": [
        {
          "path": "condition/1",
          "timestamp": "2024-10-19T12:06:09.968429+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "condition/1/entity_id/0": [
        {
          "path": "condition/1/entity_id/0",
          "timestamp": "2024-10-19T12:06:09.968513+00:00",
          "result": {
            "result": true,
            "state": "off",
            "wanted_state": "off"
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-10-19T12:06:09.969249+00:00",
          "changed_variables": {
            "context": {
              "id": "01JAJAQDSF25GWWBYHXAD08SZ8",
              "parent_id": "01JAJAQDSE4BWE8NEPVQRNAK3H",
              "user_id": null
            }
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2024-10-19T12:06:09.969482+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2024-10-19T12:06:09.969597+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1": [
        {
          "path": "action/0/choose/1",
          "timestamp": "2024-10-19T12:06:09.969817+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0",
          "timestamp": "2024-10-19T12:06:09.969917+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/2": [
        {
          "path": "action/0/choose/2",
          "timestamp": "2024-10-19T12:06:09.970124+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/2/conditions/0": [
        {
          "path": "action/0/choose/2/conditions/0",
          "timestamp": "2024-10-19T12:06:09.970226+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/2/conditions/1": [
        {
          "path": "action/0/choose/2/conditions/1",
          "timestamp": "2024-10-19T12:06:09.970350+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/2/conditions/1/conditions/0": [
        {
          "path": "action/0/choose/2/conditions/1/conditions/0",
          "timestamp": "2024-10-19T12:06:09.970438+00:00",
          "result": {
            "after": {
              "__type": "<class 'datetime.time'>",
              "isoformat": "22:05:00"
            },
            "now_time": {
              "__type": "<class 'datetime.time'>",
              "isoformat": "22:06:09.970493"
            },
            "before": {
              "__type": "<class 'datetime.time'>",
              "isoformat": "00:00:00"
            },
            "result": true
          }
        }
      ],
      "action/0/choose/2/conditions/1/conditions/1": [
        {
          "path": "action/0/choose/2/conditions/1/conditions/1",
          "timestamp": "2024-10-19T12:06:09.970612+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/2/conditions/1/conditions/1/entity_id/0": [
        {
          "path": "action/0/choose/2/conditions/1/conditions/1/entity_id/0",
          "timestamp": "2024-10-19T12:06:09.970702+00:00",
          "result": {
            "result": false,
            "state": "0",
            "duration": "2024-10-19T11:51:09.971307+00:00"
          }
        }
      ]
    },
    "config": {
      "id": "1714043373892",
      "alias": "Watch Plex Activity - Shutdown",
      "description": "",
      "triggers": [
        {
          "at": "22:05:00",
          "id": "Shut Down",
          "trigger": "time"
        },
        {
          "trigger": "state",
          "entity_id": [
            "sensor.plex_media_server"
          ],
          "for": {
            "hours": 0,
            "minutes": 0,
            "seconds": 0
          },
          "id": "No_Activity",
          "to": "0"
        },
        {
          "trigger": "webhook",
          "allowed_methods": [
            "POST",
            "PUT",
            "HEAD",
            "GET"
          ],
          "local_only": false,
          "webhook_id": "watch-plex-activity-shutdown-b6oTRz2t2fb4ed9D91nje1mz",
          "id": "Webhook"
        }
      ],
      "conditions": [
        {
          "condition": "state",
          "entity_id": "input_boolean.vacation_mode",
          "state": "off",
          "enabled": true
        },
        {
          "condition": "state",
          "entity_id": "input_boolean.guest_mode",
          "state": "off"
        }
      ],
      "actions": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": [
                    "Shut Down",
                    "Webhook"
                  ]
                },
                {
                  "condition": "numeric_state",
                  "entity_id": "sensor.plex_media_server",
                  "below": 1
                }
              ],
              "sequence": [
                {
                  "data": {},
                  "action": "shell_command.nas_shutdown"
                },
                {
                  "data": {},
                  "action": "rest_command.nuc_shutdown"
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": [
                    "Shut Down",
                    "Webhook"
                  ]
                },
                {
                  "condition": "numeric_state",
                  "entity_id": "sensor.plex_media_server",
                  "above": 0
                }
              ],
              "sequence": [
                {
                  "metadata": {},
                  "data": {
                    "message": "Plex shutdown overridden due to current activity."
                  },
                  "action": "notify.notify"
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": [
                    "No_Activity"
                  ]
                },
                {
                  "condition": "and",
                  "conditions": [
                    {
                      "condition": "time",
                      "after": "22:05:00",
                      "before": "00:00:00"
                    },
                    {
                      "condition": "state",
                      "entity_id": "sensor.plex_media_server",
                      "for": {
                        "hours": 0,
                        "minutes": 15,
                        "seconds": 0
                      },
                      "state": "0"
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "data": {},
                  "action": "shell_command.nas_shutdown"
                },
                {
                  "data": {},
                  "action": "rest_command.nuc_shutdown"
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01JAJAQDSF25GWWBYHXAD08SZ8",
      "parent_id": "01JAJAQDSE4BWE8NEPVQRNAK3H",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Watch Plex Activity - Shutdown",
      "message": "triggered by state of sensor.plex_media_server",
      "source": "state of sensor.plex_media_server",
      "entity_id": "automation.watch_plex_activity_shutdown",
      "context_id": "01JAJAQDSF25GWWBYHXAD08SZ8",
      "domain": "automation",
      "when": 1729339569.9687917
    }
  ]
}

The first trace indicates a time trigger, then it passed your conditions and but Plex was not shut down due to activity (sensor.plex_media_server was > 0).

The second trace indicates the no activity trigger occurred, then it passed your conditions and no action was taken because there was activity less than 15 minutes ago.

      - conditions:
          - condition: trigger
            id:
              - No Activity ### TRUE ###
          - condition: and
            conditions:
              - condition: time  ### TRUE ###
                after: "22:05:00"
                before: "00:00:00"
              - condition: state
                entity_id: sensor.plex_media_server ### FALSE ###
                for:
                  hours: 0
                  minutes: 15
                  seconds: 0
                state: "0"

I think you should have a trigger for this condition too. Then, if it is after 22:05, it should also turn off.

2 Likes

Looking at the second trace it triggered as soon as the activity went to zero and didn’t turn off after 15 min.

The logic I am trying to achieve it to look for zero activity after 22:05 and close down if the zero activity has been 15 min.

Yeah that is not what your automation does. It triggers immediately on no activity and then checks to see if there was 15 minutes of no activity. That’s never going to happen

Change your automation trigger to match the conditions in your choose action:

  - trigger: state
    entity_id:
      - sensor.plex_media_server
    for:
      hours: 0
      minutes: 15 ### <- changed
      seconds: 0

This will only trigger after 15 minutes of no activity.

EDIT: Edwin beat me to it.

Thanks @tom_l @Edwin_D

I’ve modified trigger and will test again this evening. Appreciate all the assistance and your time.

BINGO! Thanks so much for the guidance :+1::+1::+1:

1 Like