Updating sensor only in a little range of time of the day

I am using the component “Viaggia Treno Italianl Railroad”
just to monitor a single train in the morning.

Updating the sensor all day it seems to me a waste of resources (and, of course, it gives me a bunch of errors and it fills my log file).

How can I tell to update the sensor only between 5 AM and 7 AM ?

I do this:

I set the scan interval for the sensor to something very high e.g.

scan_interval: 86400

Then I have these automations and scripts which you could adapt for your use. They just create a loop which updates the sensor. I also have a button in Lovelace that can request a manual update for those ‘urgent-I-must-have-the-most-current-information’ situations:

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

      - service: script.set_interval_for_train_sensor

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

      - delay: "00:00:02"

      - service_template: >
          interval: >
            {% if states('sensor.next_train_to_xxx') == "No departures" %}
            {% else %}
            {% endif %}

  #=== Update the sensor and loop the script that sets the sensor interval 
      - service: homeassistant.update_entity
        entity_id: sensor.next_train_to_xxx

      - delay: "{{ interval }}"

      - service: script.set_interval_for_train_sensor
1 Like

I like this very much.
But I get a failure if the sensor is unknown.

:   File "set_state.py", line 15, in <module>
: TypeError: 'NoneType' object is not callable

Would it be possible to allow the script to create a new sensor if it does not already exist?

I used to get some errors…

It was a while ago but think that for my sensor (UK) at the end of the daily schedule it would sometimes report that the next train was in a stupidly long time but then have no actual data for the next train. I got around this by adding a line to my script.

          interval: >
            {% if states('sensor.next_train_to_xxx') == "No departures" or
                  states('sensor.next_train_to_xxx') | int > 30 %}

Oops, my reply was on the wrong subject. Please disregard