I have an automation to turn on my christmas tree after certain time, but only when motion is detected, so it doesn’t turn on if we are not at home.
Currently it has a fixed turn off time, and that is the part I want to change.
I want to make it turn off after 30 minutes of the motion sensor not detecting any motion, but only after certain hour. And I want this to be all in a single automation, I don’t like having several automations for a single task.
It will not work, at least not for all cases.
That will turn it off all the times the sensor has not detected motion for 30 minutes after sunset, which could be a valid tradeoff, but while we are at the couch this is kind of common, so it’s not a valid solution for this case.
Yes, in form of a “local” path
If you have file-editor, terminal or samba installed, this is one way how/where you can read the code of that blueprint,
It’s not an exact example of your requirements, but one you could use by i.e adding additional functionality as you are using a occupancy-sensor , but you also want other conditions involved
i.e yes 30min is as you say sometimes a short time, i.e your sitting on the “Cann” , so maybe you want to use additional “entities” involved in your Scenario
Im not sure why you initially want the “occupancy” as the “turn on” as a trigger, as i assume you want the xmas tree to light, “after sunset” when you are home(and not sleeping), which makes makes the “after sunset” as “turn on trigger” and “occupancy” as " 1 condition "
So which other “presences” sensors/switches etc, lights/door/windows/temps etc, do you have home which indicates that you are home, but not i.e lying on the couch,sitting on the Cann or “sleeping/gone to bed” ?
Just let it turn on when there is motion.
Select something else to turn off.
I use a button on the wall that shuts down my house. Like lights, tv, heating, door lock and turn off Christmas tree lights. When I go to bed.
You want those lights on when you home anyway.
Yes. But read his problem. Give a good solution. Not problems or more stuff to buy.
More people live in his house.
He wants his tree to turn on when somebody is home. So… motion, turn on tree. You can give it a condition (but you want that tree on when you home)
You want it off when you go to bed.
So…. At “tree off” to your AUTOMATION that triggers “going to bed” if you have that.
Bud sometimes it’s easier (living with more people in one home) to just hit a switch that runs your going to bed automation.
i know and i live alone ;), and actually have and automation, triggered by a remote-button in a buildin shelve when im passing the HallWay to my bedroom, which turn of light i,ve forgotten, and enable motion-sensors for path-way for other lights, under bed lights, and nightlights, if i need to go to the toilet in the nights.(these turns of 1min after no motion)
Sounds like exactly what I have. My below automation turns a light on each night, then turns it off if no motion is detected for 30 mins. I have a condition for the alarm being disarmed but you could omit that and include a condition within the ‘turn off’ action instead.
Yes but i think it’s the fixed time 30min(no motion) he want to address as they might cuddle on the couch(no motion there) in livingroom for 31min …but/so they don’t want the lights to turn off then
Edit: thats why i encourage him to look at their “routines” to find an additional entity states to define whether the either don’t move or out of reach from a specific/all motion sensors, but still are home And not in bed
Thank you, but that automation turns on the light unconditionally after sunset. I want the trigger to be the motion sensor, and the condition to be after sunset. Sadly, you can’t put both triggers as AND, that’s a home assistant limitation.
Condition to turn on:
Motion AND after sunset
Condition to turn off:
No motion for 30 minutes AND after 22:00
Both conditions needs to be AND. The workaround is to put the conditions in the conditions of a choose, but that is cumbersome at best
Yes you can, simply add a condition in the action section similar to how I have already. I’ll create a fresh automation for you in the morning, getting late for me now
I think, you gonna need a binary helper or template binary_sensor, in case the motion occurs after sunset but stops before 22:00. So you’ll need to save the motion stop information and check it at 22:00 (or later) - and then turn the light off if it’s on.
A trigger-based template sensor is like an automation that can also save data into itself. You can use this sensor directly (pasting it into configuration.yaml in the “template:” section, and reloading templates in Developer Tools) or recreate the same logic using an automation + input_binary helper.
Sensor code
# uncomment if there's no "template:" section in configuration.yaml
#template:
- trigger:
- trigger: state
entity_id:
- binary_sensor.motion_7_occupancy
to:
- "on"
id: movement
- trigger: state
entity_id:
- binary_sensor.motion_7_occupancy
to:
- "off"
for:
hours: 0
minutes: 30
seconds: 0
id: 'no-movement'
- trigger: time
at: "22:00:00"
id: 'time-off'
# last 2 trigger are to give the binary sensor initial state other than 'unknown'
- trigger: homeassistant
event: start
- trigger: event
event_type: event_template_reloaded
action:
- variables:
# true if after sunset and before midnight
after_sunset: "{{ is_state('sun.sun','below_horizon') and as_datetime(state_attr('sun.sun','next_rising')).day != now().day }}"
# true if after 22:00 and before midnight
after_22: "{{ now().hour >= 22 }}"
# true if movement sensor is off for at least 30 min
off_30_min: "{{ is_state('binary_sensor.no_motion_for_30_min','on') }}"
# current state of light switch
switch_on: "{{ is_state('switch.impresora', 'on') }}"
- if: "{{ trigger.id == 'movement' and after_sunset and not switch_on }}"
then:
- action: switch.turn_on
target:
entity_id: switch.impresora
- if: "{{ ((trigger.id == 'no-movement' and after_22) or (trigger.id == 'time-off' and off_30_min)) and switch_on }}"
then:
- action: switch.turn_off
target:
entity_id: switch.impresora
binary_sensor:
- name: "No motion for 30 min"
# requires HA core >= 2025.12
#default_entity_id: binary_sensor.no_motion_for_30_min
unique_id: 6c0baa9a-0826-4763-a79f-31228d444a6a
state: >-
{% if trigger.id == 'no-movement' %}
on
{% elif trigger.id == 'movement' %}
off
{% else %}
{% set current = this.state|default('off') %}
{{ current if current in ['on','off'] else 'off' }}
{% endif %}
Note: we read the state of the very sensor we create, off_30_min: "{{ is_state('binary_sensor.no_motion_for_30_min','on') }}"
This is fine but we need a proper entity_id for the sensor, which is by default derived from its name - name: "No motion for 30 min". So if you change the name of the sensor, make sure to also change it in the other place. Alternatively, if you’re on the latest version of HA core (2025.12), you can add this to sensor definition:
Wow, that is very complicated and impressive. Is this all a single template sensor?
So it can have as many triggers as you want? And actions and finally a state?
Yes, exactly. You can also add attributes. Sensor states and attributes are essentially private storage/“helpers” for a given trigger. This is very powerful, and unlike normal template sensors, the data survives HA restarts.
E.g. say you want an attribute with the lowest temperature for current day. You’d need 2 triggers: for state change of temp. sensor and for time=midnight.
- trigger:
[...]
sensor:
- name: Some Sensor
state: "Some other stuff"
attributes:
lowestTemp: >-
{% set current = this.attributes.get('lowestTemp',0) %} <== this is how you read current value, 0 is a default in case the attribute doesn't yet exist
{% if trigger.id == 'temperature_trigger' %} <== new state of temp. sensor
{% set newValue = trigger.to_state.state|float(0) %}
{{ newValue if newValue < current else current }} <== return newValue if smaller than current value, otherwise return current
{% elif trigger.id == 'midnight_trigger' %} <== midnight
{{ states('sensor.temperature_living_room')|float(0) }} <== start new day with current value of temp. sensor
{% else %}
{{ current }} <= any other trigger (i.e. not for this attribute): just return current value
{% endif %}