I;m trying to get a http Post from and android device to Home asistant

i’m trying now for several houres and i seem to do something wrong here

and i read several options, restfull witch, or API

i tried both but all i get is Login attempt failed

i use automagic to post an HTML request and i want to receive it in home assistant

this is my automagic file (screenshot)

this is my configuration.yaml

switch:

  • platform: rest
    resource: http://ip_device:8080:/api/service/switch/temp_up
    body_on: ‘{“active”: “true”}’
    body_off: ‘{“active”: “false”}’
    is_on_template: “{{ value_json.is_active }}”
    headers:
    Content-Type: application/json
    Authorization: ‘Bearer looooong code’

For a start, we cannot see all of your automagic file. Please cut and paste the text, not a picture of it.

Secondly this http://:8080:/api/service/switch/temp_up does not contain a hostname/ip address, but assuming you have just left that out for some reason, what does it return?

I left the IP adress out of the device, editted the post (it was filtered out somehow)

the rest of the automagic is the connections of trigger and action blocks, the info on the screen contains all the information for the connection setup

i’m not at home right now, so i wil post the xml file when i’m home

When you do, please format it properly:

Have you created a long-lived access token and read this?

Your body_on / body_off and is_on_template is identical to the example in the documentation: that suggests that you don’t know what you’re looking for in the response…?

If you’re using the API, you don’t the REST configuration: the API call will create and set the switch for you. I run this from my church router via a cron job, and don’t need to do anything on the HA side. It sets the state and a bunch of attributes:

#!/bin/bash

export HALLT="[MY_LONG_LIVED_TOKEN]"
export IP="`ip addr show dev pppoe0 | grep inet | cut -d\  -f 6`"
export CPU="`/usr/sbin/ubnt-hal getTemp | grep CPU: | cut -d: -f2 | cut -d\  -f1`"
export PHY="`/usr/sbin/ubnt-hal getTemp | grep PHY: | cut -d: -f2 | cut -d\  -f1`"
export LD1="`cat /proc/loadavg | cut -d\  -f1`"
export LD5="`cat /proc/loadavg | cut -d\  -f2`"
export TS="`date -Iseconds`"

curl -k -X POST -H "Authorization: Bearer $HALLT" -H "Content-Type: application/json" \
     -d "{\"state\": \"$IP\", \"attributes\": \
         {\"CPU\": \"$CPU\", \
          \"PHY\": \"$PHY\", \
          \"LD1\": \"$LD1\", \
          \"LD5\": \"$LD5\",
          \"updated\": \"$TS\" }}" \
     https://[MY_EXTERNAL_URL]/api/states/sensor.church_router

Another way to do what you want is to set up the switch in HA and use tiles on the phone:

i have created a long livd access token, but somehow it does not work

so with the POST methode i wont need to confige anything in HA?

so i’ll remove all of that from the config file.

then my question, is my xml file configured correctly

i tried for more then 10 houre’s (if not double that)

with the switch, platform: rest

also with api, but if i understand correctly, all that is not needed.

so i just need to get my xml file correct with the long-lived access token and thats it?

What are you actually trying to do? Your API call is to [...]/services/switch/temp_up, but that’s not a valid service call for a switch.

A switch has a state that is on or off. You need to use the API state setting call, which is the first POST call shown here:

If you are trying to create and turn on the entity switch.temp_up, you need to POST:

{ "state": "on" }

to [...]/api/states/switch.temp_up, along with the headers:

Authorization: Bearer [YOUR LLT]
Content-Type: application/json

its a rotery switch that creates an F2 and a F3 trigger (CW and CCW rotation)

the plan is to make an automation that increases and decreases the “set temperature” once its triggerd in HA, but i must be able to read the switch in HA first as soon as the rotery switch has trigger an F2 or F3 and send the http Post command

i created an automation with a push switch (remote) in the past that wil reset automaticly after being triggerd, (the same butten input is used to turn on/of a light) so i can use the { “state”: “on” } as a min or plus counter internal

i just need to get them inside HA

the F2 and F3 triggers are getting triggered

or am i using it the wrong way?

i’m open to any suggestions

its an Tuya S8E and i managed the get the humidity and temperature values out of it and also managed to get the relays to work

so if anybody needs help with that, please ask

Ah, not a simple on/off switch then. Create an Input Number in HA (let’s call it input_number.temp for this example) with the initial value you want:

and use the increment and decrement services via the API. So when you get an F2 (if that’s the increment), POST to [...]/api/services/input_number.temp/increment with the token and Content-Type.

i tried again for more then an houre, but it just wont happen,

i’m getting the following massage in HA:

Login attempt failed
Login attempt or request with invalid authentication from [S8E] ([S8E]). See the log for details.

i can’t seem to find them in the log

here is my configuration.yaml:


default_config:
http:

input_number:
  temp:
    name: Temperature
    initial: 18
    min: 16
    max: 22
    step: 1

automation:

alias: Room_Temperature
description: ""
trigger:
  - platform: state
    entity_id:
      - input_number.temp
condition: []
action:
  - service: input_number.increment
    data: {}
mode: single

here is my xml file:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<data version="1.38.0">
  <trigger type="key_event">
    <useDefaultName>true</useDefaultName>
    <name>Hardware Key Event: F3 Up</name>
    <enabled>true</enabled>
    <keyList>F3</keyList>
    <keyDown>false</keyDown>
    <keyUp>true</keyUp>
    <blockEvent>false</blockEvent>
  </trigger>
  <action type="http_request">
    <useDefaultName>false</useDefaultName>
    <name>Temp_up</name>
    <url>http://[ha-ip]/api/services/input_number.temp/increment</url>
    <verifyCertificates>false</verifyCertificates>
    <basicAuthentication>false</basicAuthentication>
    <username></username>
    <clientCert>false</clientCert>
    <clientCertPath></clientCertPath>
    <httpMethod>POST</httpMethod>
    <networkType>WIFI</networkType>
    <httpContentType>GENERAL_TEXT</httpContentType>
    <contentType>application/json</contentType>
    <generalTextData>{'{"entity_id": "Temperature"}'}</generalTextData>
    <formFieldList>Lastname=XYZ,Firstname=ABC</formFieldList>
    <uploadFieldName>upload_field</uploadFieldName>
    <uploadFilePath></uploadFilePath>
    <binaryContentType>application/octet-stream</binaryContentType>
    <binaryFilePath></binaryFilePath>
    <timeout>60000</timeout>
    <setCustomHTTPHeaders>true</setCustomHTTPHeaders>
    <customHTTPHeaders>Authorization: 'Bearer [MY_LONG_LIVED_TOKEN]'
content-type: 'application/json'</customHTTPHeaders>
    <storeInVariable>true</storeInVariable>
    <variable>response</variable>
    <encoding></encoding>
    <path>/storage/emulated/0/Download/file.bin</path>
    <followRedirects>true</followRedirects>
    <skipConnectivityCheck>false</skipConnectivityCheck>
  </action>
  <flow type="flow">
    <name>Tempup</name>
    <enabled>true</enabled>
    <executionPolicy>PARALLEL</executionPolicy>
    <triggercontainer id="t1" x="70.0" y="52.5">
      <trigger>Hardware Key Event: F3 Up</trigger>
    </triggercontainer>
    <actioncontainer id="t2" x="70.0" y="262.5">Temp_up</actioncontainer>
    <connection from="t1" to="t2" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
  </flow>
</data>

I managed to do it with a webhook - automation

Thanks for the information and time, it helped me get things setup

If there are people intrested, i can post how i managed to get the rotery knob increase / decrease a input value

Thx again

Please do.

thx for helping me with the input_number.temp thing, was a big help

my configuration.yaml

input_number:
  temp:
    name: Set_Temperature
    icon: mdi:thermometer-lines
    initial: 18
    min: 16
    max: 22
    step: 0.1

this as automation: Temp down

alias: Temp_Down
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
    local_only: true
    webhook_id: temp_down
condition: []
action:
  - service: input_number.decrement
    data: {}
    target:
      entity_id: input_number.temp
mode: single

this as Temp up:

alias: Temp_Up
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
    local_only: true
    webhook_id: temp_up
condition: []
action:
  - service: input_number.increment
    data: {}
    target:
      entity_id: input_number.temp
mode: single

for temp up, and for down you change F3 for F2

and the webhook to “webhook/temp_down”

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<data version="1.38.0">
  <trigger type="key_event">
    <useDefaultName>true</useDefaultName>
    <name>Hardware Key Event: F3 Up</name>
    <enabled>true</enabled>
    <keyList>F3</keyList>
    <keyDown>false</keyDown>
    <keyUp>true</keyUp>
    <blockEvent>false</blockEvent>
  </trigger>
  <action type="http_request">
    <useDefaultName>false</useDefaultName>
    <name>Temp_up</name>
    <url>http://[ha-ip]/api/webhook/temp_up</url>
    <verifyCertificates>false</verifyCertificates>
    <basicAuthentication>false</basicAuthentication>
    <username></username>
    <clientCert>false</clientCert>
    <clientCertPath></clientCertPath>
    <httpMethod>POST</httpMethod>
    <networkType>WIFI</networkType>
    <httpContentType>GENERAL_TEXT</httpContentType>
    <contentType>application/json</contentType>
    <generalTextData>{'{"entity_id": "temp.Temperature"}'}</generalTextData>
    <formFieldList>Lastname=XYZ,Firstname=ABC</formFieldList>
    <uploadFieldName>upload_field</uploadFieldName>
    <uploadFilePath></uploadFilePath>
    <binaryContentType>application/octet-stream</binaryContentType>
    <binaryFilePath></binaryFilePath>
    <timeout>60000</timeout>
    <setCustomHTTPHeaders></setCustomHTTPHeaders>
    <customHTTPHeaders></customHTTPHeaders>
    <storeInVariable>true</storeInVariable>
    <variable>response</variable>
    <encoding></encoding>
    <path>/storage/emulated/0/Download/file.bin</path>
    <followRedirects>true</followRedirects>
    <skipConnectivityCheck>false</skipConnectivityCheck>
  </action>
  <flow type="flow">
    <name>Tempup</name>
    <enabled>true</enabled>
    <executionPolicy>PARALLEL</executionPolicy>
    <triggercontainer id="t1" x="70.0" y="52.5">
      <trigger>Hardware Key Event: F3 Up</trigger>
    </triggercontainer>
    <actioncontainer id="t2" x="70.0" y="262.5">Temp_up</actioncontainer>
    <connection from="t1" to="t2" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
  </flow>
</data>
1 Like