Hi,
Unfortunately HA does not have a cronjob.
So I should build something similar.
1: sensor.solarprognose_naechster_abruf has the atrribute at the first call:
epochTimeUtc: 0
This is picked up by a shell_command and should then retrieve a CURL address.
Unfortunately, “sensor.solarprognose_raw_json_hourly” does not fill.
Here are my sensors + code:
command_line:
- sensor:
name: "Solarprognose_raw_json_hourly"
unique_id: "Solarprognose_raw_json_hourly"
command: >
bash -c "NEXT_FETCH={{ state_attr('sensor.solarprognose_naechster_abruf', 'epochTimeUtc') | default(0) }};
NOW=$(date +%s);
if [ \"$NEXT_FETCH\" = \"0\" ]; then
/usr/bin/curl -s \"https://www.solarprognose.de/web/solarprediction/api/v1?access-token=123456789&project=Garage&algorithm=mosmix&item=inverter&id=123&type=hourly&_format=json\" | jq '{ \"raw\": . }';
elif [ $NOW -lt $NEXT_FETCH ]; then
echo '{\"raw\": {\"status\": \"skip\", \"message\": \"Nächster Abruf noch nicht erreicht\"}}';
else
/usr/bin/curl -s \"https://www.solarprognose.de/web/solarprediction/api/v1?access-token=123456789e&project=Garage&algorithm=mosmix&item=inverter&id=123&type=hourly&_format=json\" | jq '{ \"raw\": . }';
fi"
value_template: "Datenstand: {{ now().strftime('%d.%m.%Y %H:%M:%S') }}"
json_attributes:
- raw
scan_interval: 10
template:
- sensor:
- name: "solarprognose_naechster_abruf"
unique_id: "solarprognose_naechster_abruf"
state: >
{% set raw = state_attr('sensor.solarprognose_raw_json_hourly', 'raw') %}
{% if raw is defined and raw.preferredNextApiRequestAt is defined and raw.preferredNextApiRequestAt.epochTimeUtc is defined %}
{% set ts = raw.preferredNextApiRequestAt.epochTimeUtc | int %}
Nächster Abruf: {{ ts | timestamp_custom('%d.%m.%Y %H:%M:%S', True) }}
{% else %}
Nächster Abruf: sofort
{% endif %}
attributes:
epochTimeUtc: >
{% set raw = state_attr('sensor.solarprognose_raw_json_hourly', 'raw') %}
{% if raw is defined and raw.preferredNextApiRequestAt is defined and raw.preferredNextApiRequestAt.epochTimeUtc is defined %}
{{ raw.preferredNextApiRequestAt.epochTimeUtc }}
{% else %}
0
{% endif %}
sensor.solarprognose_raw_json_hourly is executed every 10 sec. The “state” is updated.
Does anyone see my problem?
Or how did you solve it?
P.s. The complete process should run like this:
1: Initial setup = no timestamp yet. So set 0 so that it is picked up immediately (in 10 sec).
2: Pickup is done. On collection, a timestamp is also given as to when I can collect again. This must then be used.