How do I reset a templated sensor?

I’m detecting user interference (a user overriding an automation / setting lights to what they want) with the following in my configuration.yaml. I have a Wiz remote in my media room and it can change the lighting effect.

template:
  - trigger:
      - platform: state
        entity_id: light.media_room_ceiling_light_group
        attribute: effect
    sensor:
      - name: "Media Room Ceiling Light Group Effect Context"
        state: >
          {% set c_id = trigger.to_state.context.id %}
          {% set c_parent = trigger.to_state.context.parent_id %}
          {% set c_user = trigger.to_state.context.user_id %}
          {% if c_id != none and c_parent == none and c_user == none %}
            physical
          {% elif c_id != none and c_parent == none and c_user != none %}
            dashboard_ui
          {% elif c_id != none and c_parent != none and c_user == none %}
            automation
          {% else %}
            unknown
          {% endif %}

This works great, as expected. When a user touches a remote control to manually change the lights then the state changes to physical.

My problem is that the state stays on physical. Of course if an automation comes behind them then it will change to automation.

If the user interacts with the lights again / a second time the sensor is not updated. It was already on physical and since the most recent user interaction should make it go to physical it doesn’t change.

I need the sensor to go back to a null string / nothing after a few seconds. I want to be able to detect the second user interaction with the lights.

Is there any way that I can have this template sensor revert to nothing / empty a few seconds after it changes to something?

Add a second trigger:

template:
  - trigger:
      - platform: state
        entity_id: light.media_room_ceiling_light_group
        attribute: effect
      - platform: state
        entity_id: sensor.media_room_ceiling_light_group_effect_context
        not_to: 'unknown'
        for: "00:00:30"
        id: reset
    sensor:
      - name: "Media Room Ceiling Light Group Effect Context"
        state: >
          {% if trigger.id == 'reset' %}
            unknown
          {% else %}
            {% set c_id = trigger.to_state.context.id %}
            {% set c_parent = trigger.to_state.context.parent_id %}
            {% set c_user = trigger.to_state.context.user_id %}
            {% if c_id != none and c_parent == none and c_user == none %}
              physical
            {% elif c_id != none and c_parent == none and c_user != none %}
              dashboard_ui
            {% elif c_id != none and c_parent != none and c_user == none %}
              automation
            {% else %}
              unknown
          {% endif %}{% endif %}

Although, if it were me, I would use a state that doesn’t already have a meaning instead of unknown.

1 Like