Hi,
I have two motion sensors (lets say M1, M2) and each of them controls its own light (say L1, L2) but only one motion sensor (M1) has an illuminance sensor controlling light L1. However, I also want to turn on light L2 based on the illuminance value of M1.
I want to create a template sensor with the following considerations:
- If L1 is on, the illuminance value would be high too and L2 wouldn’t be turned on, so the new template sensor should return 0 in that case (because we assume that the reason why L1 was turned on is, because it was dark enough)
- If L1 is off, we just return the illuminance value of M1
- If L1 turns off, the illuminance value of M1 is not updated immediately. So it should still return 0 until the illuminance value is updated AFTER the light L1 turned off.
This is my current template:
- platform: template
sensors:
hallway_illuminance_wrapper:
friendly_name: "Intelligent hallway illuminance"
value_template: >
{% if is_state('light.light1, 'on') %}
0
{% else %}
{{ states('sensor.motion1_illuminance') }}
{% endif %}
device_class: "illuminance"
unit_of_measurement: "lx"
This tempalte sensor shall be then used to decide for M2 if L2 should be turned on or not.
But how can I achieve the third condition? Can I somehow compare the age of the latest state of L1 and the illuminance value and if the latter one is newer, then return it, otherwise return 0?
Btw. I saw that there is this statistics platform and it provides a “datetime_newest”, but can I also use this within a template? Or does anybody have a better idea how can I achieve the third condition?