How do I start an automation when a boolean turns on and run it every ten seconds until the boolean is turned off?

I have 13 booleans named foobarsone1aktiv to foobarsone13aktiv. (Yes, it does mean Foobar Zone 13 Active). They are turned on when Foobar starts on the multizone server. While they are on, it means that Foobar is playing online radio in that zone, and scrape sensors should check every ten seconds if the playback info sent by the radio station has changed (for name of the song, artist, program and so on) and send the info to a webpage I have. Everything is working except for the starting and stopping of the ten second automation. I really don’t want 13 automations checking every ten seconds all the time, both because I fear it may bog down the system and because it throws errors in the log since the scrape sensor is disabled as long Foobar isn’t running, to avoid loops of trying to connect to something that isn’t there.

So what would be the way to do this? Do I need a second automation that starts the “every ten seconds” automation? I do not want to use enabling and disabling of automations, that’s prone to errors in my experience, booleans are much more reliable.

I could of course have them all running constantly and use the boolean as a condition, but I assume not running at all is better.

run it every ten seconds

You can use a time pattern trigger to do that.

Thanks, but you misunderstand. I want to stop and start the running of them, I know about the time pattern trigger. That’s what I use to run it every ten seconds.

It’s hard to understand these details of what you have and want, but if I got it right, you could just add a condition to check the boolean. So that every 10 seconds the automations would run, but only if the boolean is true.

There are ways that I do similar things, but it’s complicated… The less complicated way is to just create an automation that turns on and off the 13 automations.

Set the trigger to be the state of the input boolean turning on. Put a repeat until in your actions with a delay of 10s until the state is off.

Maybe you can template the action to have it respond to 13 different triggers if you set the automation mode to parallel & use choose, but 13 separate automations would be more manageable if you want to disable specific zones.

@Fanful Thanks! So running 13 automations every 10 seconds and just stopping because of the condition doesn’t bog down a Pi 5 system? If it doesn’t I can simply do that, yeah.

@petro Of course it had to be complicated. :joy: Story of my life. Anyway, if the automations doesn’t bog down the system, I will do the boolean condition.

Edit, crossposting:
@ShadowFist I will try that first, thanks!

Just to be sure, this would be correct, right?

alias: "Foobar sone 1: Oppdatere radioinfo hvert tiende sekund"
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_boolean.foobarsone1aktiv
    to:
      - "on"
    from:
      - "off"
actions:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.foobar_sone_1_tittel
        - sensor.foobar_sone_1_artist
  - repeat:
      until:
        - condition: state
          entity_id: input_boolean.foobarsone1aktiv
          state:
            - "off"
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 10
            milliseconds: 0
        - action: automation.trigger
          metadata: {}
          target:
            entity_id: automation.foobar_sone_1_oppdatere_radioinfo_hvert_tiende_sekund
          data:
            skip_condition: true
mode: queued
max: 10

Why wouldn’t you just have this automation turn on when foobar is running instead of using the boolean? Seems like extra steps

Same goes for an automation that just turns on/off the other automations

alias: "Foobar sone 1: Oppdatere radioinfo hvert tiende sekund"
description: ""
triggers:
  - trigger: time_pattern
    minutes: "/10"
actions:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.foobar_sone_1_tittel
        - sensor.foobar_sone_1_artist

and

alias: ...
description: ""
triggers:
  - trigger from foobar
actions:
  - action: automation.turn_on
    metadata: {}
    data:
      entity_id: automation.foobar_sone_1_oppdatere_radioinfo_hvert_tiende_sekund

I just don’t like disabling and enabling automations, I have had a few “accidents” when Hass has restarted for some reason, so they didn’t turn off or on as they should. And I prefer to have as little as possible running constantly. But if the code I put in with the run until is correct, I have what I need. :grin:

1 Like

No, that doesn’t look quite right. Why are you targeting the automation in your last action?

Also, if you’re using individual automations per zone, mode should be set back to single to ensure you don’t end up with multiple instances of the same automation running.

Was just heading to bed so can’t really reply properly until tomorrow. Go through the automation, apply the changes & test.

I’ll check back on you tomorrow

Edit: put the update entity inside the sequence too. Missed that

Thanks! Not far from bed myself… I just found that one myself too, so more like this:

repeat:
  until:
    - condition: state
      entity_id: input_boolean.foobarsone7aktiv
      state:
        - "off"
  sequence:
    - delay:
        hours: 0
        minutes: 0
        seconds: 10
        milliseconds: 0
    - action: homeassistant.update_entity
      metadata: {}
      data:
        entity_id:
          - sensor.foobar_sone_7_tittel
          - sensor.foobar_sone_7_artist

The weird thing is that it doesn’t update the Scrape sensor. Suddenly it doesn’t update even if I do it manually. It only updates when Foobar starts, not during playback. I have to continue looking at that tomorrow. I couldn’t sleep because it didn’t work, and it was an Idiot error, so never mind…

So, all good?

1 Like

Yep, thank you! :+1:

1 Like

So what I’d do is a template entity that sets your polling frequency. And you’d use that in all your automations.

template:
- triggers:
  - trigger: state
    entity_id: ... # Whatever you want to use to turn on/off the polling.
  - trigger: time
    at: sensor.polling_frequency
  conditions:
  - condition: state
    entity_id: ... # whatever you need to make sure is on
    state: 'on'
  sensor:
  - name: Polling Frequency
    unique_id: 6b23d9b4-367f-4b05-9a4f-56218e8d8d3f
    device_class: timestamp
    state: "{{ now() + timedelta(seconds=10) }}"

Then your automations will just be:

triggers:
- trigger: time
  at: sensor.polling_frequency
action:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.foobar_sone_1_tittel
        - sensor.foobar_sone_1_artist

It’s complex, however it’s extremely robust and all you need to do is manage that template entities triggers.

This process will:

  1. Start exactly when you turn something on.
  2. Exactly 10 seconds after it turns on, it will poll.
  3. No crazy automations, only a somewhat crazy template.
  4. Survive restarts if you add a restart HA trigger. Probably should also add a template reloaded trigger as well.

There are more things you can do to ensure the polling frequency is always exactly 10 seconds through restarts and reloading, but it’s probably not worth the pain.

3 Likes

That looks very interesting! I will have a look at that during the weekend, thank you very much!

@petro I’m a bit confused, sorry. I didn’t find how to create a template entity. Is this something I should put in the config.yaml, one of the different kinds of template helpers, and in that case which one, or something else that I just don’t know about?

Yes that part should be in config.yaml.

1 Like

Thank you! So I need one for each zone in the audio system, or just the one? Because if I add a trigger: state with foobarsone7aktiv as the trigger to the code in config.yaml, it would mean that I need one per zone in the audio system. I can’t have all zones starting the poll because one is active, that would create the same problem. Or can I use some variant like foobarsone{{ trigger_id }}aktiv in this?