I’m trying to use my Hue motion sensor and Hue light in bathroom to change how long the light is on after there is no motion. The normal time would be 5 minutes when all is normal. The second situiation is when the humidity is over 50. Humidity is measured by different sensor. This is the code I have tried:
- alias: "Bathroom lights off when no motion"
trigger:
platform: state
entity_id: sensor.bathroom_motion_sensor
to: 'off'
for:
minutes: {{ 15 if sensor.zha_01ddaf89_1_1029.state >= 50 else 5 }}
condition:
condition: time
after: '07:00'
before: '00:00'
action:
service: homeassistant.turn_off
entity_id: light.bathroom
The humidity sensors state is the humidity in int. The code works if I just put in minutes the value 5.
You can’t use a template in the trigger as you are. It’s going to be complicated to get it just right but try the following in your trigger section:
trigger:
- platform: template
value_template: "{{ (states.sensor.bathroom_motion_sensor.state == 'off' and states.sensor.zha_01ddaf89_1_1029.state < 50 and ((as_timestamp(now()) - as_timestamp(states.sensor.bathroom_motion_sensor.last_changed)) / 60 )| int > 5) or (states.sensor.bathroom_motion_sensor.state == 'off' and states.sensor.zha_01ddaf89_1_1029.state >= 50 and ((as_timestamp(now()) - as_timestamp(states.sensor.bathroom_motion_sensor.last_changed)) / 60 )| int > 15) }}
So basically the template above says “if the humidity is below 50% and the motion sensor is off for longer than 5 minutes” then trigger. OR “if the humidity is greater than 50% and the motion sensor is off for longer than 15 minutes” then trigger.
I think. I don’t have you exact entities so I can’t test the substitutions I made to the template but I’m pretty sure I got it right.
The code does not give any errors but also does not trigger the lights off. So I have Hue motion sensor that is turning the lights on but nothing happens when the off automation should trigger.
The value that Hue motion sensor is for the last movement is in format:
“last_updated: 2019-01-06T09:23:55”
If you go to the dev tools state page and click the box to the left of the automation, in the box that pops up click on the “trigger” and see if the lights turn off.
Also it might help to post your entire off automation code again as you have it in your config right now just to make sure everything still looks ok there.
OK the next thing to do is copy only the actual template portion from the value_template and paste it into the dev-tool template editor and see what the result is on the right side. It should return either “True” or “False”.
And just as a dummy check it is between 0700 & 0000 where you are located correct?
I don’t have any of the entities that you have and when I copy your template and paste it into my template editor with no changes at all I still get “False” returned.
is returning nothing but I change “>” to “==” it returns false.
Could the reason be on that? That sensor is Xiaomi temperature sensor that is connected with ZHA module and hardware is Elelabs zigbee raspberry shield.
try wrapping the 50 with single quotation marks ( ‘50’ ) and leave the “>” in place.
EDIT: nevermind that. I figured out the issue. I hope…
I forgot that the state of the humidity detector is a string so you will need to convert it to an int to make it work.
try this:
{{ (states.sensor.bathroom_motion_sensor.state == 'off' and states.sensor.zha_01ddaf89_1_1029.state | int < 50 and ((as_timestamp(now()) - as_timestamp(states.sensor.bathroom_motion_sensor.last_updated)) / 60 )| int > 1) or (states.sensor.bathroom_motion_sensor.state == 'off' and states.sensor.zha_01ddaf89_1_1029.state | int >= 50 and ((as_timestamp(now()) - as_timestamp(states.sensor.bathroom_motion_sensor.last_updated)) / 60 )| int > 2) }}