Trigger in automation based on history_stats

Hello

I have created a sensor based on the time an xbox is with an active user:

sensor:
  - platform: history_stats
    name: Xbox encendida hoxe Hugo
    entity_id: binary_sensor.xxxxx
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

so far so good, the sensor works correctly, it shows me the time it is on in hh:mm:ss format and in the developers section with a numerical value corresponding to the hours of use:
Captura de pantalla 2022-10-09 a las 20.16.05

Then I have created an automation that once it reaches 2 hours of time, it executes the actions that I specify:

  - id: '1665325040731'
    alias: apagar xbox hugo
    description: apagar xbox hugo
    trigger:
    - platform: numeric_state
      entity_id: sensor.xbox_encendida_hoxe_hugo
      above: '2'
    - platform: time_pattern
      minutes: /3
    condition:
    - condition: state
      entity_id: binary_sensor.xxxxx
      state: 'on'
    action:
    - service: notify.bot_domotica_grupo
      data_template:
        title: "\U0001f3ae *Aviso* \U0001f3ae"
        message: "Hugo superou o tempo de uso da Xbox, apago consola"
    mode: restart

The problem is that it always launches the actions, it ignores the value of the sensor (0,76), which is less than 2, what am I doing wrong?

Thank you very much for your help, I hope I explained myself well

What is the time pattern trigger for? Since you don’t have a general condition set for the automation, when the time pattern trigger fires the actions will proceed no matter how long the XBox has been in use.

That. That is what you are doing wrong. The automation is being triggered every 3 minutes.

1 Like

I have adapted the configuration of another post seen in the forum, from a ps4.
Automate Time Limit on PS4
So I don’t know exactly what the time pattern is for, the intention is that the automation runs every, so often for if the user’s session is active again. should i remove it then?

There is no need to continually check how long it has been used for, because that’s what your above: 2 is for (though if you want it to fire when it is 2 hours, it should be above: 1, above 2 mans it won’t trigger until 3 hours). The only condition checks that the binary sensor is on.

It’s a float, so it would fire at 2 hours and a few seconds.

1 Like

OK, I have checked the post you mentioned and I can explain what is going on.
In that post - they are using a 5 minute trigger to run the automation, and then a condition to check the numeric state of the sensor.

They are doing this, because as a trigger - it only fires when the threshold has been crossed, and will not fire again until the number falls below the threshold and rises again. So basically - it’s to prevent someone turning the PS4 back on after the automation turned it off.

Thanks for your help, my question is: so if the user logs in again with a time greater than 2 hours, will the automation be triggered again?

See post above. Basically though - no. Not unless you change it to use the sensor as a condition, and trigger the automation with a time pattern.

Exactly, that’s what I want, but how do I do the automation?
I’m blocked today, tomorrow I’ll try

You mirror the triggers in the conditions so that both cases are covered:

  - id: '1665325040731'
    alias: apagar xbox hugo
    description: apagar xbox hugo
    trigger:
    - platform: numeric_state
      entity_id: sensor.xbox_encendida_hoxe_hugo
      above: '2'
    - platform: state
      entity_id: binary_sensor.xxxxx
      to: 'on'
    condition:
    - condition: state
      entity_id: binary_sensor.xxxxx
      state: 'on'
    - condition: numeric_state
      entity_id: sensor.xbox_encendida_hoxe_hugo
      above: '2'
    action:
    - service: notify.bot_domotica_grupo
      data_template:
        title: "\U0001f3ae *Aviso* \U0001f3ae"
        message: "Hugo superou o tempo de uso da Xbox, apago consola"
    mode: restart
1 Like

Just a suggestion -

If it was me, and I was setting up the automation to turn off the games console with a grace time like in the post you linked to, I would be using a choose - so that if the binary sensor is on, and the sensor is above 2.04 then it switches off immediately with no grace warning. Because it has obviously been switched back on, and it’s the only way they will learn that rules are rules :wink:

1 Like