Trouble using condition in simple light automation script

Replace ‘after’ with ‘at’ in trigger.

1 Like

That did validate. But after restarting didn’t trigger anything.

Don’t forget that ‘at’ works different than ‘after’… At means it will only be triggered at that specific time…

I might be mistaken, but don’t you need a time sensor?

Do i need to add ‘time:’ to configuration.yaml?? I Think that should do it! Testing it now.

Check here how time triggering is supposed to work:

If you add the time component, you can do it similar to what I did here, but changing the value_template condition…

I did add the time component. This is a snippet from configurations.yaml

# Track the sun
sun:

time:

# Weather Prediction
sensor:
  - platform: yr
  - platform: time_date
    display_options:
    - 'time'

Still. The configuration checker responds with:

2017-07-27 18:41:05 ERROR (MainThread) [homeassistant.loader] Unable to find component time
2017-07-27 18:41:05 ERROR (MainThread) [homeassistant.setup] Setup failed for time: Component not found.

What you want to do with this automation? To turn on lamp in 14:00:00? Remove condition. And try to set 5 minutes from now to test it.

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”.

Try this:

- alias: 'Turn on lamp'                                                                                                    
  trigger:                                                                      
    platform: state                                                             
    entity_id: device_tracker.phone               
    from: 'not_home'                                                                 
    to: 'home'  
  condition:  
  - condition: time
    after: '19:00:00'
    before: '23:59:59'  
  action:
    service: light.turn_on
    entity_id: light.bureaulamp

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 believe value_template trigger don’t work because it waits for state change.

Maybe this will work:

 - alias: 'Turn on lamp'
   trigger:
     - platform: time
       minutes: '/1'                                 
       seconds: 00  
   condition:
     condition: and
     conditions:
       - condition: state
         entity_id: device_tracker.iphone
         state: 'home'
       - condition: time
         after: '14:00:00'
         before: '23:59:59'
       - condition: state                                                                             
         entity_id: light.bureaulamp                                                                    
         state: 'off'   
   action:
     service: light.turn_on
     entity_id: light.bureaulamp
1 Like

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.

@vladosam, @johnskoglund, @billmi you can use multiple triggers

- 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.

3 Likes

This automation is perfect for johnskoglund. :smiley: very nice.:+1::+1:

1 Like

oops, I read the discussion a bit too quickly, I thought it was for you… I tagged him in the message too so he can see the solution :wink:

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. :wink: HA has a lot of flexibility :+1:

Very nice, cleaner and simpler, as the conditions don’t contradict for the two situations.

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.

@dennisaion and @turboc had some good tips here:

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:

device_tracker:
  - platform: nmap_tracker
    hosts: 
      - 192.xxx.xxx.xx
      - 192.xxx.xxx.xx
      - 192.xxx.xxx.xx
    interval_seconds: 10
    track_new_devices: no

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 :+1:

2 Likes

For this I can only advise you to use a router with integration in HASS, it will be much more reactive than using nmap_tracker.

I bought a Xiaomi Router Mini for 24,67€ and wrote the integration for hass.
Using the router as a device tracker, the action are executed (in my case a light goes on) in about 1-5 seconds upon connection to the wifi.
And in addition it’s really a good router :slight_smile: (only use this model if your internet speed is under 100Mbit/s, the wan port is limited to 100Mbit/s).

1 Like