Turning of switch when power consumption is below X watt for Y minutes

Does any of you have an example of a automation that turns off a switch, when the same switch’s power consumption has been low for a period of time?

I am quite lazy, when it comes to turning of my monitors, speakers etc. for my desk, so I want to have a automation that turns of a switch the switch, when the power consumption is below 40w for 1 hour.

I’ve tried creating one, but it doesn’t seem to trigger.

description: ""
mode: single
trigger:
  - type: power
    platform: device
    device_id: 7318b0612c6efe8bf81e8a8698dd1697
    entity_id: 5842615a704a9611ef26d1ad46ea213f
    domain: sensor
    for:
      hours: 1
      minutes: 0
      seconds: 0
    below: 40
condition: []
action:
  - type: turn_off
    device_id: 7318b0612c6efe8bf81e8a8698dd1697
    entity_id: 7e5d81fcb8bcaee7d8ae22e7c9597932
    domain: switch

The trigger will happen at the point the value changes from 40 to below 40 and stays there for an hour.

If you started your test and it was already below 40, it will not trigger.

Also if you are starting out troubleshooting YAML outside of the AI entities and stated are recommended. Devices were build for the UI and are more difficult for people to manage and work with dur to the naming and other issues. Its not impossible, but a huge PIA.

Why and how to avoid device_ids in automations and scripts.