Need advice to set single automation to turn on and off a Kasa device based on temperature

I’m looking for a way to set single automation to turn on and off a Kasa device based on outside temperature. I have the following to turn it on at 30F but would like to have a parameter included that turns it off if external temp goes above 30F for more than an hour.
Any help is appreciated.

alias: Turn on Garage Waterpipe Heater
description: ""
trigger:
  - type: temperature
    platform: device
    device_id: 2a4acb1d8b3cc9cc9f49dcbdf5951322
    entity_id: 8dc295c1b4920b9abecdc01a83ad5b42
    domain: sensor
    below: 30
    for:
      hours: 1
      minutes: 0
      seconds: 0
condition: []
action:
  - type: toggle
    device_id: e005b3b95fb8b5471b661eccdcf840b6
    entity_id: 16faae6421a0a29e291afaecc4fb4b29
    domain: switch
  - device_id: dfd8befc1b4e27b7ee9c1bcb992d1eb6
    domain: mobile_app
    type: notify
    message: Garage pipe heater turned on
mode: single
alias: Turn on Garage Waterpipe Heater
description: ""
trigger:
  - id: 'on'
    type: temperature
    platform: device
    device_id: 2a4acb1d8b3cc9cc9f49dcbdf5951322
    entity_id: 8dc295c1b4920b9abecdc01a83ad5b42
    domain: sensor
    below: 30
    for:
      hours: 1
      minutes: 0
      seconds: 0
  - id: 'off'
    type: temperature
    platform: device
    device_id: 2a4acb1d8b3cc9cc9f49dcbdf5951322
    entity_id: 8dc295c1b4920b9abecdc01a83ad5b42
    domain: sensor
    above: 30
    for:
      hours: 1
      minutes: 0
      seconds: 0
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: 16faae6421a0a29e291afaecc4fb4b29
  - service: notify.mobile_app
    data:
      message: 'Garage pipe heater turned {{ trigger.id }}'
mode: single

You will need to check Developer Tools > States to confirm that notify.mobile_app exists. If it doesn’t, modify the example to use whatever it’s actually named in your system.

The reason I changed the Device Actions in your example to service calls is because Device Actions don’t support templates.

FWIW, the Device Triggers in your example can be changed to a Numeric State Trigger but it’s not necessary.

Also, if you know the user-friendly version of the entity_id 16faae6421a0a29e291afaecc4fb4b29 then I suggest you use that in the service call (it makes it easier to understand which switch is used). It will look something like switch.some_name_you_created. Perhaps it’s switch.garage_heater.

Thanks, but I’m gettin the error in the Trace:
Executed: January 20, 2024 at 12:36:42 PM
Error: Template rendered invalid service: switch.turn_

You’ll get that message if the Device Triggers lack a value for id or if you attempted to test the automation using its Run command (which doesn’t exercise the triggers, thereby failing to define trigger.id, and simply executes the actions).