Sensor readings - limit to specific time / day?

Hello,

Example - google_travel_time:

- platform: google_travel_time
  name: Home To Work
  api_key: !secret googlemaps_api
  origin: zone.home
  destination: zone.work
  scan_interval: 300

It works perfectly… however I don’t need sensor readings every 5 min on 365/7/24 but… but… only working days between 16:00-18:00

Is there a chance to limit sensor readings to specific time / day ??

Regards,
M

Any ideas?

You don’t want the readings to be carried out or you don’t want to see them on your dashboard?

I have no solution for the former, but you could achieve the latter with putting the sensor in a group and then make this group visible from 16h - 18h only.

Hello,

Thank you for answer.

I don’t want sensor readings all over the time (only specific time windows) so hiding them on dashboard doesn’t resolve my need.

Any other suggestions?

Maybe you can use a value_template in your scan_interval with an if-then-else condition in it.

There is a workday sensor you can can use

https://www.home-assistant.io/components/binary_sensor.workday/

I set up my own template sensor indicating working hours that could easily be adjusted - here’s a draft version:

- platform: template
  sensors:
    traveltime:
      friendly_name: TravelTime
# traveltime is Mon-Fri, starts at 16:00h and lasts until 18:00h
      value_template: '{{ now().weekday() in (0,1,2,3,4) and now().hour >=16 and now().hour <= 18 }}'

You might then be able to use the output of the traveltime sensor to adjust the scan-interval:

- platform: google_travel_time
  name: Home To Work
  api_key: !secret googlemaps_api
  origin: zone.home
  destination: zone.work
  scan_interval: 
    value_template: >-
      {%- if states.sensor.traveltime.state == 'true' -%} 300
      {%- else -%} 3600
      {% endif %}

Not really sure about the syntax and if you can use a value_template there at all, but I guess it’s worth a try.

Maybe your if-condition needs to look like this:

{% if is_state('sensor.traveltime.state', 'true') %}

Looks really interesting. I have to test it out.
Thank you :wink:

Are you sure if is possible to use value_template for changing scan_interval in platform (sensor) definition (like: google_travel_time) ??

I receive syntax errors in case of using value_template in this situation.

It appears you could make a really simple python script that you could run by shell command automation that will update the travel time sensor (i’d use mqtt)

Nope, not really sure about this.

Can you post the syntax error, please.

Thank you :wink:

I can try to write appdaemon app as well.
However it duplicates functionality… which is already built.

- Invalid config for [sensor]: [value_template] is an invalid option for [sensor]. Check: sensor->scan_interval->value_template. (See /home/hass/.homeassistant/configuration.yaml, line 127). Please check the docs at https://home-assistant.io/components/sensor/

Bummer - maybe data_template works instead of value_template.

It’s strange, though, that it is an ‘invalid option for [sensor]’ - I would have expected to be about the scan_interval.
Is your spacing set up properly in the value_template?

Hm… the same story with data_template:

- Invalid config for [sensor]: [data_template] is an invalid option for [sensor]. Check: sensor->scan_interval->data_template. (See /home/hass/.homeassistant/configuration.yaml, line 127
). Please check the docs at https://home-assistant.io/components/sensor/

I don’t think that it is spacing problem.
Do you have working example of this kind of configuration for any kind of sensor?

I have a number of sensors that use value_template in them, e.g. rest, template, command_line, and file sensors:

- platform: rest
  resource: http://ip.jsontest.com
  name: ExtIP
  value_template: '{{ value_json.ip }}'

- platform: template
  sensors:
    preciptotal4sprinkler:
      friendly_name: PrecipTotal4Sprinkler
      value_template: '{{ ((10*(states.sensor.PrecipForeCast.state | int)) + (20*(states.sensor.PrecipToday.state | int)) + (20*(states.sensor.PrecipYDay.state | int)) + (10*(states.sensor.PrecipYDay1.state | int)) + (6*(states.sensor.PrecipYDay2.state | int)) + (4*(states.sensor.PrecipYDay3.state | int)) + (3*(states.sensor.PrecipYDay4.state | int)) + (2.5*(states.sensor.PrecipYDay5.state | int)) + (1*(states.sensor.PrecipYDay6.state | int)) + (1*(states.sensor.PrecipYDay7.state | int)) + (1*(states.sensor.PrecipYDay8.state | int)) + (1*(states.sensor.PrecipYDay9.state | int))) }}'
      unit_of_measurement: 'U'
- platform: file
  name: Sprinkler Offset In Days
  file_path: /home/homeassistant/.homeassistant/files/sprinkler_offset_in_days.json
  value_template: '{{ value_json.sprinkler_offset }}'
  unit_of_measurement: 'd'

That’s why I was surprised to see the reference to ‘sensor’ in general rather than ‘scan-Interval’ specifically.

What happens if you try this:

  scan_interval: 
    value_template: 300

For:

  scan_interval:
    value_template: 300

I have:

- Invalid config for [sensor]: [value_template] is an invalid option for [sensor]. Check: sensor->scan_interval->value_template. (See /home/hass/.homeassistant/configuration.yaml, line 127). Please check the docs at https://home-assistant.io/components/sensor/

In the command_line sensor below I just changed

  scan_interval: 60

to

  scan_interval: 
    value_template: 60

and it works fine - so I think it’s not a general sensor or even scan_interval issue for the value_template.
Maybe it’s specific to the google_travel_time platform :confused:

Could you pate the last version of your config again?
But I’m basically out of ideas now.
Sorry!


# This will show the runtime of the HA Process on the Pi
- platform: command_line
  name: "HA runtime"
  command: echo "$(($(date +%s) - $(date -d "$(head -n1 /home/homeassistant/.homeassistant/home-assistant.log | cut -d' ' -f-2)" +%s)))"
  scan_interval: 
    value_template: 60
  value_template: >-
    {% set lasttime = value | int %}
    {% set minutes = ((lasttime % 3600) / 60) | int %}
    {% set hours = ((lasttime % 86400) / 3600) | int %}
    {% set days = (lasttime / 86400) | int %}
    {%- if lasttime < 60 -%}
      Less than a minute
    {%- else -%}
      {%- if days > 0 -%}
        {%- if days == 1 -%}
          1 day
        {%- else -%}
          {{ days }} days
        {%- endif -%}
      {%- endif -%}
      {%- if hours > 0 -%}
        {%- if days > 0 -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if hours == 1 -%}
          1 hour
        {%- else -%}
          {{ hours }} hours
        {%- endif -%}
      {%- endif -%}
      {%- if minutes > 0 -%}
        {%- if days > 0 or hours > 0 -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if minutes == 1 -%}
          1 minute
        {%- else -%}
          {{ minutes }} minutes
        {%- endif -%}
      {%- endif -%}
    {%- endif -%}

Last version of confit (doesn’t work):

- platform: google_travel_time
  name: Home To Work
  api_key: !secret googlemaps_api
  origin: zone.home
  destination: zone.work
  scan_interval:
    value_template: 300

Thanks for help anyway.

Greetings, M

1 Like