Run a script to notify me on phone how long lights have been on

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.

Can anyone assist me?

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.

The script appears to work kinda. But I do not know what actions are. It would be easier for me to set up a helper and incorporate that into the script.

So currently i added 4 into the script replacing time_on. When it runs it tells me about lghts that are not even switched on?

Plus I had to break the script into switches and lights as the message was too big for iphone notifications.

Any idea on why it tells me about lights which are not even switched on?

Oops I left out an important bit. Try this:

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('state', 'eq', 'on')
                | 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('state', 'eq', 'on')
                | selectattr('last_changed', 'gt', now() - timedelta(hours=time_on))
                | map(attribute='name')
                | list
                | join(', ')
            }}

That should cut the list size down considerably to only those lights and switches that are still on and have been on for x hours or more.

You don’t need any extra helpers, You can run the script from an automation triggered at a time (or any other trigger), or manually from a button in your dashboard. e.g.

type: button
name: List Items On
show_state: true
tap_action:
  action: perform-action
  perform_action: script.switches_and_lights_on_for_x_hrs
  data:
    time_on: 4

Sorry for delay in response, XMAS. That all works better. thanks for your help.

I tried to work it out on how to set the value of time_on but could not find a solution. I set up an number helper to be able to change that in a dashboard. Then I just want to tell google to run the lights on script and send me a notification to my iphone.
But how to I get a helper into a script. I tried various formats and the last one was
selectattr(‘last_changed’, ‘gt’, now() - timedelta(hours = ‘{{input_number.time_on}}’ )

Nothing I have tried works. Any ideas.