Detect open toilet seat automation - need help please

Hi there

I have two cats at home and try to build an automation using an Aqara vibration sensor integrated via Deconz-addon. It should detect if the toiletseat is open but nobody is in the bathroom anymore. the presence is being detected with a motion sensor.

What I have done so far

  • integrated the sensor via Deconz
  • created a template sensor to get the orientation
  • created a template sensor to get the tiltangle

templates look as following

  - platform: template
    sensors:
      toiletseat_angle:
        friendly_name: Toiletseat angle
        value_template: "{{ state_attr('binary_sensor.vibration_8', 'orientation')}}"
        unit_of_measurement: '°'
- platform: template
    sensors:
       toiletseat:
        friendly_name: Toilet seat
        value_template: >-
         {% set doordir = states.binary_sensor.vibration_8.attributes.tiltangle %}
         {% if doordir | float <= 75 %}Closed
         {% elif doordir | float > 75 %}Open
         {%- endif %}
        entity_id: binary_sensor.vibration_8

I get the following data from a debug-node:

question
How can I get the data from msg.data.state in order to build an automation based only on the second value? -72 means open , 0 means closed. I tried a switch-node which should read the values but havent had any luck. the tiltangle doesnt work reliable, otherwise I would have used it.

maybe I can replace the tiltangle with the orienation in the template (toiletseat) and adjust the values (open, closed)?

any help would be appreciated to make the world for cats a bit safer :wink:

all the best

Andrew

Mmmh, let me see if I understand the situation.
You want an alert if the bathroom is empty of people and the toilet seat is up.
You have a sensor that tells you if the bathroom is empty of people and another one telling you the state of the toilet seat.
So far you seem to be spot on with the purpose of your automation.
What is not working exactly?

PS: your world is a better place already if you can count on family members to shut the damn toilet seat :smiley:

Ok wait, I get it - your sensor returns the whole triplet of data instead of just -72.
That is interesting, is it json formatted maybe? What do the other numbers represent? Did you try to play with the developer’s tool model page to see if you can regex it out of the sensor?

hi there

first of all thanks for your quick reply, you fully understand the situation :wink: yeah, the problem is that the orientation is being reported as a triplet. here an example from the dev-tools. the values represent the axis x,y,z.

'on': true
temperature: 30
orientation:
  - 6
  - -72
  - 17
tiltangle: 106
vibrationstrength: 6
friendly_name: Vibrationssensor
device_class: vibration

unfortunately I am not really familiar with regex :confused:

Ok - the best way to start would be to see how HA sees the sensor. In your post above I see a mix of sensors and binary_sensor plus some debug that doesn’t look HA (it’s Node-Red?).
What do you have in HA for that vibration_8 device?
You could search it in the Developers tools → States page. If it’s a sensor with attributes then we can try to pull the info out of it, but we need to see the exact output.

Try with this:

{{ (state_attr('binary_sensor.vibration_8', 'tiltangle').split(",")[1] )}}

This should split the string by the comma, and return the second (0 is first) item.

I just verified it 3 times and I only get the below sensors. Here is how HA sees the sensor via States-page. yeah debug is from Node-Red.

here. the three entities


where do I enter that exactly, in my template sensor?

 - platform: template
    sensors:
      toiletseat_angle:
        friendly_name: Toiletseat angle
        value_template: **{{ (state_attr('binary_sensor.vibration_8', 'tiltangle').split(",")[1] )}}**
        unit_of_measurement: '°'

Then try with this:

{{ (state_attr('binary_sensor.vibration_8', 'orientation').split(",")[1] )}}

First try to see if it works in the Developer’s tool model page

I guess you mean Developer Tools → Template? If so I tried it with the following output:

Then it’s already a list

{{ (state_attr('binary_sensor.vibration_8', 'orientation')[1] )}}

Should do.

1 Like

oh boy, works like a charm! many thanks, made my day :wink:

1 Like

Hi. I’m doing very much the same thing, and can’t seem to quite get it; do you mind sharing your code for the working templates?
Mine is for a humble garage door though, haha!
Much appreciated!

hi there

sure, here is my code in config.yaml

  - platform: template
    sensors:
      toiletseat_angle:
        friendly_name: Toiletseat angle
        value_template: "{{ (state_attr('binary_sensor.vibration_6', 'orientation')[1] )}}"
        unit_of_measurement: '°'

Thanks!
With the help of this and other threads I’ve got it close to working.

This is what I have so far:

    door_angle:
        friendly_name: Garage Door angle
        value_template: >-
          {% if (state_attr('binary_sensor.vibration_2', 'orientation')[1] ) | int > -60 %}
            closed
          {% else %}
            open
          {% endif %}

Except my open/close is not as expected.

I get values from the sensor of around 2 for open, and -71 for closed on the second orientation figure; my statement returns this as closed though, when it should read open.

E.g. this is the garage door when open:

current

What am I missing? Surely 2 is greater than -60?

Thanks!
Paul

got ya. actually I have around the same values. In my case I only wanted to get the data from the orientation and then I use it in Node-RED to either send a message when the value is -5 or -15. at -5 its “closed” at -15 its “open”. in the event its open and the light in the bathroom is off, it will send a message via Pushover to my phone.

if you use Node-RED you could set up a flow with an event state-node and a switch.


UPDATE:
I got it working with this template:

  - platform: template
    sensors:
       toiletseat:
        friendly_name: Toilet seat
        value_template: >-
         {% if (state_attr('binary_sensor.vibration_8', 'orientation')[1] ) | int > -60 %}
         closed
         {% else %}
         open
         {% endif %}
        entity_id: binary_sensor.vibration_8

Cool! Yeah me too now - somewhere along the line I got confused by the dashes and put them into my spreadsheet of figures as negative when I was orignally assesing the sonsor.

BTW - not sure if this is relevant as I’m a total noob, but:

entity_id in templates??

Appreciate your help!

1 Like