timmo001
(Aidan Timson (Timmo))
November 28, 2017, 5:11pm
1
I want to be able to use templates to condition whether its a weekday and before a certain time. What I am looking for is:
If its a weekday and before 00:00, lights on, else if on a weekend before 02:00, else lights off.
Here is my automation so far:
- action:
- alias: Turn on lights/switches without presence detection
data_template:
entity_id: group.outside
service_template: >
{% if states('sensor.sn1_ldr') | float >= 630 %}
homeassistant.turn_off
{% else %}
{% IF WEEKDAY AND BEFORE MIDNIGHT %}
homeassistant.turn_on
{% else %}
{% IF BEFORE 2am %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% endif %}
{% endif %}
alias: Turn on/off other lights/switches
condition:
- after: '13:00:00'
before: '06:00:00'
condition: time
id: '1506045642915'
trigger:
- above: '630'
entity_id: sensor.sn1_ldr
platform: numeric_state
- below: '630'
entity_id: sensor.sn1_ldr
platform: numeric_state
Dolores
November 28, 2017, 6:10pm
2
I’m not sure what you’re trying to do, but it’s possible that a simple OR condition would help, combined with a time condition.
Also, the time condition allows you to specify days, which might further help.
treno
(travis reno)
November 28, 2017, 6:37pm
3
OK, well a couple of points…
There is no time that is less than 0 on any given day. Any time before 0 is yesterday.
Also, your code apparently does something quite different from your stated problem.
SO… I chose to implement your stated problem as a starting point for you.
Note : I included the (hour < 0) requirement even though we know that is not possible.
SO here is the template :
{% set curhour=now().hour%}
{% set wdFlag = now().weekday() in (0,1,2,3,4)%}
{% if wdFlag %}
{### weekday ###}
{%if curhour < 0 %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
{%else%}
{### weekend ###}
{% if curhour < 2 %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
{% endif %}
1 Like
timmo001
(Aidan Timson (Timmo))
November 29, 2017, 9:36am
4
Would this work?
{% set curhour = now().hour %}
{% if now().weekday() in (0,1,2,3,4) %}
{% if curhour <= 23 or curhour >= 13 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% else %}
{% if curhour <= 2 or curhour >= 13 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% endif %}
I think I have the signs matched up and the syntax looks ok I think…
treno
(travis reno)
November 29, 2017, 1:40pm
5
You have a line that says:
{% if curhour >= 23 or curhour >= 13 %}
when curhour >= 23 it will always be > 13 so this is equivalent to
{% if curhour >= 13 %}
1 Like
timmo001
(Aidan Timson (Timmo))
November 29, 2017, 1:42pm
6
I had the sign backwards, see edit
treno
(travis reno)
November 29, 2017, 1:46pm
7
That’s worse.
Every hour is either <= 23 or >= 13
Perhaps you meant <=23 AND >= 13
1 Like
timmo001
(Aidan Timson (Timmo))
November 29, 2017, 1:51pm
8
OK so,
{% set curhour = now().hour %}
{% if now().weekday() in (0,1,2,3,4) %}
{% if curhour <= 23 and curhour >= 13 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% else %}
{% if curhour <= 2 or curhour >= 13 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% endif %}
This will work?
timmo001
(Aidan Timson (Timmo))
November 29, 2017, 2:01pm
9
I just realised this would not trigger until light level changes. I have added another trigger to check at 1 minute past every hour:
- action:
- alias: Turn on lights/switches without presence detection
data_template:
entity_id: group.outside
service_template: >
{% if states('sensor.sn1_ldr') | float >= 630 %}
homeassistant.turn_off
{% else %}
{% set curhour = now().hour %}
{% if now().weekday() in (0,1,2,3,4) %}
{% if curhour <= 23 and curhour >= 13 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% else %}
{% if curhour <= 2 or curhour >= 13 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
{% endif %}
{% endif %}
alias: Turn on/off other lights/switches
condition:
- after: '13:00:00'
before: '06:00:00'
condition: time
id: '1506045642915'
trigger:
- above: '630'
entity_id: sensor.sn1_ldr
platform: numeric_state
- below: '630'
entity_id: sensor.sn1_ldr
platform: numeric_state
- platform: time
minutes: 1
seconds: 00
1 Like
Dolores
November 29, 2017, 4:24pm
10
I’m very very confused about what you’re trying to do. I think that if you say in plain english what you want to do (e.g. turn off the lights if it’s this way, turn them off if it’s that way), someone might be able to make this automation much more simple and robust.
Why not just have two automations, one to turn on the group when certain conditions are met, and another to turn it off when certain conditions are met? You could combine conditions with an “or” condition.
I like using templates, but this just strikes me as a nightmare to debug.
timmo001
(Aidan Timson (Timmo))
November 29, 2017, 5:10pm
11
You should see my other automation for my indoor lights and switches:
- action:
- alias: Setee light
data_template:
entity_id: light.setee_light
kelvin: 3000
brightness_pct: >
{% if states('sensor.sn1_ldr') | float >= 630 %}
0
{% elif states('sensor.sn1_ldr') | float >= 450 %}
60
{% elif states('sensor.sn1_ldr') | float >= 280 %}
70
{% else %}
80
{% endif %}
service: light.turn_on
- alias: PC lights
data_template:
entity_id: light.pc_light
kelvin: 3000
brightness_pct: >
{% if is_state('switch.pc', 'on') %}
2
{% elif states('sensor.sn1_ldr') | float >= 630 %}
0
{% elif states('sensor.sn1_ldr') | float >= 450 %}
15
{% elif states('sensor.sn1_ldr') | float >= 280 %}
20
{% else %}
25
{% endif %}
service: light.turn_on
# Xmas
- alias: Xmas lights (Inside)
data_template:
entity_id: group.xmas_lights
service_template: >
{% if states('sensor.sn1_ldr') | float >= 630 %}
homeassistant.turn_off
{% else %}
homeassistant.turn_on
{% endif %}
alias: Lights
condition:
- condition: state
entity_id: group.phones
state: home
- after: '13:00:00'
before: '06:00:00'
condition: time
id: '1507305996562'
trigger:
- above: '630'
entity_id: sensor.sn1_ldr
platform: numeric_state
- above: '450'
below: '630'
entity_id: sensor.sn1_ldr
platform: numeric_state
- above: '280'
below: '450'
entity_id: sensor.sn1_ldr
platform: numeric_state
- below: '280'
entity_id: sensor.sn1_ldr
platform: numeric_state
- entity_id: switch.pc
platform: state
to: 'on'
- entity_id: switch.pc
platform: state
to: 'off'
- platform: event
event_type: call_service
event_data:
service_data:
entity_id: scene.reset_lights
domain: scene
service: turn_on
- event: start
platform: homeassistant
This conditions against my pc being on, light level on my window sensor with a gradual increase of the living room bulbs and is triggered by my pc, light level, startup etc.
1 Like