Auto translating a weather forecast

Hi everbody,

I was wondering if there is anybody who knows if it’s possible to set up a temple sensor or a scripts that could translate the summary data from DarkSky. I think this could maybe be done with some kind of HTTP request to Google translate, but I have no idea how to implement it.

Google offers the service via the cloud platform https://cloud.google.com/translate/docs/translating-text#translate-translate-text-protocol - I can sign up for a 12 month free trial, but is this the only way to get this functionallity?

Making a little progress here. using the url from this script example https://ctrlq.org/code/19909-google-translate-api

Example translating “overcast (en) to overskyet (da)”

https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=da&dt=t&q=encodeURI(overcast);

And it works. the url downloads a file called f.txt

[[["encodeURI (overskyet);","encodeURI(overcast);",null,null,0]],null,"en"]

Now I just need to figure out how to somehow implement this in a script that takes the state of a sensor, puts it into the url and writes the reply to another sensor.

It seems pretty simple, but so is my coding skills or lack thereof. :sunglasses:

This sounds like a fun project for me. I’ll take a crack at it this aft and see what I can figure out.

1 Like

This is as far as I’ve gotten. It works if you hardcode the text but doesn’t seem to fetch a variable from another sensor. Maybe someone else has ideas.

  - platform: rest
    name: Translate Weather
    resource: https://translation.googleapis.com/language/translate/v2/?key=YOUR_API_KEY_HERE
    payload: '{ "q": "{{states.sensor.dark_sky_minutely_summary.state}}", "target": "ru" }'
    method: POST
    value_template: '{{ value_json.data.translations[0].translatedText }}'

Great work man, I was messing around with the idea of a rest sensor myself - not getting very fare though.

I don’t think that HA can handle a entity within a entity like that? … However It might work if we had a local json file that a script wrote the output of the dark sky sensor to, every x minutes? the rest sensor could then perhaps read the output from the local json file and serve the translation that way?

It’s one of those things where I can think it, but have a hard time coding it so it’s great to se someone else jumping in as well.

Yeah I was hoping it would. I’ve used a sensor variable inside a camera URL before and also a sensor on a sensor so I was hopeful this would work. It may need a tweak to the component. I might try my hand at that. There’s got to be a way to make this happen. I’ll keep trying on my end. Let me know if you come up with anything

Very interesting, I have never had a use for it (before now) but great to know that it works with some components.
Sounds great, I’ll try my hand on some shell scripting in the meantime.

I agree, this can’t be that hard to get working, and I think that automatic translation should have a great potential with how international this community is. I was honestly a bit surprised that I could not find anybody that had already done something similar.

Finaly a little progress here. I got a working “translate word from file sensor going” - due to a lot of weird syntax errors in bash this is done with two bash scripts and a sensor

First the sensor: (scan interval is very low due to testing)

  - platform: command_line
    name: dark_sky_translation
    command: "bash /home/homeassistant/.homeassistant/link.sh"
    value_template: '{{ value_json.data.translations[0].translatedText }}'
    scan_interval: 30

The first script ts.sh takes the word from www/ts.txt, creates a curl link and passes that to link.sh.

#!/bin/bash

WORD=$(cat /home/homeassistant/.homeassistant/www/ts.txt)
APIKEY=YOUR-API-KEY-HERE

link_data()
{
  cat <<EOF
curl -XPOST -H "Content-type: application/json" -d '{ "q": "$WORD", "target": "da" }' 'https://translation.googleapis.com/language/translate/v2/?key=$APIKEY'
EOF
}

LINK=$(echo "$(link_data)")

echo $LINK > /home/homeassistant/.homeassistant/link.sh

link.sh then just holds the curl link that the sensor is using. I would have liked to have ts.sh just give the link, but it gave all kinds of weird errors and after spending most of today trying to solve it I gave up.

Next step is the first step - actually getting text into ts.txt, hopefully I can get that figures out soon.

I just had a look to the API of DarkSky…

They already offer different languages :slight_smile:

https://darksky.net/dev/docs/forecast

You can use the ‘lang’ parameter when you request the forecast!

lang=[language] (optional)

You are a saint :innocent: … but of course danish is not supported … dammit - being so close to a soultion time and time again :sweat_smile:

Just help them with their translations… Once Danish is added you (and probably more users around the globe) can use it :slight_smile: