Using Counter as a template to trigger automations

I have a door sensor on my home office door. I tried to use the sensor to turn the lights on when I entered and off upon exit. I was able to achieve success with the help of the Counter, which is incremented every time the state is opened.

I wanted to use the counter’s count to determine if the lights should be turned on (upon entry) or off (upon exit). To do this I created an automation and used the Template as the trigger. In the template I coded:

{% if (states('counter.dad_office_door_counter') | int is odd) %}
true
{%endif%}

On this automation, I used to turn the lights on. I replaced “odd” with “even” for the exit automation.

My assumptions are

  1. When entering the counter goes from 0 to 1 (odd) and that is the start of the day. That’s when I want the lights on.
  2. When exiting, the counter goes from 1 to 2 (even) and means that I’m opening the door to leave and the lights should be turned off.

I know I’m not factoring in the office visitor. Who comes in, but this satisfies my needs to about 90%.

I’m sure there might be another way to accomplish this, if you have any ideas let me know what you think.

So every time you have to open the door and don’t leave, you’re okay with the lights going bonkers till you open a close the door? Sounds problematic.

The better way really is to add some sort of presence detection instead. Motion detector? Laptop powered on? tv on? etc… and use that to decide if the lights should turn off when the doors open.

That’s a good idea to add an additional condition based upon the computer being unlocked or something else. 90% of the time the door is only opened if I leave.

Pressure sensor on the chair?
Won’t be good as an alone sensor but together with other sensors it could give you a clue if you are in the office or not and if it’s someone else opening the door.