Trouble with canceling an action when started

hey I’m new here and am not very good at coding or such i have been trying for the better half of a day now but am unable to figure this out so i am coming for some help i know this is a lot but i hope i can get some assistance
#-------------------------------------
what i want is:

when the lastactive sensor is idle for 6 seconds it will start the automation

i will be messaged that there is a countdown running and if i move my mouse or do any activity on my pc that will refresh the sensor (the sensor doesn’t produce an on or off as far as i can tell it refreshes every second with a new name so i am tracking how long its been counting, any activity takes it back to 0 seconds and it counts up again) it will abort the automation and set it back up so its permanently looping -if idle for this many seconds then start a countdown to hibernate unless interrupted with activity-

but if it is not interrupted and left idle until the countdown finishes it will hibernate the pc
#---------------------

my issue is i cant get it to cancel the countdown/stop sending notifications if the active sensor gets updated i have tried if triggers to detect if the active bool (connected to the sensor) has been active at any point and it would send me a notification (then would restart the automation but for now just notify me) but it wont send any notifications like it wont detect if the bool has been active

this is the code i have so far (image will be below if it helps)

alias: PC Idle Countdown
description: ""
trigger:
  - platform: time_pattern
    seconds: /1

#triggers every second i belive that helps the automation activate closer to the given seconds in the template (it usually takes quite a while longer but i think this makes the action run nearer the idle time)

condition:
  - condition: template
    value_template: |-
      {{ (as_timestamp(now()) -
          as_timestamp(states.sensor.craigs_pc_lastactive.last_updated | default(0))) > 6 }}

#looks for idle time temporarily 6 seconds set

action:
  - service: notify.craigs_pc
    data:
      message: PC Countdown Started - 4 mins
  - parallel:
      - delay:
          hours: 0
          minutes: 0
          seconds: 15
          milliseconds: 0
        enabled: true
      - if:
          - condition: state
            entity_id: input_boolean.active
            for:
              hours: 0
              minutes: 0
              seconds: 0
            state: "on"
        then:
          - service: notify.craigs_pc
            metadata: {}
            data:
              message: vbgjk1
          - stop: ""
  - service: notify.craigs_pc
    data:
      message: PC Countdown Started - 2 mins
  - parallel:
      - delay:
          hours: 0
          minutes: 0
          seconds: 15
          milliseconds: 0
        enabled: true
      - if:
          - condition: state
            entity_id: input_boolean.active
            for:
              hours: 0
              minutes: 0
              seconds: 0
            state: "on"
        then:
          - service: notify.craigs_pc
            metadata: {}
            data:
              message: vbgjk 2
          - stop: ""
  - if:
      - condition: state
        entity_id: input_boolean.active
        state: "off"
        for:
          hours: 0
          minutes: 5
          seconds: 0
        enabled: true
    then:
      - service: notify.craigs_pc
        metadata: {}
        data:
          message: "off"
      - service: automation.turn_off
        target:
          entity_id: automation.new_automationhjkg  #this is to turn of the current automation when it 
                                                                                completes and the pc powers down i have 
                                                                      another automation to turn it back on when the pc is active
        data:
          stop_actions: true
    else: []
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
mode: single

i have a Boolean that toggles when the pc is active or not so the -what would be hibernate now just an off notification- would cheak it to see if it was recently active and if so to turn off or not

i know this is a big mess i apologise like i said im new to this and tried my best but i am having a lot of trouble thanks for any help
code image

this feels like such an easy automation too its just the whole stopping it from sending me countdown notifications when it detects activity that’s really stumping me i cant get it to stop if activity is detected during the action

yeah ive seen trhis but i cant find a way to turn it off when it detects a sensor update durning countdowni have attempted to try if the active boolion turns on at any point it sends me a notivication (and would also cancel the automation) but it never sends the message if the bool is toggles on in the first place

Have you considered using more than one automation?

1 Like

im open to doing so but i am unsure how to - i dont know how another automation can be used to assist i mean

perhaps if bool is on then it would turn off the automation (with another automation to automatically turn it back on which i currently have) ??

but i want it to notify me that the count down has been cancelled so

the countdown would start after set time

i could make it turn on an automation that if bool is active it would notify me and turn off countdown automation (with the existing automation turning it back on)
and if the automation doesnt detect activity then it would just keep going and notify me untill it hibernates???

Well… I admit I’m not entirely clear about what you’re trying to achieve, but thinking about it… :thinking:

It looks to me as if your automation is triggered every second, with the action section running every six seconds. I don’t think any of this is necessary. The real trigger (if I understand you correctly) is a sensor being idle for six seconds. Wouldn’t it be better to start with:

trigger:
  - platform: state
    entity_id:
      - your lastactive sensor
    to: idle
    for:
      hours: 0
      minutes: 0
      seconds: 6

The action part could then

  • Send a notification
  • Start a timer helper
  • Stop

A second automation triggered by the timer helper reaching X minutes could shut down the PC.

A third automation triggered by the lastactive sensor going from idle to another state could cancel the timer.

1 Like

that would work but the sensor gives a new state name every second the pc is active so i cant run it on state but i have thought about it running if the active bool is idle for a set amount of time?

You mean the state changes every second? Is this a numeric sensor, then?

image

its a hass agent sensor every time it detects pc actifity it will say what time it detected it so every second its a new state as you can see at 1am i slept so it stayed in that state and when i was active it has a new state every second so i made the bool to simplify it to an on when active and off when inactive sort of


Sorry, I’m being thick. :roll_eyes:

How about using the template from your condition to create a template binary sensor, then using that to trigger the automation?

i think i get what your tryying to say but i dont know what that means or how to do it haha

side note it runs fine its really just the whole getting it to abort the countown / stop sending countown notifications thats getting me stuck what we were talking about was sounding good something like this? (the times are placeholders of course)
(bool being the active Boolean like i listed above tuning on and off via the automations)

I agree.

@penguinpatrol

The automation also generates a trace file every time the Time Pattern Trigger fires. So, every second.

Good thing trace files are limited by default to a maximum of ten copies. However that means each trace file’s lifespan is ten seconds.

A 1-second Time Pattern Trigger is simply a bad idea. You should reconsider how this automation is triggered.

yes imn thinking of triggering it by the “active” Boolean as i have set up automations to turn it on and off depending on the activity as shown above