Sensor template to only update value spesific values

Hi

My goal with this post is to have a template sensor show me the name of the last person who unlocked the door. This works partial for me today by using this code:

  • platform: template
    sensors:
    door_opened_by:
    value_template: “{{ state_attr(‘lock.door’, ‘changed_by’) }}”

The issue I have with this is when the door is locked with a button or opened from the inside the value of “door_opened_by” is “none”.

I want the sensor to only show the last true value of the attribute Changed_by.

Does anyone have suggestion on how to do this? If there is an alternative way of doing this, that would be interesseting as well.

you mean something like this:

value_template: >
  {% if (states('lock.door','open') and
         state_attr('lock.door',changed_by') != 'none' %}
   Door opened by:  {{ state_attr('lock.door', 'changed_by') }}
  {% else %} Door opened from inside or button
  {% endif% }

note that this template doesn’t yet take into account the closed state… and maybe other states the door could have. That might need extra templating.

Thanks for your quick reply.

The first part looks good.

Se second part (else) would need something to keep previous/current value, and the only legal values fom me would be my name, wifes name, and the name of my kids.

For example:
If I come home and unlock the door, the value would be my name. If a go out and lock the door, i want the value to be my name untill some else unlocks the door.

I am sorry if I am not able to explain this is a good way.

ok, that would have to be ‘remembered’ or stored in a variable (a custom component) or an input_boolean/select maybe, which would be set by an automation, which is what your are describing…

But first you’d need to decide what would happen if someone else would open the door?

about the ‘legal’ names. You could easily have a condition like:

condition: template
value_template: >
  {{state_attr('lock.door',changed_by') in ['you', 'your_wife','kid1','kid2','kid3','etc']))

but you’d have to decide what would happen if this condition isn’t met. No action at all, or a warning maybe? Why else bother who opened/closed the door?

but for my main idea have a look at this:

  - alias: 'Notify outside daylight'
    id: 'Notify outside daylight'
#    initial_state: on
    trigger:
      platform: state
      entity_id: binary_sensor.outside_daylight_sensor
#      to: 'off'
    condition: []
    action:
      - service: variable.set_variable
        data:
          variable: virtual_light_daylight
          attributes_template: >
            {
                  "history_1": "{{ variable.state }}",
                  "history_2": "{{ variable.attributes.history_1 }}",
                  "history_3": "{{ variable.attributes.history_2 }}",
                  "history_4": "{{ variable.attributes.history_3 }}",
                  "history_5": "{{ variable.attributes.history_4 }}"
            }
        data_template:
          value: >
            {{trigger.to_state.state}} : {{states('sensor.virtual_light')}}
      - condition: template
        value_template: >
          {{is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: 
            Daylight {{states('binary_sensor.outside_daylight_sensor') }}: Outside lights are powered 
            {{'on ' if is_state('binary_sensor.outside_daylight_sensor', 'off') else 'off, '-}}
            at Solar angle: {{states('sensor.solar_angle')}}.

and think of the binary_sensor.outside_daylight_sensor s your lock.door.

when it goes on/open, it stores that value with the attributes value mentioned, in my setting a lx sensor. Would be the equivalent of the door lock and name of the opener in your setting. You’d need the Custom component for ‘saving’ these values, as that is what you said you wanted to do in your 2nd post.

If you don’t want the opener to be saved, you can simply skip that part and use the sensor from before, and maybe a message the door is unlocked/locked.

So, options galore :wink:

You might also want to consider if you wouldn’t simply want to trigger in state change, so either open or close, and have an automation respond to that. As you can see I have the to: off in my automation commented out, to do exactly that. I think that is what you would want to?

Using an automation to store it in an input_booleam/select sound like a good plan. I did not think of that. I will read up on the different elements here, try it and update this post with the result.

Thanks for your help for now!

Your tip with custom component variable and automation + your code worked out great for me.

This this is my final code.

- alias: 'door opened by'
  id: 'Notify last person to unlock door'
  trigger:
    platform: state
    entity_id: lock.door
    to: 'unlocked'
  condition:
    condition: template
    value_template: "{{ state_attr('lock.door', 'changed_by') in ['me','wife','kid1','kid2'] }}"
  action:
    - service: variable.set_variable
      data:
        variable: door_opened_by_var
        attributes_template: >
          {
                "history_1": "{{ variable.state }}",
                "history_2": "{{ variable.attributes.history_1 }}",
                "history_3": "{{ variable.attributes.history_2 }}",
                "history_4": "{{ variable.attributes.history_3 }}",
                "history_5": "{{ variable.attributes.history_4 }}"
          }
      data_template:
        value: >
          {{now().hour}}:{{now().minute}} - {{states('sensor.door_open_by')}}

Thanks for all the help!

2 Likes