Looping scripts - what is going on here?

I have a really simple script loop as follows with some questions below. Please can someone help me with some answers? Thank you.


automation:
  #=============================================
  #=== Set up timing of sensor calls for trains
  #=============================================
  - alias: trains_sensor
    initial_state: on
    trigger:
      - platform: homeassistant
        event: start

    action:
      - service: script.set_interval_for_train_sensor

script:
  #===================================================
  #=== Set interval for the train sensor
  #=== Reduce sensor calls when no train is scheduled
  #===================================================
  set_interval_for_train_sensor:
    sequence:
      - service: homeassistant.turn_off
        entity_id: script.update_train_sensor

      - service_template: >
          script.update_train_sensor
        data_template:
          interval: >
            {% if states('sensor.next_train_to_pad') == "No departures" %}
              00:30:00
            {% else %}
              00:01:00
            {% endif %}

  #========================================================================
  #=== Update the sensor and loop the script that sets the sensor interval
  #========================================================================
  update_train_sensor:
    sequence:

      - delay: "{{ interval }}"

      - service: homeassistant.update_entity
        entity_id: sensor.next_train_to_xxx

      - service: script.set_interval_for_train_sensor

So, why do I need the homeassistant.turn off? If I leave this out then at start up I get an error saying the script is already running

But most importantly why does this not work as I expect? I am expecting that the sensor will normally be updated every minute but in fact it happens every two minutes. I watch the scripts and the script update_train_sensor cycles on for a minute but then off for a minute. Why is it ever off (apart from for a very short time when it finishes running each cycle)?

Have you watched the logs for when the events fire? That would give you a better idea other than watching the interface. The interface never gives the full story where the logs tell you everything. Turn on info and watch the logs.

Thanks for the advice (I needed to set to DEBUG though).

I might have solved this and as so often is the case it was something completely unexpected - or perhaps I should say un-thought-of.

The uk_transport sensor normally restricts the number of api calls so that you don’t go over the limit (which is about 87 seconds between calls). I believe this might be hard coded into the sensor in such a way that even if you set a high scan_interval it will still prevent an update until 87 seconds has passed since the last one. All of which means my update after one minute would fail, but the second one after two minutes would work.

@robmarkcole - Is this correct? I hope so because I have spent far to long on this already!! :wink:

EDIT:
AArrgghh!! I don’t think that is the issue. I set the delay to “00:01:30” which is more than 87 seconds and it still only updated on the second and every subsequent second pass i.e. after every three minutes.

My testing, using logs and notifications tell me that it is was definitely running through script.update_train_sensor every minute (before I changed it to “00:01:30”).

I have no idea what is going on - I’m grateful for any indicators!!

The source code is here. The calls to the api are Throttled, but the time period is calculated using interval = timedelta(seconds=87*number_sensors). So if you have 2 sensors the update time is once every 2*87 seconds. Please clarify what you want to achieve? Cheers

Since you ask… :wink:

I have currently have one sensor because of the api limit, but would like to have two.

With the new update_entity service I thought I might be able to create scripts that would update only during times when the trains I am interested in were timetabled. This would have freed up spare api calls for a second sensor.

Can you clarify if, with the scan_interval set high, the sensor will update every time the update_entity service is called irrespective of when it was last updated? Or does it have logic built in to prevent that?

The way your platform works really isn’t a problem for me I was just trying to work out why my simple script loop appears to work in an unexpected way.

Thanks.