jcgoette / baby_buddy_homeassistant

This custom integration provides sensors for Baby Buddy API endpoints.
MIT License
64 stars 30 forks source link

Timer getting Bad Request: /api/timers/ #82

Closed DieterKoblenz closed 1 year ago

DieterKoblenz commented 1 year ago
service: babybuddy.start_timer
data:
  name: Sleep
target:
  entity_id: switch.test_123_timer

While I guess this should work, I'm getting a bad request.

Using baby buddy v1.13.2 Using integration v2.6.1

Getting this error: Could not create timers. error: {'start': ['Date/time can not be in the future.']}. Please upgrade to babybuddy v1.11.0. In the meantime, attempting to use 'now()'...

jcgoette commented 1 year ago

What version of BB are you on? Does it actually create the timer?

DieterKoblenz commented 1 year ago

I've just updated my post, I'm on 1.13.2 and it doesn't create a timer.

jcgoette commented 1 year ago

Did this used to work? If so, when did it stop?

DieterKoblenz commented 1 year ago

I've just installed it since a couple of days, and working on a touchscreen for BB. So I haven't used it before.

jcgoette commented 1 year ago

Check that timezones of HA and BB are the same.

DieterKoblenz commented 1 year ago

HA: Timezone GMT +1 (EU/Amsterdam) BB: Also Eu/Amsterdam

So should be the same.

jcgoette commented 1 year ago

Hmm. That's odd – since you're not passing in time from HA, BB itself should default to now().

Recommend upping logging to DEBUG and posting what you get.

Also, if you are able/comfortable, would try changing timezone.now to timezone.localtime here.

DieterKoblenz commented 1 year ago

Strange, I've set DEBUG to true but doesn't seem to do more.

Fyi, I am using docker. Could that be a problem?

jcgoette commented 1 year ago

DEBUG true where? Sounds like you mean BB.

Docker for what?

Sounds like there might be some confusion between the two different apps.

DieterKoblenz commented 1 year ago

I've set DEBUG as a ENV on the docker container running BB.

jcgoette commented 1 year ago

Yea, I mean DEBUG level in HA.

DieterKoblenz commented 1 year ago

Even with debug log I just get this response in the log:

Deze fout is ontstaan door een aangepaste integratie.

Logger: custom_components.babybuddy.client Source: custom_components/babybuddy/client.py:74 Integration: Baby Buddy (documentation, issues) First occurred: 11:57:30 (1 occurrences) Last logged: 11:57:30

Could not create timers. error: {'start': ['Date/time can not be in the future.']}. Please upgrade to babybuddy v1.11.0. In the meantime, attempting to use 'now()'...

DieterKoblenz commented 1 year ago

I'll try with default debug logging...

DieterKoblenz commented 1 year ago

2022-11-06 12:02:21.107 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [140427783011728] Received {'type': 'execute_script', 'sequence': [{'service': 'babybuddy.start_timer', 'data': {}, 'target': {'entity_id': 'switch.test_123_timer'}}], 'id': 52} 2022-11-06 12:02:21.110 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=babybuddy, service=start_timer, service_data=entity_id=['switch.test_123_timer']> 2022-11-06 12:02:21.110 DEBUG (MainThread) [custom_components.babybuddy.client] POST data: {'child': 1, 'start': datetime.datetime(2022, 11, 6, 12, 2, 21, 110757, tzinfo=zoneinfo.ZoneInfo(key='Europe/Amsterdam'))} 2022-11-06 12:02:21.135 ERROR (MainThread) [custom_components.babybuddy.client] Could not create timers. error: {'start': ['Date/time can not be in the future.']}. Please upgrade to babybuddy v1.11.0. In the meantime, attempting to use 'now()'... 2022-11-06 12:02:21.137 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/children/ 2022-11-06 12:02:21.153 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":1,"next":null,"previous":null,"results":[{"id":1,"first_name":"Test","last_name":"123","birth_date":"2022-10-30","slug":"test-123","picture":null}]} 2022-11-06 12:02:21.153 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/bmi/?child=1&limit=1 2022-11-06 12:02:21.170 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":0,"next":null,"previous":null,"results":[]} 2022-11-06 12:02:21.170 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/changes/?child=1&limit=1 2022-11-06 12:02:21.266 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":14,"next":"http://192.168.1.3:4321/api/changes/?child=1&limit=1&offset=1","previous":null,"results":[{"id":21,"child":1,"time":"2022-11-04T17:01:06.196000+01:00","wet":true,"solid":false,"color":"","amount":null,"notes":"openHasp","tags":[]}]} 2022-11-06 12:02:21.266 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/feedings/?child=1&limit=1 2022-11-06 12:02:21.362 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":1,"next":null,"previous":null,"results":[{"id":20,"child":1,"start":"2022-11-04T19:09:00+01:00","end":"2022-11-04T19:09:00+01:00","duration":"00:00:00","type":"formula","method":"bottle","amount":10.0,"notes":"Via openhasp","tags":[]}]} 2022-11-06 12:02:21.362 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/head-circumference/?child=1&limit=1 2022-11-06 12:02:21.454 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":0,"next":null,"previous":null,"results":[]} 2022-11-06 12:02:21.455 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/height/?child=1&limit=1 2022-11-06 12:02:21.474 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":1,"next":null,"previous":null,"results":[{"id":1,"child":1,"height":60.0,"date":"2022-10-30","notes":"","tags":[]}]} 2022-11-06 12:02:21.475 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/notes/?child=1&limit=1 2022-11-06 12:02:21.559 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":0,"next":null,"previous":null,"results":[]} 2022-11-06 12:02:21.560 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/pumping/?child=1&limit=1 2022-11-06 12:02:21.648 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":0,"next":null,"previous":null,"results":[]} 2022-11-06 12:02:21.648 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/sleep/?child=1&limit=1 2022-11-06 12:02:21.671 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":1,"next":null,"previous":null,"results":[{"id":1,"child":1,"start":"2022-11-04T15:25:00+01:00","end":"2022-11-04T15:30:39.572815+01:00","duration":"00:05:39.572815","nap":true,"notes":"Test","tags":[]}]} 2022-11-06 12:02:21.671 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/temperature/?child=1&limit=1 2022-11-06 12:02:21.762 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":0,"next":null,"previous":null,"results":[]} 2022-11-06 12:02:21.763 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/timers/?child=1&limit=1 2022-11-06 12:02:21.860 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":3,"next":"http://192.168.1.3:4321/api/timers/?child=1&limit=1&offset=1","previous":null,"results":[{"id":6,"child":1,"name":"Sleep Timer","start":"2022-11-04T15:25:00+01:00","end":"2022-11-04T15:30:39.572815+01:00","duration":"00:05:39.572815","active":false,"user":1}]} 2022-11-06 12:02:21.860 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/tummy-times/?child=1&limit=1 2022-11-06 12:02:21.954 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":0,"next":null,"previous":null,"results":[]} 2022-11-06 12:02:21.954 DEBUG (MainThread) [custom_components.babybuddy.client] GET URL: http://192.168.1.3:4321/api/weight/?child=1&limit=1 2022-11-06 12:02:22.050 DEBUG (MainThread) [custom_components.babybuddy.client] GET response: {"count":3,"next":"http://192.168.1.3:4321/api/weight/?child=1&limit=1&offset=1","previous":null,"results":[{"id":1,"child":1,"weight":10.0,"date":"2022-10-30","notes":"","tags":[]}]} 2022-11-06 12:02:22.050 DEBUG (MainThread) [custom_components.babybuddy] Finished fetching babybuddy data in 0.913 seconds (success: True)

jcgoette commented 1 year ago

Does creating a Temperature entry without a given time work? Or does it throw an error too?

DieterKoblenz commented 1 year ago

Temperature does work.

jcgoette commented 1 year ago

if you are able/comfortable, would try changing timezone.now to timezone.localtime here.

DieterKoblenz commented 1 year ago

I've changed the line in models.py, restarted BB and re-ran the service in HA. However, still get the same error, unfortunately.

I am away for a couple of days. I'll see if a clean BB install with this component re-installed will work.

jcgoette commented 1 year ago

Played around with BB tonight and couldn't find any smoking guns.

Are you using docker or docker-compose for BB? If so, will you post the docker run command or the docker-compose.yaml you are using? Also, your BB user settings. Wanting to see how/where you are setting TZ.

DieterKoblenz commented 1 year ago

I'm using docker on a QNAP NAS, therefor I don't have a run command to show you.

I've set the TZ ENV to "Europe/Amsterdam", this is the same as the timezone setting in user settings in BB.

If I go into the BB terminal it uses CET (so the correct TZ)

There is no /etc/localtime, shouldn't there be?

DieterKoblenz commented 1 year ago

I've removed the container, and tried a new one with docker compose.

`version: "2.1" services: babybuddy: image: lscr.io/linuxserver/babybuddy container_name: babybuddy2 environment:

Same old.

jcgoette commented 1 year ago

Try setting TIME_ZONE instead of TZ:

https://github.com/babybuddy/babybuddy/blob/50ffd2775b6ef2abcaf5733673334d5c9380c1b4/babybuddy/settings/base.py#L161

DieterKoblenz commented 1 year ago

Done, no change.

jcgoette commented 1 year ago

At this point, try POSTing directly to the BB API. If that fails, I think we can close this issue and you can open an issue with the BB repo directly.

DieterKoblenz commented 1 year ago

Ok, I'm closing this then. I'm getting a weird error on the BB API. When I post.

DieterKoblenz commented 1 year ago

Actually, I POSTed to /api/timers and not /api/timers/.

With /api/timers/ I get the expected result.

DieterKoblenz commented 1 year ago

I've tried again from the HA component:

2022-11-15 15:24:59.218 DEBUG (MainThread) [custom_components.babybuddy.client] POST data: {'child': 1, 'start': datetime.datetime(2022, 11, 15, 15, 24, 59, 218396, tzinfo=zoneinfo.ZoneInfo(key='Europe/Amsterdam')), 'name': 'Slapen'} 2022-11-15 15:24:59.254 ERROR (MainThread) [custom_components.babybuddy.client] Could not create timers. error: {'start': ['Date/time can not be in the future.']}. Please upgrade to babybuddy v1.11.0. In the meantime, attempting to use 'now()'...

kguy18 commented 1 year ago

I am getting the exact same error with the same setup. I am running BB in docker with docker-compose and HA running on a VM on the same machine. My error logs look identical. That said when I go to HA Developer Tools > Service and call the baby buddy timer function I can get it to start a timer. I cannot get it to trigger via automation though? Any thoughts?

Okay just tried again and now I cannot get it to work to start a timer, updated to Home Assistant latest (whoops)

On a Virtual Box Machine: Home Assistant 2023.2.1 Supervisor 2023.01.1 Operating System 9.4 Frontend 20230110.0 - latest

On Docker Baby Buddy v1.13.2 Integration v2.6.6

Error I receive in the logs:

This error originated from a custom integration.

Logger: custom_components.babybuddy.client
Source: custom_components/babybuddy/client.py:75 
Integration: Baby Buddy (documentation, issues) 
First occurred: 6:32:27 AM (5 occurrences) 
Last logged: 2:20:19 PM

Could not create timers. error: {'start': ['Date/time can not be in the future.']}. Please upgrade to babybuddy v1.11.0. In the meantime, attempting to use 'now()'... 
kguy18 commented 1 year ago

I did some additional investigating, I think it has to do with Home Assistant on a VM and time drift? What is really weird is I can pass the following into an automation, hit the refresh automations in the developer tools, and call the automation and it will successfully start a timer - once. If I try to do it again, it will not start the timer and throws the above error. I am not sure how else to trouble shoot. Looking at the trace it is showing the correct time, but it does not show milliseconds and I think that is where the drift is happening? I am not sure how to offset by -1 minute, but maybe that will help?

alias: "Baby Buddy Logger: Sleep Start Blue Button"
trigger:
  - platform: event
    event_type: esphome.bblogger.keypad_blue
    event_data:
      title: single_click
action:
  - service: babybuddy.start_timer
    data:
      name: Sleep
      start: "{{ as_timestamp(now())|timestamp_custom('%Y-%m-%dT%H:%M:%SZ',true) }}"
    target:
      entity_id: switch.baby_name_timer
      device_id: device_ID
kguy18 commented 1 year ago

Okay, I have it working, I was able to figure out an offset (minus 1 minute), it probably is not the best option but it works - add this to your automations and the timer will start.

  start: "{{ (now().strftime("%s") | int - (1*60)) | timestamp_custom('%Y-%m-%dT%H:%M:%SZ',false) }}"
jcgoette commented 1 year ago

Anyone having this issue, could you:

  1. Check the timezone key from www.yourbabybuddy.com/api/profile
  2. Check the timezone with {{ now().tzinfo }} in HA template
DieterKoblenz commented 1 year ago

Both are Europe/Amsterdam.

jcgoette commented 1 year ago

👋 Thanks @DieterKoblenz , no luck there then. Curious, are you still having the issue? Or did you find a work around? Are you using a VM like @kguy18 ?

DieterKoblenz commented 1 year ago

I am still having the issue, at the moment using the workaround mentioned above.

I run BB in Docker on a separate machine as the HA VM.

jcgoette commented 1 year ago

Ok, so now I feel good that this isn't an issue with this integration.

We could probably build in some automatically adjusting offset, but I don't think that's the ideal solution.

I did see some potentially useful comments regarding this issue on SO:

I am going to close this issue for now. Thanks for everyone's effort and time troubleshooting.