pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

How does wait_for_trigger syntax works in pyscript? Struggling with Actionable Notification

Open warheat1990 opened this issue 3 months ago • 1 comments

I've used pyscript (no YAML) for all my automation/scripts/trigger/etc and it rocks. However I'm struggling with how to do Actionable Notification. Consider this syntax that I've come across in this website https://www.devwithimagination.com/2023/04/16/using-actionable-notifications-in-home-assistant/

alias: Notify - Kids lights left on
description: ""
trigger:
  - platform: time
    at: "08:00:00"
condition:
  - condition: and
    conditions:
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
        alias: Is it a weekday?
      - condition: or
        conditions:
          - condition: state
            entity_id: light.childone_room_cloud_light
            state: "on"
          - condition: state
            entity_id: light.childone_room_bulb
            state: "on"
          - condition: state
            entity_id: light.childtwo_bedroom_main_bulb
            state: "on"
        alias: Are any of the boys lights on?
action:
  - alias: Set up variables for the actions
    variables:
      action_no: "{{ 'NO_' ~ context.id }}"
      action_turnoff: "{{ 'TURNOFF_' ~ context.id }}"
  - alias: Notify Mobile
    service: notify.mobile_app_twelve
    data:
      message: Boys lights are still on. Turn off?
      data:
        actions:
          - action: "{{ action_turnoff }}"
            title: Turn off lights
          - action: "{{ action_no }}"
            title: Leave On
  - alias: Wait for a response
    wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_no }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_turnoff }}"
  - alias: Perform the action
    choose:
      - conditions: "{{ wait.trigger.event.data.action == action_no }}"
        sequence: []
      - conditions: "{{ wait.trigger.event.data.action == action_turnoff }}"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.childone_room_bulb
                - light.childtwo_bedroom_main_bulb
                - light.childone_room_cloud_light
mode: single


Seems straightforward except I'm struggling with this wait_for_trigger part of the YAML syntax

    wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_no }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_turnoff }}"
  - alias: Perform the action
    choose:
      - conditions: "{{ wait.trigger.event.data.action == action_no }}"
        sequence: []
      - conditions: "{{ wait.trigger.event.data.action == action_turnoff }}"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.childone_room_bulb
                - light.childtwo_bedroom_main_bulb
                - light.childone_room_cloud_light

How do I write this in pyscript?

warheat1990 avatar Sep 20 '25 19:09 warheat1990

This should look something like this:

@time_trigger("cron(0 8 * * 1-5)")  # 8:00 AM on weekdays (Mon-Fri)
def notify_kids_lights(trigger_time = None, trigger_type = None, **kwargs):
    # Check if any lights are on
    lights_on = any([
        state.get('light.childone_room_cloud_light') == "on",
        state.get('light.childone_room_bulb') == "on",
        state.get('light.childtwo_bedroom_main_bulb') == "on",
    ])

    if not lights_on:
        return

    # Set up action identifiers
    context_id = 'notify_kids_lights'
    action_no = f"NO_{context_id}"
    action_turnoff = f"TURNOFF_{context_id}"

    # Send notification with actions
    notify.mobile_app_twelve(
        message="Boys lights are still on. Turn off?",
        data={
            "actions": [
                {"action": action_turnoff, "title": "Turn off lights"},
                {"action": action_no, "title": "Leave On"}
            ]
        }
    )

    # Wait for notification action response
    trigger_info = task.wait_until(
        event_trigger=[
            "mobile_app_notification_action",
            f"data['action'] in ({action_no}, {action_turnoff})"
        ]
    )

    # Check which action was selected
    selected_action = trigger_info["event"]["data"]["action"]

    if selected_action == action_no:
        # Leave lights on - no action needed
        pass
    elif selected_action == action_turnoff:
        # Turn off the lights
        # noinspection PyTypeChecker
        light.turn_off(entity_id=[
            "light.childone_room_bulb",
            "light.childtwo_bedroom_main_bulb",
            "light.childone_room_cloud_light"
        ])

I did not test this. This is just for you to get my main idea.

ALERTua avatar Sep 21 '25 10:09 ALERTua