Notification Action trigger to check if your sleeping after watching tv

Hi All.

i been trying to get a Automation together with a script going that i want to send a action notification to me to check if i am sleeping.
My idee was that i wanted it to wait to see of i do push the button to keep looking on the tv. And if i do that it aborts the script.

Problem i have now is that that the automation works, and so does the Notification action to some extent. The problem is that either i have the buttons working and i can select if i wanna continue looking @ my tv. or i cant turn it off and go to sleep. But when i put in my wait for trigger code it just turn off the tv always.

Ive been at it for a week now trying to search the forums and working on it myself. trying to figure out what im doing wrong. If someone could have a look at my code and see if you can figure out what im doing wrong or maby have some pointers. <3

Or maby if someone already got someting like this maby you can point me in the right direction.
Many thanks.

My Script Code

tvturnoffsleep:
  alias: tvturnoffsleep
  sequence:
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          actions:
            - action: "SRTVON"
              title: "FortsÀtt titta pÄ tvsovrum"
            - action: "SRTVAV"
              title: "StÀng av tvsovrum"
    - wait_for_trigger:
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: SRTVON
      timeout: '00:00:30'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == SRTVON }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
            - service: script.turn_off
              entity_id: script.tvturnoffsleep
        - conditions: "{{ wait.trigger.event.data.action == SRTVAV }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
#    - delay:
#        - seconds: 10
#        - minutes: 1
    - service: remote.turn_off
      entity_id: remote.sovrum

My Automation code:

- alias: turnoff_action_notification_sovrumstv_no_aktivity
  id: '22367557898765681391549'
  initial_state: 'on'
  trigger:
   - platform: time
     at: "01:00:00"
   - platform: time
     at: "02:00:00"
   - platform: time
     at: "02:20:00"
  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:50:00'
        before: '04:00:00'
      - condition: state
        entity_id: remote.sovrum
        state: 'on'
  action:
    - service: script.turn_on
      target:
        entity_id: script.tvturnoffsleep

You just need to remove the extra service call to turn off the tv that you had at the end. That service call was independent of either of your Choose options, so it would always run, no matter which action you select on your phone. I assume you added that in case you were already asleep and didn’t press either button
 which made it necessary to also have a script.turn_off if you wanted the tv to stay on.

By adding another option to handle when no button is selected the tv will still turn off and you can remove the unecessary script.turn_off service call.

tvturnoffsleep:
  alias: tvturnoffsleep
  sequence:
    - alias: 
       variables:
         action_on: "{{ 'SRTVON_' ~ context.id }}"
         action_off: "{{ 'SRTVAV_' ~ context.id }}"
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          actions:
            - action: "{{ action_on }}"
              title: "FortsÀtt titta pÄ tvsovrum"
            - action: "{{ action_off }}"
              title: "StÀng av tvsovrum"
    - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_on }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_off }}"
      timeout: '00:00:30'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == action_on }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
        - conditions: "{{ wait.trigger.event.data.action == action_off }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
        - conditions: "{{wait.trigger == 'none'}}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Du redan sover"
            - service: remote.turn_off
              entity_id: remote.sovrum
1 Like

Ty for taking your time to look at my problem. <3

Saw that you edited the code. ill have a look and see if it works with the new one. Ty again for helping me out.

The greyed-out is due to an extra "


I’ve edited my post above. The original was missing a trigger for the Off button.

1 Like

Ty man. I see you added some more Magic ^^
Ill try it out see how it goes now.

Its unhappy about .Invalid config for [script]: Unable to determine action @ data[‘sequence’][0].
Ive tried to remove here and there to see were the problem is. hmm

If you have the time to have a look at it again id mutch appriciate it.

I got it working now without error by using this code below.
But it wont turn of the tv if i dont push any buttons. (if i have fallen asleep)
So if im not doing anything nothing happens, If you have any suggestions to why that may be id be happy to get any input.

tvturnoffsleep:
  alias: tvturnoffsleep
  sequence:
    - variables:
        action_on: "{{ 'SRTVON_' ~ context.id }}"
        action_off: "{{ 'SRTVAV_' ~ context.id }}"
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          actions:
            - action: "{{ action_on }}"
              title: "FortsÀtt titta pÄ tvsovrum"
              icon: "sfsymbols:tv.and.mediabox"
            - action: "{{ action_off }}"
              title: "StÀng av tvsovrum"
              icon: "sfsymbols:bed.double.circle"
    - wait_for_trigger:
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_on }}"
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_off }}"
      timeout: '00:00:30'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == action_on }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
        - conditions: "{{ wait.trigger.event.data.action == action_off }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
        - conditions: "{{wait.trigger == 'none'}}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Du redan sover"
            - service: remote.turn_off
              entity_id: remote.sovrum

Personally I would have said instead of using

        - conditions: "{{wait.trigger == 'none'}}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Du redan sover"
            - service: remote.turn_off
              entity_id: remote.sovrum

It would make more sense to put that sequence in to the default: block of that choose block.
That way if either of the 2 conditions aren’t met, then the default block is executed.

        default:
          - service: notify.notify
            data:
              title: "Sovgott"
              message: "Du redan sover"
          - service: remote.turn_off
            entity_id: remote.sovrum
2 Likes

I tried a few things, and it looks like either of the following are working for when you don’t press a button within the timeout period:

value_template: '{{wait.trigger == None}}'

value_template: '{{wait.remaining == 0}}'

1 Like

Are you talking about doing it like this?

        - value_template: '{{wait.trigger == None}}'
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Du redan sover"
            - service: remote.turn_off
              entity_id: remote.sovrum

Ok. hm were exaclty?

Ive tried to read around on were to put the default block but no ciggar.
And were ever i put it in the script just promts errors.

If you could take your time to show me an example of were it goes that would be lovley.

tvturnoffsleep:
  alias: tvturnoffsleep
  sequence:
    - variables:
        action_on: "{{ 'SRTVON_' ~ context.id }}"
        action_off: "{{ 'SRTVAV_' ~ context.id }}"
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          actions:
            - action: "{{ action_on }}"
              title: "FortsÀtt titta pÄ tvsovrum"
              icon: "sfsymbols:tv.and.mediabox"
            - action: "{{ action_off }}"
              title: "StÀng av tvsovrum"
              icon: "sfsymbols:bed.double.circle"
    - wait_for_trigger:
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_on }}"
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_off }}"
      timeout: '00:00:30'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == action_on }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
        - conditions: "{{ wait.trigger.event.data.action == action_off }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
      default:
        - service: notify.notify
          data:
            title: "Sovgott"
            message: "Du redan sover"
         - service: remote.turn_off
           entity_id: remote.sovrum

default, is part of the choose block, so should be at the same indentation as the choose statement.

1 Like

Ill try it out, ty for talong your time.

1 Like

That finaly did it! <3

Thank you Andrew!

There was abitt of code alignment in the end that i fix and then it worked like you showed!
I guess in a way now that the script turn off is not there at all, then one could maby do this as a automation insted, or maby not sins we use.

  sequence:
    - variables:
        action_on: "{{ 'SRTVON_' ~ context.id }}"
        action_off: "{{ 'SRTVAV_' ~ context.id }}"

The final code that work for the Script was the one below
Script:

tvturnoffsleep:
  alias: tvturnoffsleep
  sequence:
    - variables:
        action_on: "{{ 'SRTVON_' ~ context.id }}"
        action_off: "{{ 'SRTVAV_' ~ context.id }}"
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          actions:
            - action: "{{ action_on }}"
              title: "FortsÀtt titta pÄ tvsovrum"
              icon: "sfsymbols:tv.and.mediabox"
            - action: "{{ action_off }}"
              title: "StÀng av tvsovrum"
              icon: "sfsymbols:bed.double.circle"
    - wait_for_trigger:
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_on }}"
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_off }}"
      timeout: '00:00:30'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == action_on }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
        - conditions: "{{ wait.trigger.event.data.action == action_off }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
      default:
        - service: notify.notify
          data:
            title: "Sovgott"
            message: "Du redan sover"
        - service: remote.turn_off
          entity_id: remote.sovrum

Automation:

- alias: turnoff_action_notification_sovrumstv_no_aktivity
  id: '4000'
  initial_state: 'on'
  trigger:
   - platform: time
     at: "01:00:00"
   - platform: time
     at: "02:00:00"
   - platform: time
     at: "02:30:00"
   - platform: time
     at: "03:00:00"
  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:50:00'
        before: '04:00:00'
      - condition: state
        entity_id: remote.sovrum
        state: 'on'
  action:
    - service: script.turn_on
      target:
        entity_id: script.tvturnoffsleep
2 Likes

You could definitely do it as an automation, if you wanted to. It is possible to set variables that are available to the whole automation or under specific sections such as Condition or Action.

It can get annoying having notifications pile up. I usually have the old notification removed by assigning a timeout or using the clearing function:

As Automation
- alias: turnoff_action_notification_sovrumstv_no_aktivity
  id: '22367557898765681391549'
  initial_state: 'on'
  trigger:
   - platform: time
     at: "01:00:00"
   - platform: time
     at: "02:00:00"
   - platform: time
     at: "02:20:00"
  condition:
    - condition: state
      entity_id: remote.sovrum
      state: 'on'
  action:
    - variables:
        action_on: "{{ 'SRTVON_' ~ context.id }}"
        action_off: "{{ 'SRTVAV_' ~ context.id }}"
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          timeout: 40
          actions:
            - action: "{{ action_on }}"
              title: "FortsÀtt titta pÄ tvsovrum"
              icon: "sfsymbols:tv.and.mediabox"
            - action: "{{ action_off }}"
              title: "StÀng av tvsovrum"
              icon: "sfsymbols:bed.double.circle"
    - wait_for_trigger:
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_on }}"
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_off }}"
      timeout: '00:00:30'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == action_on }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
        - conditions: "{{ wait.trigger.event.data.action == action_off }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
      default:
        - service: notify.notify
          data:
            title: "Sovgott"
            message: "Du redan sover"
        - service: remote.turn_off
          entity_id: remote.sovrum
1 Like

Ty!

Thank you both @Didgeridrew @mobile.andrew.jones for putting down your time to help me. And propably others that might wanna use the same idée.
You made me and my wife verry glad, and as they say “Happy wife happy life.”

Last changes i made with the automation you posted was a 1min wait. and some changes to the text.

- alias: turnoff_tvsovrum_sleeping_action_notification
  id: '4002'
  initial_state: 'on'
  trigger:
   - platform: time
     at: "01:30:00"
   - platform: time
     at: "02:00:00"
   - platform: time
     at: "02:30:00"
   - platform: time
     at: "03:00:00"
  condition:
    - condition: state
      entity_id: remote.sovrum
      state: 'on'
  action:
    - variables:
        action_on: "{{ 'SRTVON_' ~ context.id }}"
        action_off: "{{ 'SRTVAV_' ~ context.id }}"
    - service: notify.notify
      data:
        message: "Sover du?, StÀng av tv?"
        data:
          timeout: 90
          actions:
            - action: "{{ action_on }}"
              title: "FortsÀtt titta pÄ tvsovrum"
              icon: "sfsymbols:tv.and.mediabox"
            - action: "{{ action_off }}"
              title: "StÀng av tvsovrum"
              icon: "sfsymbols:bed.double.circle"
    - wait_for_trigger:
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_on }}"
        - platform: event
          event_type: mobile_app_notification_action
          event_data:
            action: "{{ action_off }}"
      timeout: '00:01:00'
      continue_on_timeout: true
    - choose:
        - conditions: "{{ wait.trigger.event.data.action == action_on }}"
          sequence:
            - service: notify.notify
              data:
                title: "Du vÀljer att titta vidare"
                message: "TÀnk pÄ jobbet...."
        - conditions: "{{ wait.trigger.event.data.action == action_off }}"
          sequence:
            - service: notify.notify
              data:
                title: "Sovgott"
                message: "Bra beslut"
            - service: remote.turn_off
              entity_id: remote.sovrum
      default:
        - service: notify.notify
          data:
            title: "Sovgott"
            message: "StÀnger av tvn"
        - service: remote.turn_off
          entity_id: remote.sovrum
3 Likes

Been using this for sometime now and can only say it works flawlessly. Ty again for all your help.

1 Like