bodymiscale icon indicating copy to clipboard operation
bodymiscale copied to clipboard

Data lose after restart HA

Open sasukebinbin opened this issue 2 years ago • 23 comments

Not sure if this is expected. Everytime restart HA, all data from bodymiscale will lose and status will become problem.

sasukebinbin avatar Oct 12 '21 15:10 sasukebinbin

Je ne sais pas si cela est attendu. À chaque redémarrage de HA, toutes les données de bodymiscale seront perdues et le statut deviendra un problème.

Yes this is normal, the component reads the sensor (s). To compensate, I put in place a node red. https://community.home-assistant.io/t/exemple-xiaomi-miscale-select-the-person-who-weighs-himself-notif-action/299321

dckiller51 avatar Oct 12 '21 22:10 dckiller51

There is a RestoreEntity class that may be subclassed and utilized to implement such functionality.

I have recently forked this repository, and will attempt to add this.

alryaz avatar Oct 25 '21 14:10 alryaz

this can be solved in a simple way (when using BLE monitor for Homeassistant):

go to integrations, choose "configure" on the Bluetooth Low Energy Monitor integration, check if "Restore state" is activated, otherwise activate it. Now, after every restart of HA, the last State is shown

Maybe dckiller51 can put that hint into the readme.md

criticallimit avatar Oct 31 '21 15:10 criticallimit

Bluetooth Low Energy Monitor integration,

There is no Bluetooth Low Energy Monitor integration under Configure->intergrations.

sasukebinbin avatar Nov 01 '21 05:11 sasukebinbin

are you using esphome or Passive BLE monitor integration?

if you are using esphome, I don´t have a solution, but on passive BLE monitor it is working fine

criticallimit avatar Nov 01 '21 11:11 criticallimit

are you using esphome or Passive BLE monitor integration?

if you are using esphome, I don´t have a solution, but on passive BLE monitor it is working fine unfortunately I using esphome.

sasukebinbin avatar Nov 01 '21 11:11 sasukebinbin

Would it be possible to select for the weight and impedance sensors a helper? i have an automation already setup to store the last recorded weight and impedance into a number helper (which gets restored at start time) so this would alleviate this regardless of ble integration or esphome.

cranky-coder avatar Aug 16 '22 15:08 cranky-coder

Indeed, it could be simpler if it would be possible to specify an input_number helper as inputs for the integration. Otherwise it is necessary to add extra template sensors as a workaround to bypass helpers' values like this:

template:
  - sensor:
    - name: "Weight (last value)"
      unique_id: weight_last
      unit_of_measurement: "Kg"
      icon: "mdi:weight-kilogram"
      state: "{{ states('input_number.weight_last') }}"

    - name: "Impedance (last value)"
      unique_id: impedance_last
      unit_of_measurement: "Omh"
      icon: "mdi:omega"
      state: "{{ states('input_number.impedance_last') }}"

For completeness, input number helpers:

input_number:
  weight_last:
    name: "Weight (last value)"
    min: 0
    max: 100
    step: 0.01
    unit_of_measurement: "Kg"
    icon: "mdi:weight-kilogram"
    mode: box

  impedance_last:
    name: "Impedance (last value)"
    min: 0
    max: 1000
    step: 1
    unit_of_measurement: "Omh"
    icon: "mdi:omega"
    mode: box

Automations to update input number helpers when sensor values obtained from ESPHome change:

- id: '1663000060470'
  alias: Save weight
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.weight
  condition: []
  action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.weight'') | float }}'
    target:
      entity_id: input_number.weight_last
  mode: single
- id: '1663000498790'
  alias: Save impedance
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.impedance
  condition: []
  action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.impedance'') | float }}'
    target:
      entity_id: input_number.impedance_last
  mode: single

vladmovchan avatar Sep 12 '22 18:09 vladmovchan

That's almost exactly what Im doing now as well. Just a bummer that the other values from the integration won't get updated till the next weigh in. which is why I had wondered if we could just modify the selection from just those sensor values to just selecting the entity you want (in this case it would be the helpers)

cranky-coder avatar Sep 12 '22 19:09 cranky-coder

Hello everyone, I follow your comments, have you looked at my proposal to go through node red and Node Red Companion. With this solution no problem after a reboot but given are stored in each sensor node red companions

https://community.home-assistant.io/t/exemple-xiaomi-miscale-select-the-person-who-weighs-himself-notif-action/299321

dckiller51 avatar Sep 12 '22 19:09 dckiller51

Sorry, I'm new to HA, and I'm not familiar with Node Red yet.

vladmovchan avatar Sep 12 '22 19:09 vladmovchan

I don't use node red, nor have any plans to. So to do node red would add a full layer of something new just for this. Just curious (from a developer perspective) what is the concern with allowing the selection of a helper entity? please note, i have done 0 HA development, so I have no idea how involved any of that is :)

cranky-coder avatar Sep 12 '22 20:09 cranky-coder

I forgot to specify following one of the last updates. Can you check your sensors normally each attribute has its own sensor this one must keep your data after the restart? Can you confirm it for me because my card currently uses the attributes and not the sensors? If this is ok for you then I should revise my card accordingly.

dckiller51 avatar Sep 13 '22 08:09 dckiller51

Which update? I am on 3.0.3 and I still show all the sensors as unknown until i get on the scale after a restart.

cranky-coder avatar Sep 14 '22 03:09 cranky-coder

input_number: weight_last: name: "Weight (last value)" min: 0 max: 100 step: 0.01 unit_of_measurement: "Kg" icon: "mdi:weight-kilogram" mode: box

impedance_last: name: "Impedance (last value)" min: 0 max: 1000 step: 1 unit_of_measurement: "Omh" icon: "mdi:omega" mode: box

Can you please elaborate where to implement this solution as I'm a noob noobie? Thank you in adnvance!

grego1981 avatar Oct 13 '22 09:10 grego1981

@grego1981 input_number section which you quoted has to be added to configuration.yaml file. After it is added you need to restart HA: Developer tools -> Check configuration and if the result is green -> Restart.

Or as a simpler approach, you could do the same via HA web interface without modifying configuration.yaml. Just go to Settings -> Devices & Services -> Helpers tab -> Create helper -> Number -> Fill the fields with appropriate values -> Create

According to the https://github.com/dckiller51/bodymiscale/releases/tag/v3.0.4 with the very recent version of the plugin it is no longer necessary to create template sensors (the first code snippet in my post). And it should be possible to use input number helpers directly in the plugin.

And automations (the third code snippet) are created via Services -> Automations & Scenes -> Create Automation.

vladmovchan avatar Oct 13 '22 09:10 vladmovchan

I updated and switched to the helpers. On HA restart though, Im not sure if the integration is triggering before the helpers restore. But in my situation, all the sensors for bodymiscale still show as unknown. BUT, without having to get on the scale, if i wait a bit and then hit reload, it does recalculate everything. What is the trigger mechanism that does the calculations?

cranky-coder avatar Oct 14 '22 20:10 cranky-coder

@cranky-coder, could you please check:

  • if helpers have proper values after HA restart
  • that Weight sensor and Impedance sensor in BodyMiScale configuration point to input number helpers (i.e. input_number.xxx), which preserve their state during HA restart. And not to sensors (i.e. sensor.xxx), which switch to Unknown state on HA restart.

vladmovchan avatar Oct 14 '22 20:10 vladmovchan

Confirmed. Not pointing to the sensors, but only pointing to the input_number helpers. both have valid values.

cranky-coder avatar Oct 14 '22 21:10 cranky-coder

Thank you. Then I'm out of ideas so far.

vladmovchan avatar Oct 14 '22 21:10 vladmovchan

What triggers the recalculations? both sensors weight and impedance or just one or the other?

cranky-coder avatar Oct 17 '22 14:10 cranky-coder

this can be solved in a simple way (when using BLE monitor for Homeassistant):

go to integrations, choose "configure" on the Bluetooth Low Energy Monitor integration, check if "Restore state" is activated, otherwise activate it. Now, after every restart of HA, the last State is shown

Maybe dckiller51 can put that hint into the readme.md

Just wanted to confirm that checking "restore state" in BLE instantly solved this for me (didn't even need a restart).

Gerto avatar Jan 07 '23 09:01 Gerto

I had the same issue when using ESPHome and the HA API. I noticed that commenting out api: and adding the mqtt: block did the trick (requires MQTT though). No other adjustments necessary. It now seems to survive both esp32 and ha reboots without loosing the calculated values.

Full example:

esphome:
  name: "xiaomi-mi-scale"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

mqtt:
  topic_prefix: bodymiscale
  discovery: true
  broker: 192.168.1.80
  port: 1883
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery_prefix: homeassistant

ota:
  password: "REPLACE_ME"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
    static_ip: 192.168.1.42
    gateway: 192.168.1.1
    subnet: 255.255.255.0 

esp32_ble_tracker:
  scan_parameters:
    # default is 320ms:
    interval: 160ms
    # default is 30ms:
    window: 60ms

xiaomi_ble:

sensor:
  - platform: xiaomi_miscale
    mac_address: '5C:CA:D3:78:A7:04'
    weight:
      name: "Xiaomi Mi Scale Weight"
      id: weight_miscale
      on_value:
        then:
          - lambda: |-
              if (id(weight_miscale).state >= 80 && id(weight_miscale).state <= 100) {
                return id(weight_user).publish_state(x);}
#              else if (id(weight_miscale).state >= 55 && id(weight_miscale).state <= 75) {
#                return id(weight_user2).publish_state(x);}

    impedance:
      name: "Xiaomi Mi Scale Impedance"
      id: impedance_miscale
      on_value:
        then:
          - lambda: |-
              if (id(weight_miscale).state >= 80 && id(weight_miscale).state <= 100) {
                return id(impedance_user).publish_state(x);}
#              else if (id(weight_miscale).state >= 55 && id(weight_miscale).state <= 75) {
#                return id(impedance_user2).publish_state(x);}

  - platform: template
    name: Weight User
    id: weight_user
    unit_of_measurement: 'kg'
    icon: mdi:weight-kilogram
    accuracy_decimals: 2
    
  - platform: template
    name: Impedance User
    id: impedance_user
    unit_of_measurement: 'ohm'
    icon: mdi:omega
    accuracy_decimals: 0
    
#  - platform: template
#    name: Weight User2
#    id: weight_user2
#    unit_of_measurement: 'kg'
#    icon: mdi:weight-kilogram
#    accuracy_decimals: 2
    
#  - platform: template
#    name: Impedance User2
#    id: impedance_user2
#    unit_of_measurement: 'ohm'
#    icon: mdi:omega
#    accuracy_decimals: 0

IDmedia avatar Jan 16 '23 17:01 IDmedia