Hi I’m having difficulties using conditions in my automation script. It does validate but it doesn’t work. This is my second automation so far so I’m sorry for this noob question The docs should include more basic examples because the current ones didn’t help me.
- alias: 'Turn on bureaulamp after 14:00'
trigger:
platform: time
after: '14:00:00'
condition:
- condition: time
after: '14:00:00'
before: '23:00:00'
action:
service: light.turn_on
entity_id: light.bureaulamp
This script is located in ‘automations.yaml’ with ‘automation: !include automations.yaml’ in ‘configuration.yaml’
eventually I want to have this automation: “When I am home turn on lamp between 19:00 and 0:00”
The trigger in this case will be my iphone state “home”.
Your script works like a charm!! But only when I am outside my home zone and entering home. What I’d like to have is that HA checks if I’m at home AND if time is between 19:00 and 23:59 then turn on the lights. I changed some of your code but can’t get it to work. I check for two conditions and I think the trigger isn’t working. It should trigger when starting up HA. And yes I changed the time condition to someting after few hours ago and before somewhere in the future.
This is what I have so far:
- alias: 'Turn on lamp'
trigger:
- platform: template
value_template: "{% if is_state('device_tracker.iphone', 'home') %}true{% endif %}"
condition:
- condition: state
entity_id: device_tracker.iphone
state: 'home'
- condition: time
after: '00:01:00'
before: '03:45:00'
action:
service: light.turn_on
entity_id: light.bureaulamp
I’d hit this with two automations that both take the same action of turning on the light, since it sounds like you really have two situations you want to turn on the light.
The first automation would trigger on your device status changing to home, with the condition that the time is between 1400 and 2300. This covers you if you are not home at 1400 but want the light on when you arrive.
The second automation would trigger at 1400 on the condition you are home. This covers you if you are already home at 1400.
I expect you may also want a third automation that turns the light off at 2300 with no conditions.
- alias: 'Turn on lamp'
trigger:
- platform: state
entity_id: device_tracker.phone
to: 'home'
- platform: time
at: '19:00:00'
condition:
- condition: time
after: '19:00:00'
- condition: state
entity_id: device_tracker.phone
state: 'home'
action:
service: light.turn_on
entity_id: light.bureaulamp
so basically, the light will turn on either at 19:00 or when you get home, providing it is after 19:00 and your are home.
Note also that if the condition after: 19:00 automatically gets false at midnight, so no need to add before: 23:59.
EDIT: On the plus side, if you turn_off the light, let’s say at 20:00, then go away, then come back at 21:00, it will turn_on once again as it will re trigger when you get home.
Will try this one in a moment. I fiddled with some other code before you posted this solution. My current automation looks alot like yours. And @vladosam I did use a time interval in my setup. HA has a lot of flexibility
The above script did actually work as it should. Thanks everyone for your help. I had to change some tracker settings in configuration.yaml to get the script to trigger. @touliloup said something about presence automation and lights that got me thinking about my device_tracker:
If you want to test the condition and the trigger, you should simply disconnect your phone from the wifi and wait for the state to change in HA. Reconnect it and see if it turns the light on or not.
Limit the number of devices you scan. … I scan only a maximum of 10 devices.
What I did was give these devices a fixed IP address in a specified range I scan with NMAP.
So I changed my device_tracker (after fixing the IP’s (DHCP) I want to track in modem) to this:
In practice it’s not that every state change of my phone is detected in 10sec. It’s more like 2 or 3 minutes. For now I’m ok with that. I consider this as solved. Thanks again