Help needed on template formatting

hi,
i’m trying to setup a binary sensor based on multiple motion sensors and room locations of current home devices to identify is anyone is present in room e.g. anyone in livinigroom or bedroom.

below code is not working when template value is defined in multiple lines…

      no_one_in_livingroom:
        friendly_name: No one in Livingroom
        value_template: >- 
                        '{{ (states.sensor.lalit_room_location.state != "livingroom" and
                             states.sensor.jyoti_room_location.state != "livingroom")
                          and ( 
                                ( (now()-states.binary_sensor.livingroom_motion.last_changed).total_seconds() > 1*60) and 
                                ( (now()-states.binary_sensor.gallery_motion.last_changed).total_seconds() > 1*60) and 
                                ( (now()-states.binary_sensor.kitchen_motion.last_changed).total_seconds() > 1*60)
                              )
                          
                         }}'
        entity_id: sensor.time

while below code is working fine if i put everything into 1 line.

      no_one_in_livingroom:
        friendly_name: No one in Livingroom
        value_template: '{{ (states.sensor.lalit_room_location.state != "livingroom" and states.sensor.jyoti_room_location.state != "livingroom") and ( ( (now()-states.binary_sensor.livingroom_motion.last_changed).total_seconds() > 1*60) and ( (now()-states.binary_sensor.gallery_motion.last_changed).total_seconds() > 1*60) and ( (now()-states.binary_sensor.kitchen_motion.last_changed).total_seconds() > 1*60) ) }}'
                        # '{{ (states.sensor.lalit_room_location.state != "livingroom" and
                        #      states.sensor.jyoti_room_location.state != "livingroom")
                        #   and ( 
                        #         ( (now()-states.binary_sensor.livingroom_motion.last_changed).total_seconds() > 1*60) and 
                        #         ( (now()-states.binary_sensor.gallery_motion.last_changed).total_seconds() > 1*60) and 
                        #         ( (now()-states.binary_sensor.kitchen_motion.last_changed).total_seconds() > 1*60)
                        #       )
                          
                        #  }}'
        entity_id: sensor.time

any thoughts why it’s not working in multi-line formatting?

Did you try removing the ’ from the multi-line? Believe you only need surrounding quotes on single line. Just a guess.

Also, fwiw, I get my time (in microseconds [seconds = microseconds % 60]) since last changed like this:

{{ (as_timestamp(states('sensor.date_time').replace(',','')) - as_timestamp(states.sensor['6900_kitchen_hvac_action'].last_changed| default(0)) | int ) }}

And my states like this:
is_state('device_tracker.volvo', 'home')

Yep, that would fix it. Also you’ve got more parentheses than required.

      no_one_in_livingroom:
        friendly_name: No one in Livingroom
        value_template: >
          {{ states.sensor.lalit_room_location.state != "livingroom" and
             states.sensor.jyoti_room_location.state != "livingroom" and
             (now()-states.binary_sensor.livingroom_motion.last_changed).total_seconds() > 1*60 and 
             (now()-states.binary_sensor.gallery_motion.last_changed).total_seconds() > 1*60 and 
             (now()-states.binary_sensor.kitchen_motion.last_changed).total_seconds() > 1*60
          }}
        entity_id: sensor.time

The result, after stripping leading and trailing whitespace, and converting to all lower case, must be exactly true, whereas in your attempt it would have been 'true' (with extra quotes as part of the string), which does not match true.

yes, it worked removing the quotes.

thanks both

1 Like