Smart charger

I just wanted to share my “smart charger”.
It uses the Home Assistant companion app to know what device is charging on this plug and stops the charge at 90% (ish).
It is built in Node red and has a Lovelace picture elements card to display the device and the current battery level.
When you charge something that does not have the companion app installed and the flow can’t determine what is being charged then it will leave the plug on for two hours and display the countdown in Lovelace.

All you need is a smart plug and a USB charger connected to it, and the Home Assistant companion app installed on the devices you want to charge.

smart charger on
smart charger on dumb

Even though the sequence in Node red is very large, I have used change nodes and function nodes to make sure the entities should only be declared once and then they are passed along with the messages.
That way it is not that many nodes that needs to be changed.

All nodes that needs to be changed is commented in the flow with a comment node and in some nodes there is a description.

yaml

configuration.yaml

input_boolean:
  ha_two_min_after_restart: # this is to start up the sequence again if it was on when you rebooted HA
    name: turns on two minutes after restart
    initial: false

  charger_vardagsrum_GUI: # This boolean tells Lovelace to hide/display the view
    name: charger_vardagsrum_GUI

  charger_vardagsrum_known: # this determins if the device has companion app installed and displays the name and battery level or time left
    name: charger_vardagsrum_GUI


input_text:
  charger_vardagsrum_device: # the friendly name of the device that is charged
    name: charger_vardagsrum_device
    icon: mdi:cellphone-charging

  charger_vardagsrum_header: # what is displayed as header in the picture element card
    name: charger_vardagsrum_header
    icon: mdi:usb-port
    initial: "Smart laddare vardagsrum"
  
  charger_vardagsrum_percent: # battery level of the device that is charged
    name: charger_vardagsrum_percent

  charger_vardagsrum_time_left: # time left if the device on charge does not have home assistant app installed
    name: charger_vardagsrum_time_left
    icon: mdi:clock-time-eight-outline

sensor:
  - platform: uptime # uptime sensor that is used to trigger the automation
    unit_of_measurement: minutes

customize.yaml

customize:
  input_text.charger_vardagsrum_percent:
    unit_of_measurement: '%'
    
  input_text.charger_vardagsrum_header:
    templates:
                               # change switch to what you use
      icon_color: if (entities['switch.32431260c44f33bed286'].state == 'off') return 'rgb(255,0,0)';
                  if (entities['switch.32431260c44f33bed286'].state == 'on') return 'rgb(0,255,0)';

automations.yaml

- id: '1597354226140'
  alias: After restart set boolean to on
  description: ''
  trigger:
  - above: '2'
    entity_id: sensor.uptime
    platform: numeric_state
  condition:
  - condition: state
    entity_id: input_boolean.ha_two_min_after_restart
    state: 'off'
  action:
  - data: {}
    entity_id: input_boolean.ha_two_min_after_restart
    service: input_boolean.turn_on
  mode: single

Lovelace:

elements:
  - entity: input_text.charger_vardagsrum_header
    style:
      left: 10%
      top: 17%
      transform: 'scale(2,2)'
    tap_action:
      action: call-service
      service: switch.toggle
      service_data:
        entity_id: switch.32431260c44f33bed286
    type: state-icon
  - entity: input_text.charger_vardagsrum_header
    style:
      left: 30%
      top: 17%
      transform: 'scale(1.5,1.7)'
    tap_action:
      action: call-service
      service: switch.toggle
      service_data:
        entity_id: switch.32431260c44f33bed286
    type: state-label
  - conditions:
      - entity: input_boolean.charger_vardagsrum_gui
        state: 'on'
    elements:
      - icon: 'mdi:tablet-cellphone'
        style:
          left: 15%
          top: 43%
          transform: 'scale(2,2)'
        type: icon
      - entity: input_text.charger_vardagsrum_device
        style:
          left: 30%
          top: 42%
          transform: 'scale(1.5,1.5)'
        type: state-label
      - conditions:
          - entity: input_boolean.charger_vardagsrum_known
            state: 'on'
        elements:
          - icon: 'mdi:cellphone-charging'
            style:
              left: 14%
              top: 67%
              transform: 'scale(2,2)'
            type: icon
          - entity: input_text.charger_vardagsrum_percent
            style:
              left: 27%
              top: 65%
              transform: 'scale(1.5,1.5)'
            type: state-label
        type: conditional
      - conditions:
          - entity: input_boolean.charger_vardagsrum_known
            state: 'off'
        elements:
          - icon: 'mdi:clock-time-eight-outline'
            style:
              left: 15%
              top: 67%
              transform: 'scale(2,2)'
            type: icon
          - entity: input_text.charger_vardagsrum_time_left
            style:
              left: 30%
              top: 65%
              transform: 'scale(1.5,1.5)'
            type: state-label
        type: conditional
    type: conditional
image: local/frame.png
style: null
tap_action:
  action: none
type: picture-elements

Resources:

Frame:
http://hoppvader.nu/bilder/frame.png

Sound file:
http://hoppvader.nu/bilder/Magic_Wand_Noise.mp3

Couldn’t add the Node red here since there is a limit of characters.
It’s in this file:
http://hoppvader.nu/bilder/Node_red.txt

That’s a good project, but I’m using macrodroid (android automation) to send http requests to turn on/off or schedule etc. to control switches. It looks independent without help of HA.

That is also a way of doing it.
I thought (not much so I might have missed something) about it but that would mean I had to do the “programming” on the phone(s).
And that means the phone has to figure out what wall plug it’s connected too. Meaning it would have to cycle the power on different plugs, which will possibly turn off the charge on one phone and then on again, and that phone will try to figure out what plug that it’s connected to and turn off the power to another device… and all of the sudden this whole mess is complete where nothing is charging and all the plugs are just turning “them selves” on and off all the time.

And that also means if you connect a mp3 player or anything else the charger becomes “dumb”.
Sure a two hour timer is not that great but at least I can customize that with adding or subtracting time.
Which I will probably do… Just add a plus and minus sign on Lovelace and be able to control the time.

By having the plug (HA) in control I believe I can make it smarter and easier to duplicate to more devices and plugs. But that is more or less a guess since I just thought about the scenario above and kind of scraped the idea with phone being in charge of the charging.

Yeah, that is also true. All have different ways of usage on daily life, I’m just using my phone on a particular socket and it’s jus me using it… :stuck_out_tongue_winking_eye: