Turn on/off input boolean based on state change (integer increasing or equal)?

Hi,

I have a sensor that tracks/increments internet usage in gigabits. So, the value increments up (and the state changes) as bandwidth is consumed in Gigabits (rounded to 3 decimal places).

I’ve tried the following automation based on the last change but it won’t toggle on/off reliably -
switch:
input_boolean:
josh_online:
name: Josh is online
initial: off
icon: mdi:wifi

automation:

#josh online
- alias: J is online
  initial_state: 'on'
  trigger:
    platform: time
    minutes: '/1'
  condition:
    condition: template
    value_template: "{ (as_timestamp(now()) - as_timestamp(states.sensor.josh_data_today.last_changed)) != 'True' }}"
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.josh_online

#josh offline
- alias: J is online
  initial_state: 'on'
  trigger:
    platform: time
    minutes: '/1'
  condition:
    condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(states.sensor.josh_data_today.last_changed)) == 'False' }}"
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.josh_online

I also tried this as well to pick up on the integer value changing - but this did not work - I also tried just using toggle but that did not work either.

# - alias: Josh is online
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: sensor.josh_data_today
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.josh_online

And finally - I tried this - which did not seem to respond either…

# - alias: Josh is online2
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: sensor.josh_data_today
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
  action:
    service_template: > 
      {% if states.input_boolean.state == "off" %}
        input_boolean.turn_on
      {% else %}
        input_boolean.turn_off
      {% endif %}
    data:
      entity_id: input_boolean.josh_online

I am running the most recent release of HASSIO.
Any suggestions? Thanks.

1 Like

What I understand is that you want to have a binary sensor showing if your bandwidth is increasing or not?

In that case you don’t need an automation but can use a template binary sensor or treshold sensor.

Create a history statistics sensor that computes the bandwidth use over the last 1 minute.

Then create a treshold sensor with an upper limit of 0 (zero) for the statistics sensor.

Done!

Thanks! One follow-up. I’ve set up the history_stats sensor but I’m unclear as to what “state” to use for a bandwidth number that is going up. I was reading the binary sensor state: ‘on’ but I know that isn’t right. Do I need a value template for the state to see it going up?
Thanks!

  - platform: history_stats
    name: Josh online day
    entity_id: input_boolean.josh_online2
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration: 00:01:00

binary_sensor:
  - platform: threshold
    entity_id: sensor.josh_data_today
    upper: 0
    name: Josh_online2

The history stats sensor need to be based on Joshua Today

The binary sensor is a threshold sensor for (history above 0)

Perhaps I can find the time to provide an example tonight.

I misguided you. You don’t need a history statistics sensor, but a statistics sensor. This can measure another sensor over a periode of time. Please find below an example that I have tested with my power consumption.

sensor:
  - platform: statistics
    name: Power Consumption Normal Last Minute
    entity_id: sensor.power_consumption_normal
    max_age:
      minutes: 1

binary_sensor:
  - platform: template
    sensors:
      power_consumption_normal_changed:
        friendly_name: Power Consumption Normal Changed
        value_template: >-
          {{ state_attr('sensor.power_consumption_normal_last_minute_mean', 'change') > 0 }}

In your case, this should probably become:

sensor:
  - platform: statistics
    name: Josh Data Today
    entity_id: sensor.josh_data_today
    max_age:
      minutes: 1

binary_sensor:
  - platform: template
    sensors:
      josh_online:
        friendly_name: Josh Online
        value_template: >-
          {{ state_attr('sensor.josh_data_today_mean', 'change') > 0 }}

If the binary sensor is properly populated you can create an alert or automation to notify you.

Please let me know if this was of help.

Yes, very helpful. I’ve implemented it and for some reason the ‘change’ attribute does not return to zero when I stop streaming data. I also created a second sensor on the primary scraped data stream (with more digits) but it stays above zero and the binary sensor never switches off. Does it need to collect more data to operate effectively possibly? Definitely on the right track. Thanks again for the help. I’ll keep an eye on it and see if things change.

Config is same as you suggested above.

Example stat output:
mean: 0.96
count: 6
max_value: 0.966
median: 0.96
min_value: 0.957
sampling_size: 20
standard_deviation: 0
total: 5.77
variance: 0
change: 0.009000000000000008
average_change: 0.0018000000000000017
max_age: 2018-06-07T04:17:28.037370+00:00
min_age: 2018-06-07T04:16:35.169894+00:00
unit_of_measurement: Gb
friendly_name: Josh Data Today mean
icon: mdi:calculatormean

As a workaround, you might change the template to a change value of almost zero:

{{ state_attr('sensor.josh_data_today_mean', 'change') > 0.01 }}

Thanks. I did something similar last night. The root cause is i’m using a rounded number. I chose the raw data field with the number of bits and rounded/adjusted as you are suggesting. Here is where i’m at. Looks promising. I;ll let it run for the day and report back. Thanks

  - platform: template
    sensors:
      josh_online_gross:
        friendly_name: Josh Online Gross
        value_template: >-
          {{ state_attr('sensor.josh_data_gross_mean', 'change') |round(3) > 0 }}

So, the statistics are coming through
And I can see the binary sensor change:
But I cannot see the history stats. They are not showing any results.
Any thoughts? Thanks

  - platform: statistics
    name: Josh Data Today
    entity_id: sensor.josh_data_today
    max_age:
      minutes: 1

    binary_sensor:
    # measure change in change attribute and turn switch on/off
      - platform: template
        sensors:
          josh_online:
            friendly_name: Josh Online
            value_template: >-
              {{ state_attr('sensor.josh_data_today_mean', 'change') > 0 }}

  - platform: history_stats
    name: Josh online day
    entity_id: binary_sensor.josh_online
    state: 'True'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration: 00:01:00

What do you intend to use this history stats sensor for? I think it will show you the number of times you went online.

Yes, for the duration of time that I am on-line based on inbound data usage (scraped from a router page). The data usage all works well but I can’t seem to get the history sensor to populate off of the binary sensor.

It did work when I had a regular binary sensor (with my original automation binary sensor approach) but since i’ve been trying the history_stats, it won’t populate with the hours (and minuutes) that it is ‘True’, True, ‘On’, on, etc. I’ve tried every combo I can think of…

The history stats in the above example measures how long you have been online in the first minute of the current hour. In think.

You should set the duration differently.

What I would do is set the end time to now() and have a duration of 1 hour ‘1:00:00’. This sensor will then show how long you have been online in the last 60 minutes.

Thanks. Ah, yes. I see where I went wrong. after some fiddling, this now works. Thank you @metbril for your help! :

# sensor to show change in bandwidth usage using rounded data today
  - platform: statistics
    name: Josh Data Today
    entity_id: sensor.josh_data_today
    max_age:
      minutes: 1

binary_sensor:
# measure change in change attribute and turn switch on/off
  - platform: template
    sensors:
      josh_online:
        friendly_name: Josh Online
        value_template: >-
          {{ state_attr('sensor.josh_data_today_mean', 'change') > 0 }}

# J on-line in hours both daily and weekly totals
#daily
  - platform: history_stats
    name: Josh online day
    entity_id: binary_sensor.josh_online
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

#weekly
  - platform: history_stats
    name: Josh online week
    entity_id: binary_sensor.josh_online
    state: 'on'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'
1 Like

To improve my karma you could like some of my posts and mark one of them as the ‘solution’. :wink:

1 Like