Xiaomi Gateway Integration

Can’t increment +1 inside the scope of the for loop, num will never be accessible in the loop.last condition. jinja2 sucks… any solution?

Have you tried it? It works fine here.

Yes, it’s always false because “num” is not accessible inside the loop. I’ve 1 device with 40 battery so I changed the < 10 to < 41 and it’s always false.

Have you tested the code at you_ip:8123/dev template ?
It works fine for me

Yes, I always test there before using it.
You can change to:

{% set num = 0 %}
{% for state in states.binary_sensor %}
    {% if state.attributes.battery_level %}
        {% if state.attributes.battery_level | float < 45 %} 
{{num}}
                {% set num = num|float + 1 %}
{{num}}
        {% endif %}
   {% endif %}
        {%- if loop.last%}
           {%- if num > 0 %}
                  True
            {%- else -%}
                   False
            {%- endif %}
       {%- endif %}
{% endfor %}

The first {{num}} print is always 0.

http://jinja.pocoo.org/docs/2.9/templates/#id12 Scoping Behavior

Hey,
has something changed with the motion event recently? I am using the fork from @Danielhiversen . Motionevents are firing not as frequently as they fired a few days ago. I don`t know if this was occurring when i updated the code but could be.
Does anyone have any ideas on this?

Thanks :slight_smile:

in your switch component the description says binary_sensor

Same.
Did you manage? Maybe a converter?

Gateway just reports value from 300 to 1300. Available docs do not say nothing about unit of measurement. My guess was lum. If you think that lx is right than I can easily fix it.

mine now is 307, and is almost dark, I would say is 5 lux

I’m thinking of putting both sensors next to each other and trying to see correlation between the units and try to figure it out.

For some reason the ghost problem persists even with the option check_ghosts: true.
To countercheck the behavior, I made a small Node RED flow that observes the report messages and there are no ghost reports so far. I wonder where the ghost states came from in first place.

can you explain me what a “ghost problem” is, and if the option solved partly (or nothing at all).

Thanks

good idea

thats’ mine (lower level is 300).

Now I wouls say is 100 lux (750lm), and is 9am gray sky

The “ghost problem” is, like already discussed in this thread, when montion or door sensor states are turned to “on”, but nobody passed the motion sensor resp. opened a window/door.
The legend says ghosts turn them on for their entertainment.

The option “check_ghosts” didn’t change a thing as far as I can tell.

I have this problem more on the window/door sensor rather then the PIR sensor. But is pretty annoying since it kills some automations

Did you find out the source of the problem? (I don’t believe in ghosts :smiley:)

It looks like a bug of the xiaomi gateway if polling is used (cp. https://github.com/louisZL/lumi-gateway-local-api/issues/11). I’ve created a wireshark dump of the network traffic which shows the wrong values in the response of the gateway unfortunately. :frowning:

-> { "cmd":"read","sid":"158d000119fcc2"}
<- {"cmd":"read_ack","model":"magnet","sid":"158d000119fcc2","short_id":44778,"data":"{\"voltage\":3025,\"status\":\"open\"}"}

-> { "cmd":"read","sid":"158d00015870d5"}
<- {"cmd":"read_ack","model":"magnet","sid":"158d00015870d5","short_id":44432,"data":"{\"voltage\":3015,\"status\":\"open\"}"}

In reality the status is “close(d)”.

Me neither :wink:
The solution I came up with is to listen on UDP 224.0.0.50:9868(? Not sure with the port right now) and parse only the cmd:report messages with NodeRED. The collected values are sent via mqtt to mqtt (binary) sensors.
I can provide an example flow, if someone’s interested.

Yes, please