Hi!
I have a IoT application where I measure temperature in the sea. The connection with the gateway works perfectly. MQTT broker is used in HA and sensor.Vattentemperatur_1 shows nice correct temperatur.
In my case I need to save the temperature value, every time it changes, as a .txt file in a directory on an internet server for further use in an other application.
- id: '1701867996210'
alias: Send Temperature to Internet Server
trigger:
platform: state
entity_id: sensor.Vattentemperatur_1 message
action:
- service: rest_command.send_temperature
mode: single
When running the automation I get the respons in HA LOG:
Error. Url: https://wwww.server.se/t2. Status code 500. Payload: b’21.00’
The temperature value 21.00 is correct but there is a b before as I assume means binary. How can it be? The server assumes it should be a “text/plain” value, I guess?
I have struggled a lot with this and I’m on the limit to give up. What ever I do there is this b. Or do I misunderstand the error message? Maybe there are other errors?
What is the script running on the remote server actually expecting though?
Typically you would be posting stuff to a remote server using multipart/form.
You need to test whether the curl command even works in a terminal first. Like I said previously - I would have expected that you would need to post it as form fields, rather than just uploading a value as text/plain.
But without knowing what the script on the remote server is expecting, it’s difficult to advise further.
You are right. I have tested the curl and it doesn’t seem to work.
Unfortunately my knowledge is very limited. I was so happy that i succeeded with the MQTT data and having the sensor working so I dived into too deep water by just thinking, ha! this last part to save the last registered value to a server must be simple…
I don’t know what you mean by “post it as form fields”
So annoying
Then it’s difficult. I don’t know how it works. Asked the ISP and the only respons I got was: We don’t give out any information. You are supposed to use sftp if you want to transfer files.
When using sftp I must have a local file to transfer which I don’t have.
No config errors or LOG errors in HA but nothing uploaded.
Tested curl command from command line in a folder with a file “temperature.txt”
$ curl --user "user:passw" -T "temperatur.txt" sftp://path/to/t2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:01:20 --:--:-- 0^C
No errors but had to do ^C, nothing happened.
If I do the same operation in FileZilla instead of using curl it works smoothly.
SCP+ssh! Just for the moment I actually use scp in a “manual” command line update for the temp and time files.
scp temperatur.txt tid.txt [email protected]:/path/to/t2
always works perfectly but I have to manually add password.
But I don’t think SCP is supported in HA
Pretty sure scp is part of linux, so it probably is in the image for Home Assistant, as for the password - yup, that’s why you are supposed to use sshpass command before the command that will require a password. The proper way to do it though is to generate an ssh key on the server and copy it to your home assistant box so that homeassistant can log straight in to the server with the key, and skip needing a password.
- id: '1701867996210'
alias: Save Temperature to Local File
description: ''
trigger:
- platform: state
entity_id: sensor.vattentemperatur_1
action:
- service: notify.Vattentemperatur
data_template:
message: ' {{ states.sensor.vattentemperatur_1.state }}'
mode: single
Installed sshpass i Linux and copied the file to the server with good result.
Also tried scp with ssh in HA and it was supported. Wrote password manually and the file “Vattentemperatur.txt” was uploaded
Don’t know how or if sshpass is possible to install and use in HA (docker/container??). I can live with the risk of exposing the password as done in sshpass.
Read the file about setting up ssh keys. Many steps with possibilities to do mistakes. Have to think about that
If you are using Home Assistant Operating System, the commands are executed in the homeassistant container context. So if you test or debug your script, it might make sense to do this in the context of this container to get the same runtime environment.
So close (I think) but have no idea to come around this. Read several articles about this return code 127 but don’t understand how to solve this “container issue”