al-one / hass-xiaomi-miot

Automatic integrate all Xiaomi devices to HomeAssistant via miot-spec, support Wi-Fi, BLE, ZigBee devices. 小米米家智能家居设备接入Hass集成
https://miot-spec.com
Apache License 2.0
4.42k stars 617 forks source link

hfjh.fishbowl.v2 - Missing functionalities to be added #326

Closed pplucky closed 2 years ago

pplucky commented 2 years ago

I have recently bought this aquarium with model 'hfjh.fishbowl.v2' and I decided to give a try to your custom component.

Based on the device available properties, I think it would be useful to use some to make it work on different device types:

Functionality wise, for me, it would make sense to be able to:

I am available for testing/contribute somehow, if you're willing to also.

Looking forward for your feedback

al-one commented 2 years ago

Light - to use the ledboard changeable properties (such as led colour/brightness);

The device uses a non-standard miot light service. You can only control the brightness and color through the number entity. Please modify your configuration:

# configuration.yaml
xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
      select_properties: ledboard_model
      number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed

Of course, you can also create a light entity using a template.


  • Number - to set number of feeds;
  • Button - to trigger the 'set-feed-single' action.
# configuration.yaml
xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
      select_properties: ledboard_model
      number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed,feed_num
      button_actions: set_feed_single

button_actions requires the latest commit in: v0.4.x.


  • Sensor - to get latest feed records (siid: 4 / piid: 16) <-- not sure if possible and how!

Property feed-record does not have read permission.

pplucky commented 2 years ago

Light - to use the ledboard changeable properties (such as led colour/brightness);

The device uses a non-standard miot light service. You can only control the brightness and color through the number entity. Please modify your configuration:

# configuration.yaml
xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
      select_properties: ledboard_model
      number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed

Of course, you can also create a light entity using a template.

I had already thought of that, but light template requires the color_template to be supplied in a tuple hue+saturation, while MioT returns an integer. Doing this conversion in a template doesn't seem like an easy task. Any other idea/possibility?

button_actions requires the latest commit in: v0.4.x.

I manually installed this branch, but I suppose this will come as part of the next release, right?

  • Sensor - to get latest feed records (siid: 4 / piid: 16) <-- not sure if possible and how!

Property feed-record does not have read permission.

The feeder in this fishtank is removable and when the action is called and feeder in not in place, an error is logged in Mi Home app (I was hoping feed-record would also contain this information, but I suppose we'll never know). Is there any feedback/status/output from the action call 'set_feed_single'? Otherwise you need to call the action and hope it will go right, as there is no way of knowing if it succeeded or not.

Thanks in advance for all your help.

al-one commented 2 years ago

I had already thought of that, but light template requires the color_template to be supplied in a tuple hue+saturation, while MioT returns an integer. Doing this conversion in a template doesn't seem like an easy task. Any other idea/possibility?

Perhaps only brightness and color are supported through the light entity, and other properties still need to be through the number entity.


I manually installed this branch, but I suppose this will come as part of the next release, right?

Of course !


The feeder in this fishtank is removable and when the action is called and feeder in not in place, an error is logged in Mi Home app (I was hoping feed-record would also contain this information, but I suppose we'll never know).

# configuration.yaml
xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
      select_properties: ledboard_model
      number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed,feed_num
      button_actions: set_feed_single
      miio_cloud_props: event.4.1 # latest feed-record event

Is there any feedback/status/output from the action call 'set_feed_single'? Otherwise you need to call the action and hope it will go right, as there is no way of knowing if it succeeded or not.

Call action will generate an attribute miot_action_result in the main entity.

pplucky commented 2 years ago

Perhaps only brightness and color are supported through the light entity, and other properties still need to be through the number entity.

Yes, but as color needs to be set in a tuple hue + saturation (as per light template definitions), this needs rgb integer conversion to hs to work.

# configuration.yaml
xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
      select_properties: ledboard_model
      number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed,feed_num
      button_actions: set_feed_single
      miio_cloud_props: event.4.1 # latest feed-record event

Is there any feedback/status/output from the action call 'set_feed_single'? Otherwise you need to call the action and hope it will go right, as there is no way of knowing if it succeeded or not.

Call action will generate an attribute miot_action_result in the main entity.

Does this mean that I will also need the cloud active for this to work?

Here is my current config (assuming cloud will be needed):

xiaomi_miot:
### Enable Xiaomi Cloud
  username: !secret xiaomi_cloud_username
  password: !secret xiaomi_cloud_password
  device_customizes:
    hfjh.fishbowl.v2:
### Generate number entities for pump flux, brightness and led color
      number_properties: pump_flux,ledboard_brightness,ledboard_color 
### Generate a button entity to trigger the action to feed the fish
      button_actions: set_feed_single
### Add attribute miio_action_result when button action is triggered
      miio_cloud_props: event.4.1 # latest feed-record event

I'm currently not physically near the devices, so I'll only test it properly (at least the action), once I am.

pplucky commented 2 years ago

One more question, please: each time I changed this config, do I need to restart HA for it to be considered? I tried reloading the integration device but that didn't seem to work.

Thanks in advance.

al-one commented 2 years ago

These options can also be configured to customize:

# customize.yaml
switch.hfjh_v2_entity_id:
  select_properties: ledboard_model
  number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed,feed_num
  button_actions: set_feed_single
  miio_cloud_props: event.4.1 # latest feed-record event

⚙️ Configuration > Server Controls > YAML configuration reloading > Location & customizations

pplucky commented 2 years ago

Perhaps only brightness and color are supported through the light entity, and other properties still need to be through the number entity.

Yes, but as color needs to be set in a tuple hue + saturation (as per light template definitions), this needs rgb integer conversion to hs to work.

# configuration.yaml
xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
      select_properties: ledboard_model
      number_properties: ledboard_brightness,ledboard_sun,ledboard_color,ledboard_stream,ledboard_speed,feed_num
      button_actions: set_feed_single
      miio_cloud_props: event.4.1 # latest feed-record event

Is there any feedback/status/output from the action call 'set_feed_single'? Otherwise you need to call the action and hope it will go right, as there is no way of knowing if it succeeded or not.

Call action will generate an attribute miot_action_result in the main entity.

Does this mean that I will also need the cloud active for this to work?

al-one commented 2 years ago

In the local mode, this attribute miot_action_result also exists after call action.

pplucky commented 2 years ago

In the local mode, this attribute miot_action_result also exists after call action.

I disabled cloud and attribute event.4.1 generated by miio_cloud_props was gone (as expected).

Attribute miot_action_result was filled after running the action, but nothing different appears to be logged when successful or not (code = 0, regardless of feeder being available or not).

This is the output:

did: '243973991'
siid: 4
aiid: 1
code: 0

Any idea why?

Another question: will this miot_action_result only be filled if the action is triggered? If I physically click the feeder button, will it also be filled?

If I check the attribute event.4.1 generated by miio_cloud_props, I can check different data in the generated json (aside from the timestamp).

Output for failed feed attempt:

{"timestamp":1641153952,"value":[{"piid":16,"value":"2022,1,2,21,5,2,0"}]}

Output for successful feed attempt:

{"timestamp":1641197882,"value":[{"piid":16,"value":"2022,1,3,9,18,0,1"}]}

The last 2 values in value seem to correspond to a return code and number of feeds (return code 2 = error? and number of feeds = 0 - as feeder did not work).

Is there any place where I can understand the meaning of all these parameters also?

Thanks in advance for all your help.

al-one commented 2 years ago

https://github.com/al-one/hass-xiaomi-miot/blob/b3c8027941396682698f15d884115c9654927ae0/custom_components/xiaomi_miot/core/miot_local_devices.py#L79 hfjh.fishbowl.v2 has been added in MIOT_LOCAL_MODELS, You can upgrade to master branch and integrate it through automatic mode, that it can query miio_cloud_props while using local mode.

pplucky commented 2 years ago

https://github.com/al-one/hass-xiaomi-miot/blob/b3c8027941396682698f15d884115c9654927ae0/custom_components/xiaomi_miot/core/miot_local_devices.py#L79

hfjh.fishbowl.v2 has been added in MIOT_LOCAL_MODELS, You can upgrade to master branch and integrate it through automatic mode, that it can query ``miio_cloud_props while using local mode.

Updating to latest version 0.4.17 is enough? I suppose I need to add the integration device again to set it as 'Automatic', right? But if I understood correctly, Automatic mode still needs Mi account user+password. Or is this needed only during setup and not in config?

My device currently has: State updater lan

What would be the difference between having it as it is or changing it to Automatic mode?

al-one commented 2 years ago

The version 0.4.17 is enough. Automatic mode will make miio_cloud_props work.

pplucky commented 2 years ago

Output for successful feed attempt:

{"timestamp":1641197882,"value":[{"piid":16,"value":"2022,1,3,9,18,0,1"}]}

The last 2 values in value seem to correspond to a return code and number of feeds (return code 2 = error? and number of feeds = 0 - as feeder did not work).

Is there any place where I can understand the meaning of all these parameters also?

Any idea on this?

The version 0.4.17 is enough. Automatic mode will make miio_cloud_props work.

All good after upgrading and re-adding the integration in Automatic mode, as per your instructions. Thanks a lot @al-one

al-one commented 2 years ago

This is the code in the react plugin of the device:

  function formatRecord(recordStr) {
    var symbols = ['', '-', ' ', ':', '  ', ' ', ''];
    var result = '';
    var record = recordStr.split(',');
    if (record.length !== 7) return undefined;

    for (var i = 0; i < record.length; i++) {
      if (i === 0) {} else if (i === 5) {
        if (record[i] == 0) {
          result += _FBLocalizableString.localStrings.feed_success;
        } else if (record[i] == 1) {
          result += _FBLocalizableString.localStrings.feed_fail;
          result += _FBLocalizableString.localStrings.feed_fail_poweroff;
        } else if (record[i] == 2) {
          result += _FBLocalizableString.localStrings.feed_fail;
          result += _FBLocalizableString.localStrings.feed_fail_feeder_failure;
        }
      } else if (i === 6) {} else {
        if (record[i] < 10) {
          result += '0';
        }

        result += record[i];
        result += symbols[i];
      }
    }

    var fRecord = {
      text: result,
      success: record[5] == 0,
      feedNum: record[6] + " " + _FBLocalizableString.localStrings.feed_num_unit
    };
    return fRecord;
  }

hfjh.fishbowl.v2.js

pplucky commented 2 years ago

This is the code in the react plugin of the device:

  function formatRecord(recordStr) {
    var symbols = ['', '-', ' ', ':', '  ', ' ', ''];
    var result = '';
    var record = recordStr.split(',');
    if (record.length !== 7) return undefined;

    for (var i = 0; i < record.length; i++) {
      if (i === 0) {} else if (i === 5) {
        if (record[i] == 0) {
          result += _FBLocalizableString.localStrings.feed_success;
        } else if (record[i] == 1) {
          result += _FBLocalizableString.localStrings.feed_fail;
          result += _FBLocalizableString.localStrings.feed_fail_poweroff;
        } else if (record[i] == 2) {
          result += _FBLocalizableString.localStrings.feed_fail;
          result += _FBLocalizableString.localStrings.feed_fail_feeder_failure;
        }
      } else if (i === 6) {} else {
        if (record[i] < 10) {
          result += '0';
        }

        result += record[i];
        result += symbols[i];
      }
    }

    var fRecord = {
      text: result,
      success: record[5] == 0,
      feedNum: record[6] + " " + _FBLocalizableString.localStrings.feed_num_unit
    };
    return fRecord;
  }

hfjh.fishbowl.v2.js

Perfect @al-one, my assumption was right on the positions in string (separated by ,):

Thanks a lot for all your help.

For future reference for others, here is my relevant configuration (a lot coming from your suggestions above):

####################################################
#                                                  #
#            Xiaomi MioT Auto settings             #
#                                                  #
####################################################
xiaomi_miot:
### Ideas/suggestions taken from https://github.com/al-one/hass-xiaomi-miot/issues/326
  device_customizes:
    hfjh.fishbowl.v2:
### Generate number entities for pump flux, brightness and led color
      number_properties: pump_flux,ledboard_brightness,ledboard_color 
### Generate a button entity to trigger the action to feed the fish
      button_actions: set_feed_single
### Add attribute to keep track of latest feed-record event
      miio_cloud_props: event.4.1
  exclude_state_attributes:
### Generic attributes to exclude
    - mac_address
### Aquarium attributes to exclude
    - function.mcu_type
    - function.mcu_type_new
    - function.key_switch
    - function.feed_time_week
    - function.feed_num
    - function.feed_time
    - function.feed_time_switch
    - function.ledboard_time_switch
    - function.ledboard_time_open
    - function.ledboard_time_close
    - function.ledboard_model
    - function.ledboard_sun
    - function.ledboard_stream
    - function.ledboard_speed
    - function.ledboard_time_switch
    - function.ledboard_time_open
    - function.ledboard_time_close

####################################################
#                                                  #
#                      Light                       #
#                                                  #
####################################################
light:
  - platform: template
    lights:
      fishbowl_light:
        friendly_name: "Aquarium Light"
### Brightness returned by MioT is in %, while the light brightness is 0-255
        level_template: >-
            {%- if states('number.aquarium_brightness') -%}
                {{ (((states('number.aquarium_brightness')|int)*255)/100)|int }}
            {%- endif -%}
        value_template: >-
            {%- if states('number.aquarium_brightness') -%}
              {{ states('number.aquarium_brightness')|int > 0 }}
            {%- endif -%}
        availability_template: "{{ states('switch.aquarium') in ['on', 'off'] }}"
        icon_template:  "{{ 'mdi:lightbulb' }}"
### Brightness returned by MioT is in %, while the light brightness is 0-255
        set_level:
          service: number.set_value
          data:
            entity_id: number.aquarium_brightness
            value: "{{ (((brightness|int)/255)*100)|int }}"
        turn_on:
          service: script.turn_on_fishbowl_light
        turn_off:
          service: script.turn_off_fishbowl_light

####################################################
#                                                  #
#            Template Binary Sensors               #
#                                                  #
####################################################
# Entity attribute Event.4.1 contains latest feed  #
# log entry, in a JSON format like the following:  #
# Failed feed record (example)
# {
#   "timestamp": 1641153952,
#   "value": [
#     {
#       "piid": 16,
#       "value": "2022,1,2,21,5,2,0"
#     }
#   ]
# }
# Successfull feed record (example)
# {
#   "timestamp": 1641197882,
#   "value": [
#     {
#       "piid": 16,
#       "value": "2022,1,3,9,18,0,1"
#     }
#   ]
# }
# The value string seems to contain year, month,   #
# day, hours, minutes, return code (0 = OK,        #
# <> 0 = NOK) and number of portions.              #
# To know if an an error occurred, checking if the #
# return code is set to 0                          #
####################################################
binary_sensor:
  - platform: template
    sensors:
      feed_record_result:
        value_template: >-
            {%- if state_attr('switch.aquarium','event.4.1') -%}
                {% set feed_record = state_attr('switch.aquarium','event.4.1')|from_json %}
                {% set feed_result = feed_record.value[0].value.split(',')[5] %}
                {{ feed_result|int != 0 }}
            {%- endif -%}
        availability_template: "{{ states('switch.aquarium') in ['on', 'off'] }}"
        attribute_templates:
          timestamp: >-
            {%- if state_attr('switch.aquarium','event.4.1') -%}
                {% set feed_record = state_attr('switch.aquarium','event.4.1')|from_json %}
                {{ as_datetime(feed_record.timestamp)|timestamp_local }}
            {%- endif -%}
        friendly_name: 'Automatic Feeder'
        device_class: problem

####################################################
#                                                  #
#                Template Sensor                   #
#                                                  #
####################################################
sensor:
  - platform: template
    sensors:
      feed_record_timestamp:
        value_template: >-
            {%- if state_attr('switch.aquarium','event.4.1') -%}
              {% set feed_record = state_attr('switch.aquarium','event.4.1')|from_json %}
              {{ as_datetime(feed_record.timestamp)|timestamp_local }}
            {%- endif -%}
        friendly_name: 'Last Fish Feed'
        icon_template: "{{ 'mdi:fish' }}"
        device_class: timestamp

####################################################
#                                                  #
#                     Scripts                      #
#                                                  #
####################################################
script:
### Turn Light On
  turn_on_fishbowl_light:
    alias: "Turn on Fishbowl Light"
    sequence:
      - service: number.set_value
        data:
          entity_id: number.aquarium_color
          value: 16777215
      - service: number.set_value
        data:
          entity_id: number.aquarium_brightness
          value: 100

### Turn off Light
  turn_off_fishbowl_light:
    alias: "Turn off Fishbowl Light"
    sequence:
      - service: number.set_value
        data:
          entity_id: number.aquarium_brightness
          value: 0

### Feed the Fish
  feed_the_fish:
    alias: "Feed the Fish"
    sequence:
      - service: button.press
        data:
          entity_id: button.aquarium_feed_single
      - delay: 60 #Wait until entity attributes are updated after action is triggered (every 30 seconds)
      - choose:
          - alias: "Feed log record has an error --> no feeding happened"
            conditions:
              - condition: state
                entity_id: binary_sensor.feed_record_result
                state: 'on'
            sequence:
              - service: notify.ios
                data:
                  message: "🐟❌ Error while feeding fish"
          - alias: "Feed log record has NO error --> feeding OK"
            conditions:
              - condition: state
                entity_id: binary_sensor.feed_record_result
                state: 'off'
            sequence:
              - service: notify.ios
                data:
                  message: "🐟✔️ Fish feed time"

and also how it looks like in UI: image

Ofir-P commented 1 year ago

This is the code in the react plugin of the device:

  function formatRecord(recordStr) {
    var symbols = ['', '-', ' ', ':', '  ', ' ', ''];
    var result = '';
    var record = recordStr.split(',');
    if (record.length !== 7) return undefined;

    for (var i = 0; i < record.length; i++) {
      if (i === 0) {} else if (i === 5) {
        if (record[i] == 0) {
          result += _FBLocalizableString.localStrings.feed_success;
        } else if (record[i] == 1) {
          result += _FBLocalizableString.localStrings.feed_fail;
          result += _FBLocalizableString.localStrings.feed_fail_poweroff;
        } else if (record[i] == 2) {
          result += _FBLocalizableString.localStrings.feed_fail;
          result += _FBLocalizableString.localStrings.feed_fail_feeder_failure;
        }
      } else if (i === 6) {} else {
        if (record[i] < 10) {
          result += '0';
        }

        result += record[i];
        result += symbols[i];
      }
    }

    var fRecord = {
      text: result,
      success: record[5] == 0,
      feedNum: record[6] + " " + _FBLocalizableString.localStrings.feed_num_unit
    };
    return fRecord;
  }

hfjh.fishbowl.v2.js

Perfect @al-one, my assumption was right on the positions in string (separated by ,):

  • 6th position corresponds to status (0 = OK; >0 = NOK);
  • 7th position corresponds to number of portions feeded.

Thanks a lot for all your help.

For future reference for others, here is my relevant configuration (a lot coming from your suggestions above):

####################################################
#                                                  #
#            Xiaomi MioT Auto settings             #
#                                                  #
####################################################
xiaomi_miot:
### Ideas/suggestions taken from https://github.com/al-one/hass-xiaomi-miot/issues/326
  device_customizes:
    hfjh.fishbowl.v2:
### Generate number entities for pump flux, brightness and led color
      number_properties: pump_flux,ledboard_brightness,ledboard_color 
### Generate a button entity to trigger the action to feed the fish
      button_actions: set_feed_single
### Add attribute to keep track of latest feed-record event
      miio_cloud_props: event.4.1
  exclude_state_attributes:
### Generic attributes to exclude
    - mac_address
### Aquarium attributes to exclude
    - function.mcu_type
    - function.mcu_type_new
    - function.key_switch
    - function.feed_time_week
    - function.feed_num
    - function.feed_time
    - function.feed_time_switch
    - function.ledboard_time_switch
    - function.ledboard_time_open
    - function.ledboard_time_close
    - function.ledboard_model
    - function.ledboard_sun
    - function.ledboard_stream
    - function.ledboard_speed
    - function.ledboard_time_switch
    - function.ledboard_time_open
    - function.ledboard_time_close

####################################################
#                                                  #
#                      Light                       #
#                                                  #
####################################################
light:
  - platform: template
    lights:
      fishbowl_light:
        friendly_name: "Aquarium Light"
### Brightness returned by MioT is in %, while the light brightness is 0-255
        level_template: >-
            {%- if states('number.aquarium_brightness') -%}
                {{ (((states('number.aquarium_brightness')|int)*255)/100)|int }}
            {%- endif -%}
        value_template: >-
            {%- if states('number.aquarium_brightness') -%}
              {{ states('number.aquarium_brightness')|int > 0 }}
            {%- endif -%}
        availability_template: "{{ states('switch.aquarium') in ['on', 'off'] }}"
        icon_template:  "{{ 'mdi:lightbulb' }}"
### Brightness returned by MioT is in %, while the light brightness is 0-255
        set_level:
          service: number.set_value
          data:
            entity_id: number.aquarium_brightness
            value: "{{ (((brightness|int)/255)*100)|int }}"
        turn_on:
          service: script.turn_on_fishbowl_light
        turn_off:
          service: script.turn_off_fishbowl_light

####################################################
#                                                  #
#            Template Binary Sensors               #
#                                                  #
####################################################
# Entity attribute Event.4.1 contains latest feed  #
# log entry, in a JSON format like the following:  #
# Failed feed record (example)
# {
#   "timestamp": 1641153952,
#   "value": [
#     {
#       "piid": 16,
#       "value": "2022,1,2,21,5,2,0"
#     }
#   ]
# }
# Successfull feed record (example)
# {
#   "timestamp": 1641197882,
#   "value": [
#     {
#       "piid": 16,
#       "value": "2022,1,3,9,18,0,1"
#     }
#   ]
# }
# The value string seems to contain year, month,   #
# day, hours, minutes, return code (0 = OK,        #
# <> 0 = NOK) and number of portions.              #
# To know if an an error occurred, checking if the #
# return code is set to 0                          #
####################################################
binary_sensor:
  - platform: template
    sensors:
      feed_record_result:
        value_template: >-
            {%- if state_attr('switch.aquarium','event.4.1') -%}
                {% set feed_record = state_attr('switch.aquarium','event.4.1')|from_json %}
                {% set feed_result = feed_record.value[0].value.split(',')[5] %}
                {{ feed_result|int != 0 }}
            {%- endif -%}
        availability_template: "{{ states('switch.aquarium') in ['on', 'off'] }}"
        attribute_templates:
          timestamp: >-
            {%- if state_attr('switch.aquarium','event.4.1') -%}
                {% set feed_record = state_attr('switch.aquarium','event.4.1')|from_json %}
                {{ as_datetime(feed_record.timestamp)|timestamp_local }}
            {%- endif -%}
        friendly_name: 'Automatic Feeder'
        device_class: problem

####################################################
#                                                  #
#                Template Sensor                   #
#                                                  #
####################################################
sensor:
  - platform: template
    sensors:
      feed_record_timestamp:
        value_template: >-
            {%- if state_attr('switch.aquarium','event.4.1') -%}
              {% set feed_record = state_attr('switch.aquarium','event.4.1')|from_json %}
              {{ as_datetime(feed_record.timestamp)|timestamp_local }}
            {%- endif -%}
        friendly_name: 'Last Fish Feed'
        icon_template: "{{ 'mdi:fish' }}"
        device_class: timestamp

####################################################
#                                                  #
#                     Scripts                      #
#                                                  #
####################################################
script:
### Turn Light On
  turn_on_fishbowl_light:
    alias: "Turn on Fishbowl Light"
    sequence:
      - service: number.set_value
        data:
          entity_id: number.aquarium_color
          value: 16777215
      - service: number.set_value
        data:
          entity_id: number.aquarium_brightness
          value: 100

### Turn off Light
  turn_off_fishbowl_light:
    alias: "Turn off Fishbowl Light"
    sequence:
      - service: number.set_value
        data:
          entity_id: number.aquarium_brightness
          value: 0

### Feed the Fish
  feed_the_fish:
    alias: "Feed the Fish"
    sequence:
      - service: button.press
        data:
          entity_id: button.aquarium_feed_single
      - delay: 60 #Wait until entity attributes are updated after action is triggered (every 30 seconds)
      - choose:
          - alias: "Feed log record has an error --> no feeding happened"
            conditions:
              - condition: state
                entity_id: binary_sensor.feed_record_result
                state: 'on'
            sequence:
              - service: notify.ios
                data:
                  message: "🐟❌ Error while feeding fish"
          - alias: "Feed log record has NO error --> feeding OK"
            conditions:
              - condition: state
                entity_id: binary_sensor.feed_record_result
                state: 'off'
            sequence:
              - service: notify.ios
                data:
                  message: "🐟✔️ Fish feed time"

and also how it looks like in UI: image

Hey dude, I have added all those configurations but it seems that state is unavailable / off even tho' the device is functioning well and on. image image

pplucky commented 1 year ago

Hey dude, I have added all those configurations but it seems that state is unavailable / off even tho' the device is functioning well and on. image image

You need to make that you added this in your YAML configuration:

xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
### Generate number entities for pump flux, brightness and led color
      number_properties: ledboard_brightness

By the integration printscreen, this number entity does not exist and it must´(in my case, it is named number.aquario_brightness)!

And also add this configuration (don't forget changing the below entities number.aquario_brightness, switch.aquario, script.turn_on_fishbowl_light and script.turn_off_fishbowl_light by your ones):

light:
  - platform: template
    lights:
      fishbowl_light:
        friendly_name: "Luz Aquário"
        unique_id: luz_aquario
### Brightness returned by MioT is in %, while the light brightness is 0-255
        level_template: "{{ ( (((states('number.aquario_brightness')|int(0))*255)/100)|int ) if states('number.aquario_brightness') else 0 }}"
        value_template: "{{ ( states('number.aquario_brightness')|int(0) > 0) if states('number.aquario_brightness') else false }}"
        availability_template: "{{ states('switch.aquario') in ['on', 'off'] }}"
        icon_template: mdi:lightbulb
### Brightness returned by MioT is in %, while the light brightness is 0-255
        set_level:
          service: number.set_value
          target:
            entity_id: number.aquario_brightness
          data:
            value: "{{ (((brightness|int(0))/255)*100)|int }}"
        turn_on:
          service: script.turn_on_fishbowl_light
        turn_off:
          service: script.turn_off_fishbowl_light

This should do the trick, I suppose.

Ofir-P commented 1 year ago

Hey dude, I have added all those configurations but it seems that state is unavailable / off even tho' the device is functioning well and on. image image

You need to make that you added this in your YAML configuration:

xiaomi_miot:
  device_customizes:
    hfjh.fishbowl.v2:
### Generate number entities for pump flux, brightness and led color
      number_properties: ledboard_brightness

By the integration printscreen, this number entity does not exist and it must´(in my case, it is named number.aquario_brightness)!

And also add this configuration (don't forget changing the below entities number.aquario_brightness, switch.aquario, script.turn_on_fishbowl_light and script.turn_off_fishbowl_light by your ones):

light:
  - platform: template
    lights:
      fishbowl_light:
        friendly_name: "Luz Aquário"
        unique_id: luz_aquario
### Brightness returned by MioT is in %, while the light brightness is 0-255
        level_template: "{{ ( (((states('number.aquario_brightness')|int(0))*255)/100)|int ) if states('number.aquario_brightness') else 0 }}"
        value_template: "{{ ( states('number.aquario_brightness')|int(0) > 0) if states('number.aquario_brightness') else false }}"
        availability_template: "{{ states('switch.aquario') in ['on', 'off'] }}"
        icon_template: mdi:lightbulb
### Brightness returned by MioT is in %, while the light brightness is 0-255
        set_level:
          service: number.set_value
          target:
            entity_id: number.aquario_brightness
          data:
            value: "{{ (((brightness|int(0))/255)*100)|int }}"
        turn_on:
          service: script.turn_on_fishbowl_light
        turn_off:
          service: script.turn_off_fishbowl_light

This should do the trick, I suppose.

Thanks for replying! I have made sure I got it on my configuration.yaml but still no values showing up. Here is the configuration.yaml http://pastie.org/p/2PKBPNnl9197zaJTMdUmEt Sensors.yaml http://pastie.org/p/3gGprcYuU7RyXdnE5owOMA Scripts.yaml http://pastie.org/p/4WkWb7wosggOFglUG5RgIu

What am I missing here?

pplucky commented 1 year ago

Thanks for replying! I have made sure I got it on my configuration.yaml but still no values showing up. Here is the configuration.yaml http://pastie.org/p/2PKBPNnl9197zaJTMdUmEt Sensors.yaml http://pastie.org/p/3gGprcYuU7RyXdnE5owOMA Scripts.yaml http://pastie.org/p/4WkWb7wosggOFglUG5RgIu

What am I missing here?

As per the printscreens you sent from the integration, it seems that not all the customizing entities are not being generated.

Mine looks like this (the orange marked ones are the ones which are missing in yours): image

Can you maybe remove the device integration and add it again via cloud? In order to know the feeding result, this needs to be added via cloud. Not sure if yours is the same.

Otherwise I think you should activated debug log for the custom component/library to try to understand why this is working differently for you.

Ofir-P commented 1 year ago

Thanks for replying! I have made sure I got it on my configuration.yaml but still no values showing up. Here is the configuration.yaml http://pastie.org/p/2PKBPNnl9197zaJTMdUmEt Sensors.yaml http://pastie.org/p/3gGprcYuU7RyXdnE5owOMA Scripts.yaml http://pastie.org/p/4WkWb7wosggOFglUG5RgIu What am I missing here?

As per the printscreens you sent from the integration, it seems that not all the customizing entities are not being generated.

Mine looks like this (the orange marked ones are the ones which are missing in yours): image

Can you maybe remove the device integration and add it again via cloud? In order to know the feeding result, this needs to be added via cloud. Not sure if yours is the same.

Otherwise I think you should activated debug log for the custom component/library to try to understand why this is working differently for you.

image I can see them now. However, I see that the binary sensor is off, and the feed record timestamp is unavailable, is that because I need to feed them the first time via the new entities or not? image

pplucky commented 1 year ago

However, I see that the binary sensor is off, and the feed record timestamp is unavailable, is that because I need to feed them the first time via the new entities or not?

In your switch entity corresponding to the aquarium, you must have something like this, for the sensor to be fed properly: image

This information usually corresponds to the last line in the Mi Home feeding log: image

Check your Mi Home for feeding logs and try pushing the physical feed button to check if it works (both in Mi Home and HA).

Ofir-P commented 1 year ago

However, I see that the binary sensor is off, and the feed record timestamp is unavailable, is that because I need to feed them the first time via the new entities or not?

In your switch entity corresponding to the aquarium, you must have something like this, for the sensor to be fed properly: image

This information usually corresponds to the last line in the Mi Home feeding log: image

Check your Mi Home for feeding logs and try pushing the physical feed button to check if it works (both in Mi Home and HA).

The entity corresponding shows that: image

However, by using the Watchman addon which monitors missing/unavailable entities I can see that there is a problem with image This is the script: http://pastie.org/p/2FCPXTBtptVrvA3eq6A6sj image

Mi Home Feeding logs: image

pplucky commented 1 year ago

Buttons in HA usually have state 'unknown' until they are "pushed" (the state of mine is the last feed timestamp). My feeding automation is done within HA, as sometimes the kids feed the fish by pushing the physical button and I don't want 2 feed attempts to happen in a short period of time (I control that over HA).

You're not using the button to feed, but instead using the Mi Home 'Automatic Feeding', so I suppose that you'll only be able to get the sensor with the last feeding record and the binary sensor to tell you if there was any issue in the last feeding record (binary sensor will be off if all OK and on if an issue happened), but never the button state!

Ofir-P commented 1 year ago

Buttons in HA usually have state 'unknown' until they are "pushed" (the state of mine is the last feed timestamp). My feeding automation is done within HA, as sometimes the kids feed the fish by pushing the physical button and I don't want 2 feed attempts to happen in a short period of time (I control that over HA).

You're not using the button to feed, but instead using the Mi Home 'Automatic Feeding', so I suppose that you'll only be able to get the sensor with the last feeding record and the binary sensor to tell you if there was any issue in the last feeding record (binary sensor will be off if all OK and on if an issue happened), but never the button state!

Thanks for replying!

Yeah, I want to use the sensors to know and get phone notifications whenever the machine has fed them or not, I'm not using via HA because I got a lot of internet problems with my HA so I can't rely on that YET. So how do I get the sensor with the last feeding record and binary sensor in case I'm using the Mi Home Automatic Feeding feature?

pplucky commented 1 year ago

Yeah, I want to use the sensors to know and get phone notifications whenever the machine has fed them or not, I'm not using via HA because I got a lot of internet problems with my HA so I can't rely on that YET. So how do I get the sensor with the last feeding record and binary sensor in case I'm using the Mi Home Automatic Feeding feature?

As far as I can "see", you have all that is needed for both template sensor & binary_sensors to work properly.

Have you tried testing the templates in HA developer tools, to check if a valid result is yielded.

          {%- if state_attr('switch.aquario','event.4.1') -%}
              {% set feed_record = state_attr('switch.aquario','event.4.1')|from_json %}
              {% set feed_result = feed_record.value[0].value.split(',')[5] %}
              {{ feed_result|int(0) != 0 }}
          {%- endif -%}

Should return true (a feeding issue occurred) or false (no issue).

            {%- if is_state('binary_sensor.feed_record_result','off') -%}
                {%- if state_attr('switch.aquario','event.4.1') -%}
                    {% set feed_record = state_attr('switch.aquario','event.4.1')|from_json %}
                    {{ feed_record.timestamp|timestamp_local }}
                {%- endif -%}
            {%- endif -%}

Should return a timestamp corresponding to the date/time in your last successful feed record.

Double/triple check the entity names and also test the templates as suggested, you're almost there.

Ofir-P commented 1 year ago

Yeah, I want to use the sensors to know and get phone notifications whenever the machine has fed them or not, I'm not using via HA because I got a lot of internet problems with my HA so I can't rely on that YET. So how do I get the sensor with the last feeding record and binary sensor in case I'm using the Mi Home Automatic Feeding feature?

As far as I can "see", you have all that is needed for both template sensor & binary_sensors to work properly.

Have you tried testing the templates in HA developer tools, to check if a valid result is yielded.

          {%- if state_attr('switch.aquario','event.4.1') -%}
              {% set feed_record = state_attr('switch.aquario','event.4.1')|from_json %}
              {% set feed_result = feed_record.value[0].value.split(',')[5] %}
              {{ feed_result|int(0) != 0 }}
          {%- endif -%}

Should return true (a feeding issue occurred) or false (no issue).

            {%- if is_state('binary_sensor.feed_record_result','off') -%}
                {%- if state_attr('switch.aquario','event.4.1') -%}
                    {% set feed_record = state_attr('switch.aquario','event.4.1')|from_json %}
                    {{ feed_record.timestamp|timestamp_local }}
                {%- endif -%}
            {%- endif -%}

Should return a timestamp corresponding to the date/time in your last successful feed record.

Double/triple check the entity names and also test the templates as suggested, you're almost there.

The first template I got false (boolean) image The second template shows a timestamp but in a weird pattern. image

Does it means if I want to create an automation based on that I can do it like that: http://pastie.org/p/4B0uaCz2zrNazyEE81aITR ?

And how do I change the pattern of the timestamp, it seems a little buggy for me. Thanks for help!

pplucky commented 1 year ago

Does it means if I want to create an automation based on that I can do it like that: http://pastie.org/p/4B0uaCz2zrNazyEE81aITR ?

That will not work, as the binary_sensor will never change state to on, unless there is an issue with a feeding, and trigger will only happen when the state changes to 'off' .

I don't have an automation like that, because I notify success only when away from home and include it in the script which does the feed button press. Maybe you can use a state change for the sensor with feeding timestamp to do it instead, just an idea.

And how do I change the pattern of the timestamp, it seems a little buggy for me. Thanks for help!

Timestamp is perfect, it is in format ISO 8601, workable in HA and on Lovelace: image

You need to work out your templating capabilities to get what you want/need out of it, but this is definitely WAY off-topic for the custom component.