jeroenterheerdt / ring-hassio

A Home Assistant add-on for live streaming from Ring devices.
MIT License
131 stars 93 forks source link

Battery Conservation #6

Open HarryJHolmes opened 4 years ago

HarryJHolmes commented 4 years ago

This add on is great by the way. I can access ring doorbell streams successfully. I am trying to conserve the battery of the doorbell and the connection doesn't seem to close when home assistant stops viewing the stream. Looking at the logs the stream numbers just keeps increasing. Is there anyway to stop streaming and reopen upon new connection.

Thanks in Advance @jeroenterheerdt

jeroenterheerdt commented 4 years ago

yep, I know that battery usage is an issue. I have not found a way to start/stop the stream when HA is looking at it or not. So maybe it would be better to run your Ring on wired power if you can? :)

HarryJHolmes commented 4 years ago

Yeah I have got it powered but obviously not sufficiently as it drained my doorbell 2 completely overnight. Is there no way to allow, for example, a 30 second stream and then wait for a new connection? @jeroenterheerdt

HarryJHolmes commented 4 years ago

@jeroenterheerdt The ring-homebrige integration doesn't drain battery so it must be possible?

jeroenterheerdt commented 4 years ago

the thing is that I have not found a way to activate the livestream on-demand.

HarryJHolmes commented 4 years ago

This looks useful https://github.com/dgreif/ring/issues/343#issuecomment-638583268 @jeroenterheerdt

jeroenterheerdt commented 4 years ago

Yeah it does, problem is that I don't know how HA can tell us that we need a livestream. Something like the UI saying the ring card is in view or out of view would be useful

HarryJHolmes commented 4 years ago

I see what you mean. Surely when you open the stream on HA that’s when it uses the url to get a stream. I’m not sure it constantly streams all the time.

jeroenterheerdt commented 4 years ago

right, but how do we know HA does not need the stream any longer...? I am not seeing anything that I can use for example on the frontend to query if the stream is in view. To be fair, I have not looked into it that much.

HarryJHolmes commented 4 years ago

Would HA reconnect if you ended the stream every 30seconds or so until new connection?

luci-11 commented 4 years ago

Hi guys, I don't know if it could be helpful but to save battery I did this (and it seems working) 1) created a sensor based on addon status (started/stopped) 2) created a switch to enable/disable the addon 3) modified code in "livestream.js", case 5 is now like this:

                case 5:
                    sipSession = _a.sent();
                    sipSession.onCallEnded.subscribe(function () {
                        console.log('Call has ended');
                        server.close(function () { console.log('Server closed!'); });
                        // Destroy all open sockets
                        for (var socketId in sockets) {
                            console.log('socket', socketId, 'destroyed');
                            sockets[socketId].destroy();
                        }
                        app.stop();
            fs.unlinkSync(path);
                        //console.log('Restarting server');
                        //startStream();

Removing the old file when stopping addon makes that at next start the video is "updated", I also noticed that even if I stopped addon, I was still getting streams opening and closing with increasing numbers at this page: http://hassio.local:port/public/stream.m3u8 The problem I have is that we have a delay of around 10 secs. Maybe this post can be helpful, maybe not :) (I don't know anything about js) Anyway this addon is great! Thank a lot for your work @jeroenterheerdt !

jeroenterheerdt commented 4 years ago

Hmm, starting the addon on demand is an interesting idea. You could make a PR and add the things you did in HA to the readme please? Just make a section about configuration, we need to educate people not to use this addon for 24 hour streaming because the party will be over soon..

luci-11 commented 4 years ago

PR opened for the Readme. For the js part please give it a try as I don't know if all sockets and connections are closed properly (I don't know how to check)

jeroenterheerdt commented 4 years ago

@luci-11 - thanks - I am not entirely clear on what you changed, hence I asked for a PR so I can compare what you did.

jeroenterheerdt commented 4 years ago

I am not sure what is the difference between yours and mine config, but I cannot make this REST sensor work. I am using NGINX for extra security that I think is blocking this. The thought is interesting though, we should be able to make this work. Isn't there another way to get the status of an add-on?

luci-11 commented 4 years ago

Hi, I'm using nginx too. I use the addon by Frenck: Nginx Proxy Manager I tested this 3 methods: With local ip:

sensor:
  - platform: rest
    resource: "http://hassio_ip:8123/api/hassio/addons/xxxxxxxx_ringlivestream/info"
    headers:
      Authorization: "Bearer [Long_lived_token]"
      Content-Type: application/json
    name: ring_addon_official_online
    value_template: "{{value_json['data']['state']}}"

With domain:

sensor:
  - platform: rest
    resource: "https://yoursite.yourdomain.org/api/hassio/addons/xxxxxxxx_ringlivestream/info"
    headers:
      Authorization: "Bearer [Long_lived_token]"
      Content-Type: application/json
    name: ring_addon_official_online
    value_template: "{{value_json['data']['state']}}"

Or you can try the method below. It takes a couple of seconds to update after reboot and the sensor should be updated manually after start/stop command to the addon (so the switch is replaced by two scripts) :

sensor:
  - platform: command_line
    name: Supervisor_2
    command: 'curl https://yoursite.yourdomain.org/api/hassio/supervisor/info -H "Authorization: Bearer [Long_lived_token]" | jq ''{"state":.data.state,"slug":.data.slug,"addons":[.data.addons[] | select(.slug == "XXXXXXXX_ringlivestream")]}'''
    value_template: "{{ value_json.addons | length }}"
    json_attributes:
      - addons

  - platform: template
    sensors:
      ring_addon_status_from_command_line:
        friendly_name: "Ring"
        value_template: >-
          {{ state_attr('sensor.supervisor_2', 'addons')|regex_findall_index(".state'...(.+)', 'version'") }}

script:
  ring_start:
    sequence:
      - service: hassio.addon_start
        data:
          addon: XXXXXXXX_ringlivestream
      - service: shell_command.check_status
  ring_stop:
    sequence:
      - service: hassio.addon_stop
        data:
          addon: XXXXXXXX_ringlivestream
      - service: shell_command.check_status

shell_command:
  check_status: 'curl https://yoursite.yourdomain.org/api/hassio/supervisor/info -H "Authorization: Bearer [Long_Lived_Token]"'

This sensor is updated slower than the two above.

I'll do the PR for the .js part in some minutes. I wanted also to tell you that in my case the stream works also without opening the port on router.

jeroenterheerdt commented 4 years ago

I used the NGINX Home Assistant TSL Proxy, will check with the proxy manager to see if that changes anything. thanks.

jeroenterheerdt commented 4 years ago

well, that was quick - the Proxy Manager does not even start, no logging, nothing. Hmpf.

EDIT - nevermind - had to stop the other NGINX thing first. duh.

Ok that did not work. Back to the other add-on. Not sure what went wrong and I don't know enough of nginx to go configure my own proxy settings. Also, if we cannot make this work with the other add-on why bother in the first place?

And now the component does not work at all any more - no errors, but the livestream does not work, not with the port (3000) open in the router, not without... what the ....

@luci-11 I think we have a lot of issues with many different configurations being out there... multiple different options, ports, proxies, etc.

angadsingh commented 3 years ago

@jeroenterheerdt great work man! please make this more stable and add on-demand starting/stopping of the RTSP server based on access. I created the template switch and sensor as suggested here and went ahead and created a script as well which will start the addon, cast the camera to my nest hub display, wait 1.5 minutes, stop the cast, and then finally stop the addon:

cast_ring_doorbell_to_nest_display:
  alias: Cast ring doorbell to nest display
  sequence:
  - data: {}
    entity_id: switch.ring_live_stream_addon_switch
    service: switch.turn_on
  - condition: state
    entity_id: switch.ring_live_stream_addon_switch
    state: 'on'
  - data:
      media_player: media_player.living_room_display
    entity_id: camera.ring_livestream
    service: camera.play_stream
  - delay: 00:01:30
  - data: {}
    entity_id: media_player.living_room_display
    service: media_player.media_stop
  - data: {}
    entity_id: switch.ring_live_stream_addon_switch
    service: switch.turn_off

however this was a big failure. it would take forever for the addon to achieve the on state, and then it would take forever for the cast to start streaming after initially showing up. if the addon itself killed its upstream link to the camera for battery preservation that would be much much faster. here's how the battery dies when using this addon: B8AC1CC5-73CD-423D-9BF9-805617E98B31

notice the times when the addon was running its almost like a flat line. drains maybe 2% per day. with the addon running its almost 1% every 5 minutes. the only way to use this addon in its current state would be to have a wired power connection to the doorbell.

angadsingh commented 3 years ago

@jeroenterheerdt you would not need a special callback or hook from HA to tell you that it is starting the stream. all it ultimately does is hit your stream URL, so maybe have either a nginx lua script or the rtsp server itself start the upstream fetch only when a hit comes to the url

jeroenterheerdt commented 3 years ago

right, well, my node skills are next to none, so I really cannot make this work. feel free to take this code and take it further.

bkr1969 commented 2 years ago

resource: "https://yoursite.yourdomain.org/api/hassio/addons/xxxxxxxx_ringlivestream/info"

Where do you get this URL? resource: "http://hassio_ip:8123/api/hassio/addons/xxxxxxxx_ringlivestream/info"