How can I trigger a pyscript script every 30 seconds

Hi all, I am trying to trigger a pyscript script every 30secs with @time_trigger(). I was able to trigger it every minute with:

@time_trigger("startup", "cron(*/1 * * * *)")

but the cron format unfortunately does not allow to specify seconds. Is there a way to specify every 30 secs with the datetime format? I can’t figure it out based on the docs here.
Thanks!

Make two cronjobs, same twice but add 30sec delay(sleep) to other one ?

This is a time trigger condition in a pyscript .py file like this:

@time_trigger(“startup”, “cron(*/1 * * * *)”)
def update_sensors(): 
   somehow_update_sensor_values()

so unfortunately there is no sleep inbetween possible.

1 Like

I actually found a solution (or workaround?), I just changed the @time_trigger into an @service and then used a regular automation in HA with a time pattern trigger of 30secs

platform: time_pattern
seconds: "30"

that then triggers the service. If anyone knows what datetimestring to use to do the same with @time_trigger(“datetimstring”), I’d be still curious since I like things to be as independent as possible.

1 Like

And of course the real solution that I now found is:

@time_trigger('period(now, 30s)')

It is right in the documentation I just was too fixated on cron and didn’t see it…

4 Likes

Nice find! If you want to trigger on hh:mm:00 and hh:mm:30, you can also use:
@time_trigger('period(midnight, 30s)')

(instead of midnight you can use any datetime that is a full minute)