Strike my previous post. I have solved it. I am now actually capturing the state of the attached sensor to my unit. The work done in this thread helped allot. Especially @anon63427907 posts which were the basis of me solving this.
I had to use Developer Tools to identify which attribute which carried the value which indicated whether the door was open or closed was mapped to.
current: false
current_consumption: 0
voltage: 0
friendly_name: Garage Control
icon: hass:garage
It was obvious from his simple algorithm in his previous post that he was testing the value of “current_consumption”. For the life of me I couldn’t figure out why my value wasn’t changing when the door opened or closed. That was because my device was slightly different.
So I added the device again and stepped through pragmatically. When adding the device it was clear I had 5 attributes:
- Value (False)
- Value (0)
- Value (False)
- Value (20)
- Value (0)
It took me a while to figure out that what you are doing when setting up the integration is mapping these device values to the static fields:
Current
Current Consumption
Voltage
It doesn’t mean that the values contain those things, you are just mapping the device variables to them so you can see (and subsequently test) the value.
Through the developer tools I established that variable 3. changed to ‘0.1’ when the reed switch was disconnected and change to ‘0’ when it was connected. Excellent. That made the code in the above post make more sense. When that variable is 0 door is closed. When that variable is 0.1 the door is open. I tested this by controlling the door using non HA means (IR remote and Tuya App) and the variables changed along with the state of the reed switch.
I ended up mapping my device like this:
Current → 1. Value (False)
Current Consumption → 3. Value (False)
Voltage 1. Value (False)
So for my device 1. is the switch and 2. is the reed switch value indicating open and closed.
So I used the code in the above post as a basis to create a sensor attached to that 2. variable and test as it changed from 0.1 to 0 to indicate open and closed state. I didn’t like the unfriendly value that the code above spat out so I modified it to give me a friendlier value:
sensor:
- platform: template
sensors:
garage_door_is_open:
unique_id: garage_door_is_open
friendly_name: Garage Door Is Open
value_template: "{% if state_attr('switch.garage_door','current_consumption') | float * 10 > 0 %}Open{% elif state_attr('switch.garage_door','current_consumption') == 0 %}Closed{% endif %}"
All is now well. It doesn’t matter whether the door is opened or controlled manually or via HA I know have a live (offline as its via Local Tuya) state of the door and a switch that changes along with manual control as well.
Awesome!
EDIT: as I test more, it appears that there is a combination of values that will allow me to determine states “Opening” and “Closing” too along with just “Open” and “Closed”. Will post once I have tested thoroughly.