Help with Binary sensor Xiaomi

Hello, i want to use a binary sensor for alarm system.
i have vibration sensor and i’m trying to set up a binary_sensor in this way:

binary_sensor:
- platform: template
  sensors:
    test_finestra_studio:
      friendly_name: 'test finestra studio'
      value_template: >-
          {% if states.sensor["0x00158d0002b1136c_action"] == 'vibration' %}
            on
          {% elif states.sensor["0x00158d0002b1136c_action"] == 'tilt' %}
            on
          {% elif states.sensor["0x00158d0002b1136c_action"] == 'drop' %}
            on
          {% elif states.sensor["0x00158d0002b1136c_action"] == '' %}
            off
          {% endif %}

But not work…
the sensor is this:

sensor.0x00158d0002b1136c_action

The value in the State Attributes is this:

{
“linkquality”: 131,
“battery”: 100,
“voltage”: 3015,
“angle”: 5,
“angle_x”: -2,
“angle_y”: 75,
“angle_z”: 14,
“angle_x_absolute”: 92,
“angle_y_absolute”: 15,
“action”: “”,
“friendly_name”: “Finestra Studio”,
“icon”: “mdi:gesture-double-tap”
}

What i need is the action value that can change to tilt, drop or vibrate

If I understand you correctly, you want the binary sensor to be on if the action attribute of sensor.0x00158d0002b1136c_action is vibration, tilt or drop. If so, then:

binary_sensor:
- platform: template
  sensors:
    test_finestra_studio:
      friendly_name: 'test finestra studio'
      value_template: >
        {{ state_attr('sensor.0x00158d0002b1136c_action', 'action') in
           ('vibration', 'tilt', 'drop') }}
2 Likes

Yeeeeesssss!!! It works!!!
Thanks a lot!!
I have another question, there is a way to lock in On for 10 seconds?
Because when the sensor is in vibration or other states after 1 second will turn to “” and i can’t see which sensor has triggered my alarm.
I use this in my notification:

{% for state in states.binary_sensor -%}\n  {% if state.state == 'on' %}\n    {{ state.name }}\n  {%- endif -%}\n{%- endfor -%}\n"

Really thanks a lot i spent many days to make this working

1 Like

See: Template - Home Assistant

Really thanks a lot!!! :smiley:

1 Like

I have a problem now… after the reboot of the server not work anymore and in the log i have this:

Error during template condition: UndefinedError: ‘None’ has no attribute ‘attributes’

There’s nothing in my suggested solution that would cause that error.

EDIT: It says “template condition”. This sounds like a template condition somewhere in one of your automations.

Thanks a lot, but i don’t know why but won’t work anymore…
It seems that sometimes work just the first time i move the sensor but won’t turn on again if i move again the sensor and after reboot for just 1 time work but not every time…
Do you have some Idea?

What does “work” mean? What is your current implementation of the binary template sensor?

My guess is one of the possible values reported by the device may be no value at all (empty string). While helping someone else with the same device, I learned it’s possible for it to return an action attribute with no value.

That may be. But that still should not cause an error. The state_attr() function specifically deals with those issues and will return None if the entity doesn’t exist, or the attribute doesn’t exist. Or even it they do both exist, and the value of the action attribute is an empty string, the template will evaluate to False, and the template binary sensor’s state will be off.

Take 2: If sensor.0x00158d0002b1136c_action doesn’t exist, or it doesn’t have an action attribute, or if the attribute’s value is an empty string, the template will still work. In any of these cases it will evaluate to False and the template binary sensor’s state will be set to off.

Which begs the question, What is this sensor: sensor.0x00158d0002b1136c_action'? If there’s the possibility it may be undefined at startup is it because it is slow to being automatically discovered or is it yet another Template Sensor?

Not sure that matters. Still not clear what “doesn’t work” means. Also the error mentioned seems to have to do with an automation, not this template binary sensory. Too many details missing at this point.

Thanks a lot!! Sorry for my late reply but I’m driving…
I’m not sure that the error of the template is related to what we have do…
So how can I search for more details?
Yes the sensor still in “” value and change for only 1 second to vibration or tilt ecc… and after that return to “”
This sensor use Zigbee2Mqtt to send the state information and after the system will boot it will take around 5 minutes to began ready to use

Maybe we should stick to the original intent of this topic, and that is the creation of the template binary sensor. When you are able, can you post your existing YAML code for it? Also, does it behave the way you want (which I believe is, when sensor.0x00158d0002b1136c_action's action attribute changes to one of the specified values, the binary sensor changes to on, and stays that way for at least 10 seconds)?

If the template binary sensor is behaving the way you wanted, then I think this topic is complete. If you’re having another issue, then you should start a new topic. If you do it would probably be good to provide a link back to this topic for reference.

I will post the code when I’m at home but I have added the same string that you have share to me…
The problem is that won’t work anymore…
If the sensor.0x00158d0002b1136c_action 's action is vibration or other the binary sensor still off… so won’t change…
I have also found this in the log

 
2019-10-01 19:23:55 WARNING (MainThread) [homeassistant.components.binary_sensor] Setup of platform template is taking over 10 seconds.

I don’t know how but can we use the entity_id to force the binary sensor to update?

This message:

Setup of platform template is taking over 10 seconds.

means there’s a more fundamental problem than updating the entity.

1 Like

Please post the code. It should update itself as necessary if entered correctly.

This is what i have in Configuration.yaml

#Sensori Vibrazione a Binari
binary_sensor:
- platform: template
  sensors:
    finestra_studio:
      friendly_name: 'Finestra Studio'
      device_class: window
      delay_off:
        minutes: 1
      value_template: >
        {{ state_attr('sensor.0x00158d0002b1136c_action', 'action') in
           ('vibration', 'tilt', 'drop') }}
    porta_blindata:
      friendly_name: 'Porta Blindata'
      device_class: door
      delay_off:
        minutes: 1
      value_template: >
        {{ state_attr('sensor.0x00158d0002af6ac2_action', 'action') in
           ('vibration', 'tilt', 'drop') }}