I am not an expert at YAML and lots of examples I query don’t tell me whether their code is a sensor or an automation or a script so my querys have not shown me a solution.
I just want to run a script that tells me how long all the switches and lights (that are currently on) have been on for.
I did set up the History_Stats but that ends up giving me a bar chart and how long a light has been on every day for the past week.
What I want to know is if a light has been running for hours, then I can track down problems. I will just run the script and get a notification on my iphone what lights are currently on and how long they have been on.
This will tell you which lights and switches have been on for x hours:
script:
switches_and_lights_on_for_x_hrs:
alias: Switches and lights that have been on for x hours or more
sequence:
- action: notify.your_notification_service_here # change this
data:
title: "💡 <b>Switches and lights on</b>"
message: >
The following lights have been on for 4 hours or more:
{{ states.light
| selectattr('last_changed', 'gt', now() - timedelta(hours=time_on))
| map(attribute='name')
| list
| join(', ')
}}
The following switches have been on for 4 hours or more:
{{ states.switch
| selectattr('last_changed', 'gt', now() - timedelta(hours=time_on))
| map(attribute='name')
| list
| join(', ')
}}
You would use it like this:
actions:
- action: script.switches_and_lights_on_for_x_hrs
data:
time_on: 4 # change to the number of hours the light or switch must have been on for, at least, to be in the reply list.