Tasker & Long Lived Access Tokens

Whereas the api_password can be placed in the URL (like in your example), the Long Lived Access Token goes in the Request Header.

Authorization: Bearer ey62s9A64etcetcetcblablabla

Example:
The goal is to make an Android phone report its battery level and power status to Home Assistant via an HTTP Sensor and an HTTP Binary Sensor, respectively.

MacroDroid is an Android automation app like Tasker. It only lets you do an HTTP GET (not HTTP POST) and doesn’t allow you to define a Request Header. Therefore you can’t use it with the token-based authorization system. However, it supports Tasker plugins like HTTP Request Shortcuts which does support HTTP POST and custom Request Headers. Using the combination of MacroDroid with the HTTP Shortcuts plugin, I made it login to Home Assistant with an access token and report battery level and power status.

Another automation app is Automate. It natively supports HTTP POST and custom Request Headers. Therefore, without employing any plugins, I made it login to Home Assistant with a token and report battery and power.

If whatever you’re using doesn’t let you define a Request Header, to hold the token, then you’ve got a problem.


EDIT

Here’s an example using curl. You’ll notice it performs an HTTP POST and contains two Request Headers, one has the token and the other indicates the body is in JSON format.

curl -X POST -H "Authorization: Bearer joxODUTMjQ0NjYA5fQ.3J2sSvlNGB87MgI9AkBvJQtm1ng" \
    -H "Content-Type: application/json" \
    -d '{"state": "off"}' \
    http://YourServerName:8123/api/states/binary_sensor.phone_power

@123 thank you the http request shortcuts work with you example

thanks to all for the help so i tried Tutorial: Using the new Auth system with tasker and it works with lights or switches
put wen i try to send my tablet’s battery info i dont get anything here my config in tasker
PARAMETER 1
/api/services/states/sensor.test
PARAMETER 2
{"state":"%BATT";atributes":{"friendly_name":"Tab s 2 battry";"icon":mdi:android","unit_of_mesurement":"%"}

A few typos in your Parameter 2. This should work:

{"state": "%BATT", attributes": {"friendly_name": "Tab s 2 battery"; "icon": "mdi:android", "unit_of_measurement": "%" }}

I go it to work thanks to @phantomdarkness and @123 for your time and help

Hi @123

I’m trying to use Macrodroid with HA but with no success.

Can you give me some help?

What I would like to do with Macrodroid is to turn on and off a relay defined in HA when my wall mount laptop will reach certain % of battery, >= 90% will switch off the relay, when <= 15% switch on the relay.

I have the relay switch defined in HA and I have to create a sensor to have the tablet update the battery percentage and have an automation do the job.

Any suggestion?

Thanks

Before we dive into that, do you currently use MQTT and Node-Red?

If you do, then I can show you how I use them, with Macrodroid, to send battery status to HA.

If not, then the approach will be precisely how I described it in my previous post.

Yes, I use MQTT and NodeRed!

My solution relies on MacroDroid’s ability to transmit data via UDP. The UDP packet is sent to my existing Node-Red server where a flow converts it to MQTT. The process looks like this:

[MacroDroid] --(UDP)--> [Node-Red] --(MQTT)--> [MQTT Broker] --(MQTT)--> [Home-Assistant]

Two macros are needed, one for handling changes to the battery level and another for power connection status (so you know if the device is connected to AC or not). The macros format their measurements into JSON strings and transmit them, via UDP, to the Node-Red server on port 5010.

A Node-Red flow listens to UDP 5010 and converts the incoming JSON strings into MQTT topics. It publishes two topics:
phone/battery
phone/power

In Home Assistant, you need an MQTT Binary Sensor, for receiving the power topic, and an MQTT Sensor for the battery topic.

In binary_sensors.yaml:

  - platform: mqtt
    name: "Phone Power"
    state_topic: "phone/power"
    device_class: connectivity

In sensors.yaml:

  - platform: mqtt
    name: "Phone Battery"
    state_topic: "phone/battery"
    unit_of_measurement: "%"
    device_class: battery
    expire_after: 1200

Here’s an overview of the two macros I created. You’ll have to create them on your tablet.

Battery Level

  • Trigger: When there’s any change in battery level.
  • Action: Send the battery level as a JSON string via UDP to my Node-Red server on port 5010.
  • Constraint: Do this only if the phone is connected to my home’s WiFi network.

Power Status

  • Trigger: When there’s any change in power-connection status.
  • Action: Send the power status as a JSON string via UDP to my Node-Red server on port 5010.
  • Constraint: Do this only if the phone is connected to my home’s WiFi network.

I chose to have the JSON string contain both the power status and battery level. The items enclosed by square brackets are how you define MacroDroid variables.

"{"power": "[power]", "battery": [battery]}"

Upon evaluation, the JSON string can look like this (phone is connected to a power source and battery is at 78%):

{"power": "on", "battery": 78}

The Node-Red flow consists of four nodes:

  1. UDP Input node - Receives UDP packets on port 5010.
  2. JSON node - Converts the received JSON string into an object.
  3. Function node - Converts the JSON object into MQTT topics (phone/power and phone/battery).
  4. MQTT Output node: Publishes MQTT topics.

Here’s the Node-red flow:

[
    {
        "id": "c399e80c.845e88",
        "type": "udp in",
        "z": "c08de27a.3035c",
        "name": "",
        "iface": "",
        "port": "5010",
        "ipv": "udp4",
        "multicast": "false",
        "group": "",
        "datatype": "utf8",
        "x": 120,
        "y": 100,
        "wires": [
            [
                "4714cd20.c8b364"
            ]
        ]
    },
    {
        "id": "4714cd20.c8b364",
        "type": "json",
        "z": "c08de27a.3035c",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 290,
        "y": 100,
        "wires": [
            [
                "d72d6678.0b3888"
            ]
        ]
    },
    {
        "id": "d72d6678.0b3888",
        "type": "function",
        "z": "c08de27a.3035c",
        "name": "MQTT Converter",
        "func": "if ((msg.payload.battery !== undefined) && (msg.payload.power !== undefined)) {\n    var MsgList = [];\n    var path = \"phone/\";\n    msg.payload.power = msg.payload.power.toUpperCase();\n    \n    for (var key in msg.payload) {\n        MsgList.push({topic:path + key, payload:msg.payload[key], qos:0, retain:true});\n    }\n    return [MsgList];\n}\n",
        "outputs": 1,
        "noerr": 0,
        "x": 490,
        "y": 100,
        "wires": [
            [
                "7bf2e7d6.36dfc8"
            ]
        ]
    },
    {
        "id": "7bf2e7d6.36dfc8",
        "type": "mqtt out",
        "z": "c08de27a.3035c",
        "name": "",
        "topic": "",
        "qos": "",
        "retain": "",
        "broker": "a621bb5.8358248",
        "x": 690,
        "y": 100,
        "wires": []
    },
    {
        "id": "a621bb5.8358248",
        "type": "mqtt-broker",
        "z": "",
        "name": "YourMQTTBrokerName",
        "broker": "YourMQTTBrokerName",
        "port": "1883",
        "clientid": "YourMQTTclientID",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": ""
    }
]


EDIT
If you don’t like “phone” as the root topic name, you can of course change it to whatever you want. You simply need to change it within the flow’s function node and in the two Home Assistant sensors.

EDIT
As a refinement, you can create a single macro that triggers on either battery or power changes.

1 Like

Fantastic!!!

Really well done and very useful also for other things!

I will try soon!

hi, if i use localhost not working, like https://localhost:8123

@Raiden123
You might have more luck with your question if you provide some context.
What is “not working”? Tasker RestAPI, MQTT, Node Red?
What are you running? Hassio / HomeAssistant?

If Tasker, localhost would not work because it would resolve to the phone’s IP not your hass instance.
Also, unless you’ve setup TLS / SSL https would not be correct either.

Just saw this: https://www.reddit.com/r/homeassistant/comments/bze8ey/home_assistant_plugin_for_tasker/ and also this post. Hope it helps…