andrew-codechimp / HA-Battery-Notes

A Home Assistant integration to provide battery notes of devices
MIT License
445 stars 83 forks source link

Error with "Battery Replaced" Community Automation #1750

Closed sockmonkey0223 closed 3 weeks ago

sockmonkey0223 commented 3 weeks ago

System Health details

System Information

version core-2024.6.1
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.12.2
os_name Linux
os_version 6.6.29-haos
arch x86_64
timezone America/New_York
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok GitHub API Calls Remaining | 5000 Installed Version | 1.34.0 Stage | running Available Repositories | 1470 Downloaded Repositories | 112 HACS Data | ok
AccuWeather can_reach_server | ok -- | -- remaining_requests | 19
Home Assistant Cloud logged_in | true -- | -- subscription_expiration | July 6, 2024 at 8:00 PM relayer_connected | true relayer_region | us-east-1 remote_enabled | true remote_connected | true alexa_enabled | true google_enabled | false remote_server | us-east-1-1.ui.nabu.casa certificate_status | ready instance_id | can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Home Assistant OS 12.3 -- | -- update_channel | stable supervisor_version | supervisor-2024.06.0 agent_version | 1.6.0 docker_version | 25.0.5 disk_total | 30.8 GB disk_used | 11.7 GB healthy | true supported | true host_connectivity | true supervisor_connectivity | true ntp_synchronized | true virtualization | kvm board | ova supervisor_api | ok version_api | ok installed_addons | Samba share (12.3.1), Mosquitto broker (6.4.1), Let's Encrypt (5.0.18), TasmoAdmin (0.30.3), AppDaemon (0.16.6), chrony (3.0.1), Advanced SSH & Web Terminal (18.0.0), Samba Backup (5.2.0), ESPHome (2024.5.5), Z-Wave JS (0.5.0), Whisper (2.1.0), Piper (1.5.0), openWakeWord (1.10.0), Matter Server (6.1.1), Tesla HTTP Proxy (2.2.7)
Dashboards dashboards | 8 -- | -- resources | 61 views | 34 mode | storage
Recorder oldest_recorder_run | March 13, 2024 at 10:52 PM -- | -- current_recorder_run | June 8, 2024 at 12:47 PM estimated_db_size | 15427.38 MiB database_engine | mysql database_version | 10.11.7

Checklist

Describe the issue

I set up the Community "Battery Replaced" automation. When I replaced a battery today (0% --> 100%) the automation triggered but failed with an error:

Error: string value is None for dictionary value @ data['source_entity_id']
Result:
params:
  domain: battery_notes
  service: set_battery_replaced
  service_data:
    device_id: 137380e2ebf12df13f892a64ba50fc7d
    source_entity_id: null
  target: {}
running_script: false

Full automation YAML:

alias: "Action: Set Battery Replaced"
description: ""
trigger:
  - platform: event
    event_type: battery_notes_battery_increased
    alias: Battery Notes "Battery Increased" Event is Fired
condition: []
action:
  - service: battery_notes.set_battery_replaced
    data:
      device_id: "{{ trigger.event.data.device_id }}"
      source_entity_id: "{{ trigger.event.data.source_entity_id }}"
    alias: Call "Set Battery Replaced" Service
  - service: persistent_notification.create
    data:
      title: |
        {{ trigger.event.data.device_name }} Battery Replaced
      message: |
        Battery level increased past threshold and has been marked as replaced.
    alias: Create Persistent Notification that Battery Replaced
mode: queued

configuration.yaml:

battery_notes:
  default_battery_low_threshold: 10
  battery_increase_threshold: 40
  hide_battery: true

Reproduction steps

  1. Replace low battery with new battery
  2. Check automation traces
  3. Observe error

Debug logs

Not enabled at time of battery replacement.

Diagnostics dump

Cannot download, diagnostics error:

Logger: aiohttp.server
Source: /usr/local/lib/python3.12/site-packages/aiohttp/web_protocol.py:421
First occurred: 8:42:21 AM (5 occurrences)
Last logged: 8:57:10 AM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/aiohttp/web_protocol.py", line 452, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/web_app.py", line 543, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/web_middlewares.py", line 114, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 92, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 83, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 26, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 85, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 242, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 32, in headers_middleware
    response = await handler(request)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/http.py", line 73, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/diagnostics/__init__.py", line 280, in get
    data = await info.config_entry_diagnostics(hass, config_entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: async_get_config_entry_diagnostics() takes 1 positional argument but 2 were given
andrew-codechimp commented 3 weeks ago

I've been able to replicate this, rather than try and kludge a change to the automation I'll fix this as a new release on the battery replaced service itself.

andrew-codechimp commented 3 weeks ago

This is now fixed in the 2.3.6 release, thanks for reporting it.

sambarlick commented 1 week ago

Hi,

I realise this issue is closed, but I am experiencing this same issue, the source_entity_id is empty.

I am running Battery Notes v2.3.9 HA: 2024.6.4

I have attached the log from my automation. I have tested the event data on multiple devices all with the same issue.

If I can provide any more information please let me know.

This is the event data:

event_type: battery_notes_battery_increased data: device_id: f86a43d6399a8335de1e51a0d1e0992b source_entity_id: "" device_name: Bedroom Window battery_low: false battery_type_and_quantity: Rechargeable battery_type: Rechargeable battery_quantity: 1 battery_level: 100 previous_battery_level: 10 origin: LOCAL time_fired: "2024-06-25T09:59:26.093334+00:00" context: id: 01J17DC04D44FYRSPCKY6DMPVA parent_id: null user_id: null

Home Assistant 2024-06-25 19_59_50.json

sambarlick commented 1 week ago

source_entity_id is also empty in the event battery_notes_battery_threshold.

event_type: battery_notes_battery_threshold data: device_id: f86a43d6399a8335de1e51a0d1e0992b source_entity_id: "" device_name: Bedroom Window battery_low: true battery_type_and_quantity: Rechargeable battery_type: Rechargeable battery_quantity: 1 battery_level: 5 previous_battery_level: 100 reminder: false origin: LOCAL time_fired: "2024-06-25T10:25:50.137025+00:00" context: id: 01J17EWB1S6S6Q6V5X008KKCXB parent_id: null user_id: null

andrew-codechimp commented 1 week ago

Hi @sambarlick it looks like this is a different issue. source_entity_id can be "", it was previously NULL and that caused the original issue.

I've just found one area of the documentation where the parameter was wrongly named, assuming you have your own automation the action should look like this, I can see from the error it's trying to call the service with entity_id instead of source_entity_id.

action:
  - service: battery_notes.set_battery_replaced
    data:
      device_id: "{{ trigger.event.data.device_id }}"
      source_entity_id: "{{ trigger.event.data.source_entity_id }}"