If statement in automation to determine triggers "for:" time

you should try the below adjustments (always use correct yaml spacing. Also you’d best use the correct service)

- alias: "Bathroom lights off when no motion"
  initial_state: 'on'
  trigger:
    - platform: template
      value_template: >
        {{ (states.sensor.bathroom_motion_sensor.state == 'off' and 
            states.sensor.zha_01ddaf89_1_1029.state | int < 50 and 
            (now() - states.sensor.bathroom_motion_sensor.last_changed).total_seconds() > 1*60) or 

            (states.sensor.bathroom_motion_sensor.state == 'off' and 
             states.sensor.zha_01ddaf89_1_1029.state | int >= 50 and 
            (now() - states.sensor.bathroom_motion_sensor.last_changed).total_seconds() > 2*60) }}
  action:
    service: light.turn_off
    entity_id: light.bathroom

I wont reiterate on advising to use an intermediary sensor. Just read this again, and see if all aspects apply: Automation Trigger - Home Assistant

The automation is still not firing. I have not idea why it is so random, sometimes it fires but not at the right time…

I did not understand the intermediary sensor usage.

Problem might be the fact that sensor and HA are using different timezones.

{{ now() }}

{{ states.sensor.bathroom_motion_sensor.last_changed }}

shows

2019-01-07 18:42:36.800584+02:00

2019-01-07 16:40:04.055274+00:00

How can I change sensors timezone to +02?

Edit: Changed the timezone of HA to +00 but it did not help with this.
@Mariusthvdb could you help with the intermediary sensor thing and elaborate a bit more what you mean?

Will do but please give me some time , out of office now .

You could start with using the state == ‘off’ bit as condition and not in the trigger.

Makes it simpler already . Be back later

No hurry, have to stop for today :slight_smile:
Thanks guys again for help.

just to recap…

if you say:

am I correct in saying it like this:

you want to switch off the light 5 minutes after no motion has been detected (motion_sensor == ‘off’) unless humidity is >50 in which case you want 15 minutes?

this would be something like

- alias: "Bathroom lights off when no motion"
  trigger:
    platform: state
    entity_id: sensor.bathroom_motion_sensor
    to: 'off'
  condition:
    condition: time
    after: '07:00:00'
    before: '00:00:00'
  action:
    - delay:
        minutes: >
          {{ 15 if states('sensor.zha_01ddaf89_1_1029' ) | int >= 50 else 5 }}
    - service: light.turn_off
      entity_id: light.bathroom

if this is the correct logic, let me know.
as always, study the docs, the scripting language is rather powerful and this would be the relevant bit for the delay part: Script Syntax - Home Assistant

That is correct. But I get this:

Error loading /config/configuration.yaml: invalid key: "OrderedDict([("2 if states('sensor.zha_01ddaf89_1_1029') | int >= 50 else 1", None)])"

But I will read the documentation

ah ok, its a template so you probably need

{{ '15' if states('sensor.zha_01ddaf89_1_1029' ) | int >= 50 else '5' }}

if not the sensor might be incorrect, I’ve checked the template here and it works as expected:

21

what does

{{states('sensor.zha_01ddaf89_1_1029' )}} result in in dev-template?

Initial testing worked twice! So that might be it. The code is now like this

- alias: "Bathroom lights off when no motion"
  trigger:
    platform: state
    entity_id: sensor.bathroom_motion_sensor
    to: 'off'
  condition:
    condition: time
    after: '07:00'
    before: '00:00'
  action:
    - delay:
        minutes: "{{ '2' if states('sensor.zha_01ddaf89_1_1029' ) | int >= 50 else '1' }}"
    - service: light.turn_off
      entity_id: light.bathroom

Will test more tomorrow. Thank you for now!

cool.
according to the docs, use time condition with an extra set of :00 , as in my example

also make it easy on yourself and use multiline notation, to do away with the " " around the template, again see my example.

Hope to hear positive results tomorrow :wink:

Quick testing was done.

The automation seem to choose always “else”. And it might become a problem that if the sensor goes to “off” and timer begins, it will not reset if there is motion again…

I changed the if part to this:

  action:
    - delay:
        minutes: "{{ 15 if states('sensor.zha_01ddaf89_1_1029' ) | int >= 35 else 4 }}"

Edit: That did not help. It will not wait 15minutes

this is exactly why I suggested to create the intermediary binary_sensor in post 5… It does exactly that: times a set period, and if during that period motion is detected, it starts again.

doesn’t take away the fact that your sensor template is behaving unexpectedly. As said, it works as expected in my test setup, so the only variable should be your sensor…

did you test only the template in the dev-template?
don’t want to muddle your reasoning, but why would you want the light to be on longer when the humidity is higher? shouldn’t you rather want to ventilate?

it will only wait 15 minutes if the humidity is >= than 35. so show me the dev-template in that condition, so we can see what is happening?

you can even manually set it to see if the template works alright:

{%set humid = 35 %} # or 4
 - delay:
        minutes: "{{ 15 if humid | int >= 35 else 4 }}"

if that works (and it does, as Ive just tested it), you know it an only be the sensor causing the issue.

Wow, I can’t believe that you just tried to straw man my point so blatantly.

Of course those two things aren’t the same but that wasn’t my point at all and I’m fairly sure you know that.

The point I was making was about the two comparison statements based on this:

You suggested that the following was “better”:

than what I was using, which you said that “you don’t want to do”; and that was:

((as_timestamp(now()) - as_timestamp(states.sensor.bathroom_motion_sensor.last_changed)) / 60 )| int &gt; 15)

Which when converting to the total_seconds() method is equivalent to:

{{ (now() - states.sensor.bathroom_motion_sensor.last_changed).total_seconds() / 60 &gt; 15 }}

So those two statements are functionally equivalent. The only thing that is different is that I convert total_seconds to minutes then compare to the delay minutes and you compare total seconds to the delay minutes that you converted to seconds.

You just changed the order of the math.

I did not understand the concept of intermediary binary_sensor so I can not try that… Can you explain it a bit more, like where to but the code, in configuration.yaml?

I want the light to stay on longer because the motion sensor can’t see to the shower cabinet when there is humidity so when you take longer shower the light turns off and you have to come out of the shower to get the light turn on again. There is no ventilation to be turned on, in my country every bathroom is ventilated and they do not have any external ventilations like in USA for example.

So in short: Light stays on longer when in shower and after and before shower it should stay on shorter time.

in dev-template the code gave me resulrt “15” when humidity was over 50. And 15 was in quotes.

I have Hue motion sensor and it is not supported by HA by default so I have installed via custom_component if that has any meaning in this. TBH I have not updated that in 1.5 years…

BTW, in my Hue app there is two motion sensors and I only own one. Before anymore changes to HA, i will delete the motion sensors from HUE app and pair it again :smiley: After that I will update the hue.py to version 0.8 and then will create the automation using the intermediary sensor IF I get more information on how to do that.

sure.
this is a template binary_sensor, which you declare with the other binary_sensors, depending on your configuration. As you can see in post 5, it is based on a regular motion_sensor (I also use Hue sensors), and turns off 1 minute after the base sensor turns off, and stayed off. If motion again, the base sensor will turn on again, and the timed sensor will start over.

Now this timed sensor can be used as state in triggers (of course also in conditions and templates anywhere) main thing was to use it in a trigger, since triggers aren’t easily templatable. As you have already discovered :wink:

that all being said, I think the automation I posted in post 46 is the closest to what you said you wanted to do in your opening post. It might not yet do as you expect it, but I fear your sensor.zha_01ddaf89_1_1029 is causing that. Or rephrase, the state of the sensorzha_01ddaf89_1_1029 might be different from how we are trying to use it.

That’s why I asked you to post the sensor with a screenshot in the dev-template page, and also the full template.

If you do that, we can see what it returns and if some adjustments have to be made.

also:

this is correct isn’t it? the template is:

{{ '15' if states('sensor.zha_01ddaf89_1_1029' ) | int >= 50 else '5' }}

that is the almost direct copy of your opening post…?

I will post the screenshots when at home. The zha sensor is using zha module of HA to be detected. It is Xiaomi temperature sensor. In attributes it says “Unit of measurement: %” and in states it shows just the value of humidity. But I will post screenshot of that too.

And the result of the code you quoted is correct, but still the light was not 15minutes on.

a I see. But then we are getting somewhere:
the automation is triggered correctly , the condition time is working as expected, and the template seems to be alright. Though I am not sure of the need for the quotes to not. That would be worth a try again, now we are sure the logic is what you want it to be.

Is the light turned off at all? Or doesn’t the automation go past the delay.

It turned off but way sooner than it should have. Was not able to see if it was random times or some fixed time. It did feel like more random short time.

I did try without quotes but can try again in the evening.

But I do need the timer to reset so should I try to get this version of automation working or just start with the intermediary sensor?

if that’s the case, the delay wasn’t working correctly. It should. So we need to fix that.

that’s up to you. If you fix this, it does exactly what you desired in the opening post. Switch of a light after a certain time, depending in the humidity.

It’s all a matter of triggers and conditions. you could add a wait_template, as was suggested earlier Script Syntax - Home Assistant. This would ensure your action waits until the sensor is off (in the event motion would be detected again).

But at some time the automation fires, and reentering the bathroom could be too late… You should think about the most likely scenarios and try to catch these.

Fear to catch all you might have to complicate it a bit, what about this:

motion detection False: trigger and run turnoff light script
motion detection True: trigger and stop turnoff light script

in turn off light script , the condition is checked if hum is over 50 and set delay accordingly.

above would ensure that even if the delay timer is already started, new motion would restart the sequence, by stopping the script (and prevent it from turning off the light) and start all over.

Before the light were commanded by HUE app and if was so that the timer starts when there is no motion but if there is motion again it will restart the timer. So this made it work pretty good as I wanted. But the problem is that the sensor does not see inside the shower cabinet so when you are in the shower the light will turn off too soon.

That is why I want to move the functionality to HA so I can add the humidity.

So like you said, when the motion sensor state changes to “off”, it should start the lights off script and choose the minutes based on humidity. But when the state changes to “on” it should restart the lights off script in all cases regardless of humidity or if the light is on or off.

I’m starting to be really confused.