Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement animals as switch instead of sensor #9

Open
jacotec opened this issue Aug 19, 2022 · 5 comments
Open

Implement animals as switch instead of sensor #9

jacotec opened this issue Aug 19, 2022 · 5 comments

Comments

@jacotec
Copy link
Contributor

jacotec commented Aug 19, 2022

Just to track this here in the ToDo list as you've already confirmed this in the HomeAssistant forums as a "next improvement" in PLH:

Animals should be changed to be a switch instead of a sensor, so you can manually correct the pet location (i.e. in case it was let in by a human via front door).

@jacotec
Copy link
Contributor Author

jacotec commented Sep 1, 2022

Workaround for this:

With #12 you can do the tracking fully in HA. Use automations triggered by a change of the "Time" attribute. As an additional condition use the state (I'm using a template here to just get "Inside" and "Outside" out of "Inside/Lookedout" and "Outside/Lookedin", combining this into "sensor.catname.where").

In a multi-flap setup you can also use the MAC address of the device which gave the last status update to get finer granular positions.

Every information (Inside / Outside) from every flap triggers an own automation which sets the option of an input_select helper, which in my case can be "Home", "Screened Area", "Outside" and "Unknown". My custom-button with the cat icon and name is green (Home), orange (Screened area), red (Outside) or gray (Unknown). Tapping the button switches through the statuses for manual correction.

The "Unknown" status can be activated manually in case we don't know where the cat is (i.e. open terrace door in summer). The next contact with any flap (even a "Lookedin" or "Lookedout") updates the status as HA now knows exactly where she/he is.

@cacula
Copy link

cacula commented Feb 1, 2023

@jacotec could you be so kind to share the code of your implementation in HA? I would like to do it like you suggested but I'm lacking the experience in HA to write the code myself :( thanks in advance :)

@jacotec
Copy link
Contributor Author

jacotec commented Feb 2, 2023

@cacula These are my templates in HA. I've included a sample for my "Testcat" (a chip I use for testing), you need to implement these templates for any of your "real" pets:

  - platform: template
    sensors:
      catflap_battery_percent:
        friendly_name: CatFlap WiGa Battery
        value_template: '{{ min((((states("sensor.wintergarten_battery") |float - 4.8) *100)/1.5) |int,100) }}'
      catflap_battery_status:
        friendly_name: CatFlap WiGa Battery Status
        value_template: '{% if (states("sensor.catflap_battery_percent")|int >= 20 and states("sensor.catflap_battery_percent")|int <= 100) %}green{% elif  (states("sensor.catflap_battery_percent")|int >= 10 and states("sensor.catflap_battery_percent")|int < 20) %}yellow{% elif  (states("sensor.catflap_battery_percent")|int < 10) %}red{% endif %}'
      catflap_gehege_battery_percent:
        friendly_name: CatFlap Gehege Battery
        value_template: '{{ min((((states("sensor.gehege_battery") |float - 4.8) *100)/1.5) |int,100) }}'
      catflap_gehege_battery_status:
        friendly_name: CatFlap Gehege Battery Status
        value_template: '{% if (states("sensor.catflap_gehege_battery_percent")|int >= 20 and states("sensor.catflap_gehege_battery_percent")|int <= 100) %}green{% elif  (states("sensor.catflap_gehege_battery_percent")|int >= 10 and states("sensor.catflap_gehege_battery_percent")|int < 20) %}yellow{% elif  (states("sensor.catflap_gehege_battery_percent")|int < 10) %}red{% endif %}'
      cat_testcat_laststate:
        friendly_name: Testcat State
        value_template: "{{ state_attr('sensor.testkatze','State') }}"
      cat_testcat_where:
        friendly_name: Testcat_where
        value_template: '{% if (states("sensor.cat_testcat_laststate") == "Inside" or states("sensor.cat_testcat_laststate") == "Lookedout") %}Inside{% elif  (states("sensor.cat_testcat_laststate") == "Outside" or states("sensor.cat_testcat_laststate") == "Lookedin") %}Outside{% endif %}'
      cat_testcat_updatetime:
        friendly_name: Testcat Update Time
        value_template: "{{ state_attr('sensor.testkatze','Time') }}"

These templates are calculating the battery status of my two cat flaps and for each cat they calculate:

  • catname_where: Reduces the last reported location to just "Inside" or "Outside", updated also by a "Lookedin" or "Lookedout" status update of a flap
  • catname_updatetime: Timestamp of the last event where this cat was "seen" by a flap
  • catname_laststate: Last "real" state from the flap

You need to track your cat's position via automations. This depends on your setup. For me I have a flap from the house (=Haus) into the screened outside area (=Gehege) and a second one from the screened area into the "big world" (Draußen).
So I have these possible cat location updates:

  • Cat_where is reporting "Inside" by the house flap: Cat is in the house
  • Cat_where is reporting "Outside" by the house flap: Cat is in the screened area
  • Cat_where is reporting "Inside" by the outer flap: Cat is in the screened area
  • Cat_where is reporting "Outside" by the outer flap: Cat is in the "big world"

The button in Lovelace is an Input.Select with the 4 possible states:

  • Haus (Inside)
  • Gehege (Screened area)
  • Draußen (Outside)
  • Unbekannt (Unknown)

The states do have different colors. The automation "Position Testcat ändern" is an action triggered by a tap on these buttons, so you can manually switch the cat states in HA (i.e. if you let your cat in through the terrace door). Or set your cat to "unknown" if you currently do not know where she is, the next contact with a flap will then update the state again.

You need to have these automations per cat!

(Flap MAC addresses anonymized)

- id: '1661975207770'
  alias: Position Testcat Gehege durch Gatter
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.testkatze
    attribute: Time
  condition:
  - condition: state
    entity_id: sensor.testkatze
    attribute: Update_Mac
    state: 1175xxxxxxxxxxxx
  - condition: state
    state: Inside
    entity_id: sensor.cat_testcat_where
  action:
  - service: input_select.select_option
    data:
      option: Gehege
    target:
      entity_id: input_select.testcat_location
  - service: notify.mobile_app_marcos_iphone_14
    data:
      message: Testcat ist ist Gehege gekommen
      title: TestWalk
  mode: single
- id: '1661975400689'
  alias: Position Testkatze Draußen
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.testkatze
    attribute: Time
  condition:
  - condition: state
    entity_id: sensor.testkatze
    attribute: Update_Mac
    state: 1175xxxxxxxxxxxx
  - condition: state
    state: Outside
    entity_id: sensor.cat_testcat_where
  action:
  - service: input_select.select_option
    data:
      option: Draußen
    target:
      entity_id: input_select.testcat_location
  - service: notify.mobile_app_marcos_iphone_14
    data:
      message: Testcat ist nach Draußen gegangen {{ states('sensor.cat_testcat_updatetime')
        }}
      title: TestWalk
  mode: single
- id: '1661975836661'
  alias: Position Testcat ändern
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_button.testcat_changeposition
    to: 'On'
  condition: []
  action:
  - service: input_select.select_next
    data: {}
    target:
      entity_id: input_select.testcat_location
  mode: single
- id: '1661977743513'
  alias: Position Testcat Gehege durch Haus
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.testkatze
    attribute: Time
  condition:
  - condition: state
    entity_id: sensor.testkatze
    attribute: Update_Mac
    state: 651Bxxxxxxxxxxxx
  - condition: state
    entity_id: sensor.cat_testcat_where
    state: Outside
  action:
  - service: input_select.select_option
    data:
      option: Gehege
    target:
      entity_id: input_select.testcat_location
  - service: notify.mobile_app_marcos_iphone_14
    data:
      message: Testcat ist ins Gehege gegangen
      title: TestWalk
  mode: single
- id: '1661977851245'
  alias: Position Testcat Haus
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.testkatze
    attribute: Time
  condition:
  - condition: state
    entity_id: sensor.testkatze
    attribute: Update_Mac
    state: 651Bxxxxxxxxxxxx
  - condition: state
    entity_id: sensor.cat_testcat_where
    state: Inside
  action:
  - service: input_select.select_option
    data:
      option: Haus
    target:
      entity_id: input_select.testcat_location
  - service: notify.mobile_app_marcos_iphone_14
    data:
      message: Testcat ist ins Haus gekommen
      title: TestWalk
  mode: single

I hope this gives you an idea ;-)

@clyso
Copy link

clyso commented Feb 3, 2023

@jacotec thanks for your detailed answer - very helpful & appreciated 👍🏽 - i will try and let you know

@cacula
Copy link

cacula commented Mar 5, 2023

@jacotec the answer from @clyso - that's actually my work account ;-)
I tried to implement this today. I have all in one file as a package. this is how it looks so far:

template:
  - sensor:
      - name: catflap_battery_percent
        state: '{{ min((((states("sensor.katzenkeller_battery") |float - 4.8) *100)/1.2) |int,100) }}'
      - name: catflap_battery_status
        state: '{% if (states("sensor.catflap_battery_percent")|int >= 20 and states("sensor.catflap_battery_percent")|int <= 100) %}green{% elif  (states("sensor.catflap_battery_percent")|int >= 10 and states("sensor.catflap_battery_percent")|int < 20) %}yellow{% elif  (states("sensor.catflap_battery_percent")|int < 10) %}red{% endif %}'
      - name: cat_cat1_laststate
        state: "{{ state_attr('sensor.cat1','State') }}"
      - name: cat_cat1_where
        state: '{% if (states("sensor.cat_cat1_laststate") == "Inside" or states("sensor.cat_cat1_laststate") == "Lookedout") %}Inside{% elif  (states("sensor.cat_cat1_laststate") == "Outside" or states("sensor.cat_cat1_laststate") == "Lookedin") %}Outside{% endif %}'
      - name: cat_cat1_updatetime
        state: "{{ state_attr('sensor.cat1','Time') }}"
      - name: cat_cat2_laststate
        state: "{{ state_attr('sensor.cat2','State') }}"
      - name: cat_cat2_where
        state: '{% if (states("sensor.cat_cat2_laststate") == "Inside" or states("sensor.cat_cat2_laststate") == "Lookedout") %}Inside{% elif  (states("sensor.cat_cat2_laststate") == "Outside" or states("sensor.cat_cat2_laststate") == "Lookedin") %}Outside{% endif %}'
      - name: cat_cat2_updatetime
        state: "{{ state_attr('sensor.cat2','Time') }}"
      - name: cat_cat3_laststate
        state: "{{ state_attr('sensor.cat3','State') }}"
      - name: cat_cat3_where
        state: '{% if (states("sensor.cat_cat3_laststate") == "Inside" or states("sensor.cat_cat3_laststate") == "Lookedout") %}Inside{% elif  (states("sensor.cat_cat3_laststate") == "Outside" or states("sensor.cat_cat3_laststate") == "Lookedin") %}Outside{% endif %}'
      - name: cat_cat3_updatetime
        state: "{{ state_attr('sensor.cat3','Time') }}"

input_select:
  cat1_location:
    options:
      - "Haus"
      - "Draußen"
      - "Unbekannt"
  cat2_location:
    options:
      - "Haus"
      - "Draußen"
      - "Unbekannt"
  cat3_location:
    options:
      - "Haus"
      - "Draußen"
      - "Unbekannt"

input_button:
  cat1_changeposition:
  cat2_changeposition:
  cat3_changeposition:

automation catflap:
  - alias: "Position cat1 Draußen"
    trigger:
      - platform: state
        entity_id:
          - sensor.cat1
        attribute: Time
    condition:
      - condition: state
        entity_id: sensor.cat1
        attribute: Update_Mac
        state: MAC
      - condition: state
        entity_id: sensor.cat_cat1_where
        state: Outside
    action:
      - service: input_select.select_option
        data:
          option: Draußen
        target:
          entity_id: input_select.cat1_location
      - service: notify.mobile_app_pixel_3a
        data:
          message: cat1 ist nach Draußen gegangen
          title: Katzenklappe
  - alias: "Position cat1 Haus"
    trigger:
      - platform: state
        entity_id:
          - sensor.cat1
        attribute: Time
    condition:
      - condition: state
        entity_id: sensor.cat1
        attribute: Update_Mac
        state: MAC
      - condition: state
        entity_id: sensor.cat_cat1_where
        state: Inside
    action:
      - service: input_select.select_option
        data:
          option: Haus
        target:
          entity_id: input_select.cat1_location
      - service: notify.mobile_app_pixel_3a
        data:
          message: cat1 ist ins Haus gekommen
          title: Katzenklappe
  - alias: "Position cat1 ändern"
    trigger:
      - platform: state
        entity_id:
          - input_button.cat1_changeposition
        to: "On"
    condition: []
    action:
      - service: input_select.select_next
        data: {}
        target:
        entity_id: input_select.cat1_location
  - alias: "Position cat2 Draußen"
    trigger:
      - platform: state
        entity_id:
          - sensor.cat2
        attribute: Time
    condition:
      - condition: state
        entity_id: sensor.cat2
        attribute: Update_Mac
        state: MAC
      - condition: state
        entity_id: sensor.cat_cat2_where
        state: Outside
    action:
      - service: input_select.select_option
        data:
          option: Draußen
        target:
          entity_id: input_select.cat2_location
      - service: notify.mobile_app_pixel_3a
        data:
          message: cat2 ist nach Draußen gegangen
          title: Katzenklappe
  - alias: "Position cat2 Haus"
    trigger:
      - platform: state
        entity_id:
          - sensor.cat2
        attribute: Time
    condition:
      - condition: state
        entity_id: sensor.cat2
        attribute: Update_Mac
        state: MAC
      - condition: state
        entity_id: sensor.cat_cat2_where
        state: Inside
    action:
      - service: input_select.select_option
        data:
          option: Haus
        target:
          entity_id: input_select.cat2_location
      - service: notify.mobile_app_pixel_3a
        data:
          message: cat2 ist ins Haus gekommen
          title: Katzenklappe
  - alias: "Position cat2 ändern"
    trigger:
      - platform: state
        entity_id:
          - input_button.cat2_changeposition
        to: "On"
    condition: []
    action:
      - service: input_select.select_next
        data: {}
        target:
        entity_id: input_select.cat2_location
  - alias: "Position cat3 Draußen"
    trigger:
      - platform: state
        entity_id:
          - sensor.cat3
        attribute: Time
    condition:
      - condition: state
        entity_id: sensor.cat3
        attribute: Update_Mac
        state: MAC
      - condition: state
        entity_id: sensor.cat_cat3_where
        state: Outside
    action:
      - service: input_select.select_option
        data:
          option: Draußen
        target:
          entity_id: input_select.cat3_location
      - service: notify.mobile_app_pixel_3a
        data:
          message: cat3 ist nach Draußen gegangen
          title: Katzenklappe
  - alias: "Position cat3 Haus"
    trigger:
      - platform: state
        entity_id:
          - sensor.cat3
        attribute: Time
    condition:
      - condition: state
        entity_id: sensor.cat3
        attribute: Update_Mac
        state: MAC
      - condition: state
        entity_id: sensor.cat_cat3_where
        state: Inside
    action:
      - service: input_select.select_option
        data:
          option: Haus
        target:
          entity_id: input_select.cat3_location
      - service: notify.mobile_app_pixel_3a
        data:
          message: cat3 ist ins Haus gekommen
          title: Katzenklappe
  - alias: "Position cat3 ändern"
    trigger:
      - platform: state
        entity_id:
          - input_button.cat3_changeposition
        to: "On"
    condition: []
    action:
      - service: input_select.select_next
        data: {}
        target:
        entity_id: input_select.cat3_location
 

What I'm struggling with is the Implementation in Lovelace. I wasn't able to configure the Buttons for each cat so that when i press the button the Postion will change. Also I failed to use catflap_battery_status to change the color of battery symbol. And lastly I did not find out how to configure the cats states to have different colors. Home Assistant is really cool but very difficult to learn :-(
Maybe if you find time to give me some advice with the remaining stuff I'd really be grateful :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants