Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rest:
- #############################################################
- ###### FROM SEARCH ENGINE RETRIEVE CHEAPEST STATION ID ######
- #############################################################
- - resource: https://carburanti.mise.gov.it/ospzApi/search/zone
- method: POST
- payload_template: >
- {
- "points": [
- {
- "lat": {{ state_attr("zone.home", "latitude") }},
- "lng": {{ state_attr("zone.home", "longitude") }}
- }
- ],
- "radius": 10,
- "fuelType": "2-x",
- "priceOrder": "asc"
- }
- headers:
- Content-Type: application/json
- scan_interval: 3600
- timeout: 60
- sensor:
- - name: distributore_economico_diesel_id
- unique_id: distributore_economico_diesel_id
- value_template: >
- {% set first_result = value_json.results | first() %}
- {% if first_result %}
- {{ first_result.id }}
- {% else %}
- unknown
- {% endif %}
- ###############################################
- ###### FROM STATION ID RETRIEVE METADATA ######
- ###############################################
- - resource_template: "https://carburanti.mise.gov.it/ospzApi/registry/servicearea/{{ states('sensor.distributore_economico_diesel_id') }}"
- scan_interval: 86400
- timeout: 60
- sensor:
- - name: distributore_economico_diesel_nome
- unique_id: distributore_economico_diesel_nome
- value_template: "{{ value_json.name }}"
- availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_diesel_marchio
- unique_id: distributore_economico_diesel_marchio
- value_template: "{{ value_json.brand }}"
- availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_diesel_indirizzo
- unique_id: distributore_economico_diesel_indirizzo
- value_template: "{{ value_json.address }}"
- icon: "mdi:map-marker"
- availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_diesel_prezzo
- unique_id: distributore_economico_diesel_prezzo
- value_template: >
- {% set diesel_self = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', true) | first() %}
- {% set diesel_servito = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', false) | first() %}
- {% if diesel_self %}
- {{ diesel_self.price | float(0) }}
- {% elif diesel_servito %}
- {{ diesel_servito.price | float(0) }}
- {% else %}
- unknown
- {% endif %}
- icon: "mdi:gas-station-in-use"
- unit_of_measurement: "€"
- device_class: "monetary"
- availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_diesel_aggiornamento
- unique_id: distributore_economico_diesel_aggiornamento
- value_template: >
- {% set diesel_self = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', true) | first() %}
- {% set diesel_servito = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', false) | first() %}
- {% if diesel_self %}
- {{ diesel_self.insertDate }}
- {% elif diesel_servito %}
- {{ diesel_servito.insertDate }}
- {% else %}
- unknown
- {% endif %}
- icon: "mdi:update"
- device_class: "timestamp"
- availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
- #########################################################
- ###### WHEN ID CHANGES, NEW METADATA ARE RETRIEVED ######
- #########################################################
- automation:
- - id: distributore_economico_diesel_on_id_change
- alias: distributore_economico_diesel_on_id_change
- description: "When the most economic station changes, station metadata are downloaded"
- trigger:
- - platform: state
- entity_id:
- - sensor.distributore_economico_diesel_id
- condition: []
- action:
- - service: homeassistant.update_entity
- target:
- entity_id: sensor.distributore_economico_diesel_marchio # All metadata depends on the same rest sensor, one entity is enough to trigger the update on all
- data: {}
- - id: distributore_economico_diesel_on_ha_load
- alias: distributore_economico_diesel_on_ha_load
- description: "Due to race-condition, during startup when station ID is loaded, the automation to fetch metadata is not triggered. For this particular case we need to trigger it manually"
- trigger:
- - platform: homeassistant
- event: start
- action:
- - delay: "00:00:10"
- - service: homeassistant.update_entity
- target:
- entity_id: sensor.distributore_economico_diesel_marchio
- data: {}
- #recorder:
- # include:
- # entities:
- # - sensor.distributore_economico_diesel_prezzo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement