Hi
I am trying to get the following code to work with my hallway motion sensor and to turn off the light:
If there has been no motion for 30 seconds with the following conditions:
2a) The light is already on
2b) The sensor.day_night is either ‘day’ or ‘night’
2c) If sensor.day_night is ‘day’ then the sun.sun has to be ‘above_horizon’
I have done the following:
sensor.day_night
- platform: template
sensors:
day_night:
friendly_name: 'Time of Day'
value_template: >-
{%- if now().hour >= 18 and now().hour < 23 %}
Evening
{% elif now().hour >= 23 and now().hour < 6 %}
Night
{% else %}
Day
{%- endif %}
hallway_motion_off.yaml
alias: 'Hallway Motion Off'
trigger:
- platform: state
entity_id: sensor.hallway_motion_sensor
to: 'OK'
for:
seconds: 30
condition:
condition: and
conditions:
- condition: state
entity_id: light.hallway_1
state: 'on'
- condition: state
entity_id: sun.sun
state: 'above_horizon'
- condition: or
conditions:
- condition: state
entity_id: sensor.day_night
state: 'Day'
- condition: state
entity_id: sensor.day_night
state: 'Night'
action:
service: homeassistant.turn_off
entity_id: light.hallway_1
data:
transition: 5
But of course that doesn’t turn the light off over night when sensor.day_night is ‘night’ as the sun isn’t ‘above_horizon’.
Can you have a AND condition within an OR statement like this:
alias: 'Hallway Motion Off'
trigger:
- platform: state
entity_id: sensor.hallway_motion_sensor
to: 'OK'
for:
seconds: 30
condition:
condition: and
conditions:
- condition: state
entity_id: light.hallway_1
state: 'on'
- condition: or
conditions:
- condition: state
entity_id: sensor.day_night
state: 'Night'
- condition: and
conditions:
- condition: state
entity_id: sun.sun
state: 'above_horizon'
- condition: state
entity_id: sensor.day_night
state: 'Day'
action:
service: homeassistant.turn_off
entity_id: light.hallway_1
data:
transition: 5
Thank you guys. Getting the following in the log though
2017-12-12 15:31:23 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: not a valid value for dictionary value @ data['condition'][1]['conditions'][1]['condition']. Got None
required key not provided @ data['condition'][1]['conditions'][1]['entity_id']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/
OK this is driving me mad. I have used the following script by using the suggestions above and other posts on the forum:
alias: 'Hallway Motion Off'
trigger:
- platform: state
entity_id: sensor.hallway_motion_sensor
to: 'OK'
for:
seconds: 30
condition:
condition: and
conditions:
- condition: state
entity_id: light.hallway_1
state: 'on'
- condition:
- condition: or
conditions:
- condition: state
entity_id: sensor.day_night
state: 'Night'
- condition: template
value_template: '{{ is_state("sun.sun", "above_horizon") }}' and '{{ is_state("sensor.day_night", "Day") }}'
action:
service: homeassistant.turn_off
entity_id: light.hallway_1
data:
transition: 5
But even though HASS.io check says its valid I get the following error in the log:
2017-12-14 22:04:27 ERROR (SyncWorker_0) [homeassistant.util.yaml] while parsing a block mapping
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 20, column 11
expected <block end>, but found '<scalar>'
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 72
2017-12-14 22:04:27 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 20, column 11
expected <block end>, but found '<scalar>'
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 72
Can anyone spot what I have done wrong? It’s seemingly moaning about the -condition: template.
alias: 'Hallway Motion Off'
trigger:
- platform: state
entity_id: sensor.hallway_motion_sensor
to: 'OK'
for:
seconds: 30
condition:
condition: and
conditions:
- condition: state
entity_id: light.hallway_1
state: 'on'
- condition:
- condition: or
conditions:
- condition: state
entity_id: sensor.day_night
state: 'Night'
- condition: template
value_template: {{ is_state("sun.sun", "above_horizon") and is_state("sensor.day_night", "Day") }}
action:
service: homeassistant.turn_off
entity_id: light.hallway_1
data:
transition: 5
and get the following error
2017-12-15 08:58:52 ERROR (SyncWorker_0) [homeassistant.util.yaml] while parsing a flow mapping
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 28
expected ',' or '}', but got '<scalar>'
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 65
2017-12-15 08:58:52 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a flow mapping
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 28
expected ',' or '}', but got '<scalar>'
in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 65
If I surround the template with ’ ’ I get loads of errors in the log.
2017-12-15 15:40:13 ERROR (SyncWorker_0) [homeassistant.util.yaml] mapping values are not allowed here
in “/config/includes/automation/motion/hallway_motion_off.yaml”, line 22, column 18
2017-12-15 15:40:13 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/includes/automation/motion/hallway_motion_off.yaml”, line 22, column 18
So assume I cannot use the transistion or brightness field. How do I apply these in this context? Thanks
You can only turn on a light with colour / brightness data. Turning off a light… well, the light is off, so no data required / accepted. If you want the light to have a certain set of parameters set in it ready for when it is next turned on you would need to send a light.turn_on first with those settings, then follow it up with a simple light.turn_off.
Or if you really just want to set the color temperature while turning off the light; you can use the turn on command, setting brightness to 0 and color temp to the desired value. Worth a try.